diff --git a/background.js b/background.js index f2759f1f..d047cb85 100644 --- a/background.js +++ b/background.js @@ -1,3 +1,6 @@ +// Used only on Firefox, which does not support non persistent background pages. +var contentScriptRegistrations = {}; + chrome.tabs.onUpdated.addListener(function(tabId) { chrome.tabs.sendMessage(tabId, { message: 'update', @@ -6,38 +9,53 @@ chrome.tabs.onUpdated.addListener(function(tabId) { chrome.runtime.onMessage.addListener(function (request, sender, callback) { switch(request.message) { - case "submitTimes": - submitTimes(request.videoID, callback); - - //this allows the callback to be called later by the submitTimes function - return true; - case "addSponsorTime": - addSponsorTime(request.time, request.videoID, callback); - - //this allows the callback to be called later - return true; - case "getSponsorTimes": - getSponsorTimes(request.videoID, function(sponsorTimes) { - callback({ - sponsorTimes: sponsorTimes - }) - }); - - //this allows the callback to be called later - return true; - case "submitVote": - submitVote(request.type, request.UUID, callback); - - //this allows the callback to be called later - return true; - case "alertPrevious": - chrome.notifications.create("stillThere" + Math.random(), { - type: "basic", - title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?", - message: chrome.i18n.getMessage("leftTimes"), - iconUrl: "./icons/LogoSponsorBlocker256px.png" - }); - } + case "submitTimes": + submitTimes(request.videoID, callback); + + //this allows the callback to be called later by the submitTimes function + return true; + case "addSponsorTime": + addSponsorTime(request.time, request.videoID, callback); + + //this allows the callback to be called later + return true; + case "getSponsorTimes": + getSponsorTimes(request.videoID, function(sponsorTimes) { + callback({ + sponsorTimes: sponsorTimes + }) + }); + + //this allows the callback to be called later + return true; + case "submitVote": + submitVote(request.type, request.UUID, callback); + + //this allows the callback to be called later + return true; + case "alertPrevious": + chrome.notifications.create("stillThere" + Math.random(), { + type: "basic", + title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?", + message: chrome.i18n.getMessage("leftTimes"), + iconUrl: "./icons/LogoSponsorBlocker256px.png" + }); + return false; + case "registerContentScript": + browser.contentScripts.register({ + allFrames: request.allFrames, + js: request.js, + css: request.css, + matches: request.matches + }).then(() => void (contentScriptRegistrations[request.id] = registration)); + + return false; + case "unregisterContentScript": + contentScriptRegistrations[request.id].unregister(); + delete contentScriptRegistrations[request.id]; + + return false; +} }); //add help page on install diff --git a/manifest.json b/manifest.json index 149d45f3..39a417ca 100644 --- a/manifest.json +++ b/manifest.json @@ -44,7 +44,7 @@ "permissions": [ "storage", "notifications", - "https://sponsor.ajay.app/*" + "https://sponsor.ajay.app/*" ], "optional_permissions": [ "*://*/*", diff --git a/options/options.js b/options/options.js index f4ace839..74b2cfc8 100644 --- a/options/options.js +++ b/options/options.js @@ -2,7 +2,8 @@ window.addEventListener('DOMContentLoaded', init); var invidiousInstancesRegex = []; for (const url of supportedInvidiousInstances) { - invidiousInstancesRegex.push("*://*." + url + "/*"); + invidiousInstancesRegex.push("https://*." + url + "/*"); + invidiousInstancesRegex.push("http://*." + url + "/*"); } async function init() { @@ -19,6 +20,7 @@ async function init() { switch (optionsElements[i].getAttribute("option-type")) { case "toggle": let option = optionsElements[i].getAttribute("sync-option"); + chrome.storage.sync.get([option], function(result) { let optionResult = result[option]; let checkbox = optionsElements[i].querySelector("input"); @@ -141,7 +143,9 @@ function invidiousOnClick(checkbox, option) { firefoxCSS.push({file}); } - let registration = await browser.contentScripts.register({ + chrome.runtime.sendMessage({ + message: "registerContentScript", + id: "invidious", allFrames: true, js: firefoxJS, css: firefoxCSS, @@ -178,7 +182,10 @@ function invidiousOnClick(checkbox, option) { }); } else { if (isFirefox()) { - // Nothing for now + chrome.runtime.sendMessage({ + message: "unregisterContentScript", + id: "invidious" + }); } else { chrome.declarativeContent.onPageChanged.removeRules(["invidious"]); }