-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbackground.js
More file actions
executable file
·140 lines (119 loc) · 4.42 KB
/
background.js
File metadata and controls
executable file
·140 lines (119 loc) · 4.42 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// When extension installed, open options page at once
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed.");
chrome.runtime.openOptionsPage(function () {
console.log("Options page opened.");
});
});
// Create alarm
// Documentation for "chrome.alarms": https://developer.chrome.com/extensions/alarms
function createAlarm() {
getAlarmOptions(function (options) {
chrome.alarms.clearAll(function () {
console.log("Alarms cleared.");
if (options && options.activated) {
chrome.alarms.create("notify-by-script", { periodInMinutes: options.period });
console.log("Alarms created.");
}
});
});
}
createAlarm();
// Configure what will happened when alarm triggered
chrome.alarms.onAlarm.addListener((alarm) => {
console.log("Alarm triggered: " + alarm.name);
executeAlarm(alarm.name);
});
// Documentation for "chrome.notifications": https://developer.chrome.com/extensions/notifications
function notify(alarmName, title, message) {
chrome.notifications.getAll(function (notifications) {
console.log("Current notification ids: " + JSON.stringify(notifications));
const notificationIds = Object.keys(notifications);
for (let i = 0; i < notificationIds.length; i++) {
const eachNotificationId = notificationIds[i];
if (eachNotificationId.startsWith(alarmName)) {
console.log("Notification already exists: " + eachNotificationId);
chrome.notifications.clear(eachNotificationId, function (wasCleared) {
if (wasCleared) {
console.log("Notification cleared: " + eachNotificationId);
} else {
console.log("Notification couldn't be cleared: " + eachNotificationId);
}
});
}
}
const notificationId = alarmName + "-" + new Date().getTime();
chrome.notifications.create(notificationId, {
"type": "basic",
"title": title,
"iconUrl": chrome.runtime.getURL("icon.png"),
"message": message
}, function () {
console.log("Notification created: " + notificationId);
});
});
}
// Options page messages listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Message received: " + request.message);
if (request.message === "newOptionsSaved") {
createAlarm();
sendResponse({ message: "alarmsReCreated" });
} else if (request.message === "testScript") {
executeAlarmWithOptions(request.alarmOptions, "test-script");
sendResponse({ message: "scriptEvaluated" });
}
}
);
// Get alarm options from Chrome Storage API
function getAlarmOptions(callback) {
console.log("Getting options...");
chrome.storage.sync.get({
period: 5,
script: '',
activated: false
}, function (options) {
callback(options);
});
}
// Get alarm options, evaluate and notify
function executeAlarm(alarmName) {
getAlarmOptions(function (options) {
executeAlarmWithOptions(options, alarmName);
});
}
async function ensureOffscreen() {
if (await chrome.offscreen.hasDocument()) {
console.log("Offscreen document already exists.");
return;
}
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['IFRAME_SCRIPTING'],
justification: 'evaluation of the script provided by the user itself',
});
}
async function executeAlarmWithOptions(alarmOptions, alarmName) {
await ensureOffscreen();
if (alarmOptions && alarmOptions.script) {
chrome.runtime.sendMessage({ type: "evaluate", alarmName, alarmOptions: alarmOptions });
}
}
// Evaluation result message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type !== "evaluation-result") {
return;
}
if (request.data && request.data.result) {
notify(request.data.alarmName, request.data.result.title, request.data.result.message);
}
}
);
// Execute alarm when browser action clicked
chrome.action.onClicked.addListener((tab) => {
console.log("Browser action clicked.");
getAlarmOptions(function (options) {
options.activated = true;
executeAlarmWithOptions(options, "run-with-browser-action");
});
});