-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
executable file
·117 lines (101 loc) · 3.93 KB
/
main.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
// When extension installed, open options page at once
chrome.runtime.onInstalled.addListener(function () {
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(function (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.extension.getURL("icon.png"),
"message": message
}, function () {
console.log("Notification created: " + notificationId);
});
});
}
// Options page messages listener
chrome.runtime.onMessage.addListener(
function (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);
});
}
function executeAlarmWithOptions(alarmOptions, alarmName) {
if (alarmOptions && alarmOptions.script) {
const result = eval(alarmOptions.script);
if (result !== undefined) {
notify(alarmName, result.title, result.message);
}
}
}
// Execute alarm when browser action clicked
chrome.browserAction.onClicked.addListener(function (tab) {
console.log("Browser action clicked.");
getAlarmOptions(function (options) {
options.activated = true;
executeAlarmWithOptions(options, "run-with-browser-action");
});
});