-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
42 lines (36 loc) · 1.08 KB
/
background.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
chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
if (changeInfo.status !== "complete") return;
if (!tab.url) return;
let configs = await getObjectFromStorage("configs");
if (!configs) return;
let matchedConfig = getMatchedConfig(tab.url, configs);
chrome.tabs.sendMessage(
tabId,
{ type: "notify", config: matchedConfig },
null
);
});
function getMatchedConfig(url, configs) {
const urlObj = new URL(url);
const originAndPath = urlObj.origin + urlObj.pathname;
return configs.find((config) => originAndPath.match(new RegExp(config.url)));
}
chrome.action.onClicked.addListener(function (tab) {
chrome.tabs.create({ url: "options.html" });
});
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
chrome.runtime.openOptionsPage();
}
});
const getObjectFromStorage = async function (key) {
return new Promise((resolve, reject) => {
try {
chrome.storage.sync.get(key, function (value) {
resolve(value[key]);
});
} catch (ex) {
reject(ex);
}
});
};