-
Notifications
You must be signed in to change notification settings - Fork 6
/
service_worker.js
51 lines (45 loc) · 1.24 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
const toggleContextID = "contextToggle";
let lastSendResponse;
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
type : "checkbox",
id : toggleContextID,
title : chrome.i18n.getMessage("contextToggle"),
contexts: ["video"]
});
})
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId !== toggleContextID) return;
if (lastSendResponse) {
try {
lastSendResponse({
clicked : true,
ignoreVideo: info.checked
});
} catch (e) {
// Errors occur when pages are closed or reloaded,
// leaving the background script with an expired
// sendResponse. No action needed.
}
}
chrome.contextMenus.update(toggleContextID, {
checked: info.checked
});
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(lastSendResponse) try {
lastSendResponse({clicked: false});
} catch (e) {
// Errors occur when pages are closed or reloaded,
// leaving the background script with an expired
// sendResponse. No action needed.
}
lastSendResponse = sendResponse;
chrome.contextMenus.update(toggleContextID, {
checked: request.toggleChecked,
enabled: request.toggleEnabled
});
return true; // async sendResponse
}
);