-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
75 lines (65 loc) · 1.8 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// constant
import onIconPath from './const/onIconPath.js';
import defaultIconPath from './const/defaultIconPath.js';
// manager
import statusManager from './utils/statusManager.js';
import urlManager from './utils/urlManager.js';
// function
import cleaningUrl from './utils/cleaningUrl.js';
const debug = (inputContent) => {
console.log(`[DEBUG] ${new Date()}: `);
console.log("\t", inputContent);
};
const getDateParamsString = () => {
const dt = new Date();
let year = dt.getFullYear();
let month = dt.getMonth() + 1;
const days = dt.getDate();
// get 6 month ago
month -= 6;
if (month < 1) {
month += 12;
year -= 1;
};
const dateParams = `&tbs=cdr%3A1%2Ccd_min%3A${month}%2F${days}%2F${year}`;
return dateParams;
}
const checkAndUpdateUrl = (tabId, tab) => {
// debug(`inside checkAndUpdateUrl ${tab.url}`);
const originalUrl = tab.url;
if (urlManager.isGoogle(tab.url) && !urlManager.getParam(tab.url, "url")) {
let newUrl = cleaningUrl(originalUrl);
if (statusManager.getStatus()) {
newUrl += getDateParamsString()
};
if (newUrl !== originalUrl) {
// debug(`url updated from ${originalUrl} to ${newUrl}`);
chrome.tabs.update(tabId, {
url: newUrl
});
};
};
}
// when extension icon is clicked
chrome.action.onClicked.addListener((tab) => {
// change status
statusManager.changeStatus();
let currentStatus = statusManager.getStatus();
debug(`Current status: ${currentStatus}`);
// Change icon
if (currentStatus) {
chrome.action.setIcon({
path: onIconPath,
});
} else {
chrome.action.setIcon({
path: defaultIconPath,
});
};
// change url
checkAndUpdateUrl(tab.id, tab)
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// change url
checkAndUpdateUrl(tab.id, tab);
});