-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (40 loc) · 1.86 KB
/
background.js
File metadata and controls
44 lines (40 loc) · 1.86 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
chrome.runtime.onInstalled.addListener(async details => {
switch (details.reason) {
case chrome.runtime.OnInstalledReason.INSTALL:
chrome.runtime.openOptionsPage();
break;
case chrome.runtime.OnInstalledReason.UPDATE:
if (details.previousVersion.startsWith("2.") || details.previousVersion.startsWith("3.0.")) {
chrome.runtime.openOptionsPage();
}
break;
default:
const manifest = chrome.runtime.getManifest();
const permissions = { "origins": manifest.host_permissions };
if (!await chrome.permissions.contains(permissions)) {
chrome.runtime.openOptionsPage();
}
}
});
// Page actions are disabled by default and enabled on select tabs
chrome.action.disable();
// Google Chrome の API ページでは、declarativeContent を使うように書かれている。
// https://developer.chrome.com/docs/extensions/reference/api/action?hl=ja#emulate_actions_with_declarativecontent
// しかし、Firefox は declarativeContent に対応していない。
// > Unsupported APIs - DeclarativeContent API
// > Chrome's declarativeContent API is not implemented.
// > https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#unsupported_apis
// Bugzilla によれば、tabs API で代替できるとのことだったので、これを使って実装する。
// https://bugzilla.mozilla.org/show_bug.cgi?id=1435864
chrome.tabs.onUpdated.removeListener(onUpdated);
chrome.tabs.onUpdated.addListener(onUpdated);
function onUpdated(tabId, changeInfo, tab) {
if (changeInfo.status != "loading") {
return;
}
if (/https?:\/\/atcoder.jp\/contests\/[^\/]+\/tasks\/[^\/]+/.test(changeInfo.url)) {
chrome.action.enable(tabId);
} else {
chrome.action.disable();
}
}