-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
123 lines (103 loc) · 4.53 KB
/
service_worker.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//Set "Today" breakpoint as following like social game
const TODAY_BREAKPOINT_HOUR = 4;
const CHECK_FREQUENCY_MINUTES = 10;
const ONE_DAY_SECONDS = 86400 * 1000;
let mainId = null;
const detect_main_window = () => {
chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, (windows) => {
if (windows.length == 1) {
mainId = null;
return;
}
const sortedWindowIdArray = windows.sort((a, b) => b.tabs.length - a.tabs.length).map((win) => win.id);
mainId = sortedWindowIdArray[0];
}
)
}
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason == "install") {
//Init data
chrome.storage.local.set({ 'pages': {} });
chrome.storage.local.set({ "last_check_time": new Date().getTime() });
chrome.storage.local.set({ "menu": [1, 3, 7, 14, 30, 90, 180, 365, 1000] });
detect_main_window();
}
});
chrome.runtime.onStartup.addListener(() => {
setTimeout(() => {
detect_main_window();
checkPeriodically();
}, 10 * 1000);
}
);
chrome.windows.onCreated.addListener(detect_main_window);
chrome.windows.onRemoved.addListener(detect_main_window);
const checkPeriodically = () => {
chrome.storage.local.get(['pages', "last_check_time"], (result) => {
if (!result.pages) return;
const now = new Date();
const now_seconds = now.getTime();
const last_check_time = result.last_check_time;
const is_today_breakpoint = now.getHours() == TODAY_BREAKPOINT_HOUR &&
now.getMinutes() < CHECK_FREQUENCY_MINUTES * 1.5;//inclease to allow time error
let browsing_list = [];
for (const [url, detail] of Object.entries(result.pages)) {
let browse = false;
const elapsed_time = now_seconds - detail.last_shown_time;
if (elapsed_time > detail.interval * ONE_DAY_SECONDS) {
browse = true;
} else if ((now_seconds - last_check_time) > 2 * CHECK_FREQUENCY_MINUTES * 60 * 1000 || //If last_check_time is more than CHECK_FREQUENCY_MINUTES * 2 ago, determine now is shortly after booting time.
is_today_breakpoint) {
let today_start_time = now_seconds - ((now.getHours() - TODAY_BREAKPOINT_HOUR) * 3600 + now.getMinutes() * 60 + now.getSeconds()) * 1000;
if (today_start_time > now_seconds) today_start_time -= ONE_DAY_SECONDS//modify when now is between AM 0:00-5:00
const elapsed_days = Math.ceil((today_start_time - detail.last_shown_time) / ONE_DAY_SECONDS);
if (elapsed_days <= 0) continue;
if (elapsed_days >= detail.interval) browse = true;
else if (detail.interval % 7 == 0) {//if interval is n weeks, priority is given to the day of the week
if (((detail.interval - elapsed_days) / 7 < 1.0) && now.getDay() == detail.day_of_week && now.getHours() >= TODAY_BREAKPOINT_HOUR) browse = true;
}
}
if (browse) {
browsing_list.push(url)
detail.last_shown_time = now_seconds;
}
}
chrome.storage.local.set({ "last_check_time": now_seconds })
if (browsing_list.length) {
for (const [index, url] of browsing_list.entries()) {
if (mainId == null) setTimeout(() => { chrome.tabs.create({ url: url }) }, index * 1000);
else setTimeout(() => { chrome.tabs.create({ url: url, windowId: mainId }) }, index * 1000);
}
chrome.storage.local.set({ "pages": result.pages });
}
});
}
function reloadBadge(tab) {
if (!tab || !tab.url) return;
chrome.storage.local.get('pages', (result) => {
if (!result || !result.pages) {
return;
}
const detail = result.pages[tab.url];
if (detail) {
chrome.action.setBadgeText({ "text": String(detail.interval) })
chrome.action.setBadgeBackgroundColor({ color: "black" });
} else {
chrome.action.setBadgeText({ "text": "" })
}
})
}
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
reloadBadge(tab)
})
})
chrome.tabs.onUpdated.addListener((_tabId, _changeInfo, tab) => {
reloadBadge(tab)
})
// service_worker(including alarms) sleeps when browser is not active for a while
chrome.alarms.create("periodic", { periodInMinutes: 10 });
chrome.alarms.onAlarm.addListener(function (_alarm) {
detect_main_window();
checkPeriodically();
});