First Commit

This commit is contained in:
2022-09-02 20:56:31 +03:00
parent 9745407c31
commit e2201ce3d4
9 changed files with 442 additions and 1 deletions

48
extension/background.js Normal file
View File

@@ -0,0 +1,48 @@
const activeTabs = {};
let contentScriptRerunner = null;
function runContentScript(tabId) {
chrome.tabs.executeScript(tabId, {
allFrames: true,
file: 'run.js',
});
}
function rerunContentScripts() {
// unfortunately we need to rerun periodically to handle iframes changing..
// eventually fixed when we require the new host permission or chrome releases a content script api
for (const tabId of Object.keys(activeTabs)) {
runContentScript(parseInt(tabId, 10));
}
}
function registerDynamicContentScript() {
chrome.tabs.onUpdated.addListener((tabId, {status}, {url}) => {
if (!status || !url) {
return;
}
runContentScript(tabId);
activeTabs[tabId] = true;
if (contentScriptRerunner == null) {
contentScriptRerunner = setInterval(rerunContentScripts, 5000);
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
delete activeTabs[tabId];
if (contentScriptRerunner != null && Object.keys(activeTabs).length === 0) {
clearInterval(contentScriptRerunner);
contentScriptRerunner = null;
}
});
}
chrome.runtime.onInstalled.addListener(({reason}) => {
if (reason !== 'install') {
return;
}
});
registerDynamicContentScript();