-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
32 lines (27 loc) · 1.11 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
// tabId (int) and <if content script loaded> (bool) kv pairs,to indicate if a tab injected content script
/*global chrome*/
let executedContentScriptByTabId = {};
chrome.webNavigation.onCommitted.addListener(async ({ tabId, url }) => {
console.log(`web switching. tabId: ${tabId}, url: ${url}`);
executedContentScriptByTabId[tabId] = false;
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action == 'inject-content-script') {
let targetTabId = request.tabId;
if (executedContentScriptByTabId[targetTabId]) {
sendResponse({ msg: 'DONE (no need to inject again)' });
return;
}
chrome.scripting
.executeScript({
target: { tabId: targetTabId },
files: ['./dist/main.bundle.js'],
// files: ["content-script.js"]
})
.then(() => {
console.log(`executed content script to tab ${targetTabId}`);
executedContentScriptByTabId[targetTabId] = true;
});
sendResponse({ msg: 'DONE' });
}
});