-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
64 lines (55 loc) · 2.05 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
// chrome.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
// console.log('Worker',
// sender.tab
// ? "from a content script:" + sender.tab.url
// : "from the extension"
// );
// if (msg.action === "execute-script") {
// // Get the current tab.
// const activeTab = msg.activeTab;
// console.log("Worker execute-script", activeTab.id, activeTab);
// // const contentResponse = await chrome.tabs.sendMessage(activeTab.id, {
// // action: "test",
// // tabId: activeTab.id,
// // });
// // console.log("worker contentResponse", contentResponse);
// (async () => {
// const activeTab = msg.activeTab;
// console.log("async call", activeTab);
// const response = await chrome.tabs.sendMessage(activeTab.id, {
// action: "test",
// tabId: activeTab.id,
// });
// // do something with response here, not outside the function
// console.log('Async response to worker', response);
// })();
// }
// sendResponse(true);
// });
// chrome.action.onClicked.addListener((tab) => {
// console.log("Action clicked", tab);
// alert("Action clicked");
// });
// service-worker.js
// Wrap in an onInstalled callback to avoid unnecessary work
// every time the service worker is run
chrome.runtime.onInstalled.addListener(() => {
// Page actions are disabled by default and enabled on select tabs
chrome.action.disable();
// Clear all rules to ensure only our expected rules are set
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// Declare a rule to enable the action on example.com pages
let exampleRule = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlContains: "loop.microsoft.com/p/" },
}),
],
actions: [new chrome.declarativeContent.ShowAction()],
};
console.log('rules', exampleRule);
// Finally, apply our new array of rules
let rules = [exampleRule];
chrome.declarativeContent.onPageChanged.addRules(rules);
});
});