-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
executable file
·58 lines (50 loc) · 1.55 KB
/
background.js
File metadata and controls
executable file
·58 lines (50 loc) · 1.55 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
// 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.");
});
});
// Get alarm options from Chrome Storage API
function getAlarmOptions(callback) {
console.log("Getting options...");
chrome.storage.sync.get({
period: 5,
activated: false
}, function (options) {
callback(options);
});
}
// Create alarm
// Documentation for "chrome.alarms": https://developer.chrome.com/extensions/alarms
function createAlarm() {
getAlarmOptions((options) => {
chrome.alarms.clearAll(function () {
console.log("Alarms cleared.");
if (options && options.activated) {
chrome.alarms.create("vocabReminder", { periodInMinutes: options.period });
console.log("Alarm created.");
}
});
});
}
createAlarm();
// 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" });
}
});
// Alarm listener
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === "vocabReminder") {
try {
await chrome.action.openPopup();
console.log("Popup opened.");
} catch (error) {
// Ignored
}
}
});