aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/background.ts
diff options
context:
space:
mode:
authorAjay <[email protected]>2023-08-11 11:39:06 -0400
committerAjay <[email protected]>2023-08-11 11:39:06 -0400
commite3f3ed20e690a53bde98be9bcf371a591ca102e1 (patch)
tree592cfb0f12542834546e4e4e53342418679c88be /src/background.ts
parent52149f4c0f2d24a93426c968d537922a5360d8d9 (diff)
downloadSponsorBlock-e3f3ed20e690a53bde98be9bcf371a591ca102e1.tar.gz
SponsorBlock-e3f3ed20e690a53bde98be9bcf371a591ca102e1.zip
Enable non persistent background page on Firefox
Diffstat (limited to 'src/background.ts')
-rw-r--r--src/background.ts61
1 files changed, 46 insertions, 15 deletions
diff --git a/src/background.ts b/src/background.ts
index 7ed4d2b2..5b0bceb8 100644
--- a/src/background.ts
+++ b/src/background.ts
@@ -148,27 +148,58 @@ chrome.runtime.onInstalled.addListener(function () {
*
* @param {JSON} options
*/
-function registerFirefoxContentScript(options: Registration) {
- const oldRegistration = contentScriptRegistrations[options.id];
- if (oldRegistration) oldRegistration.unregister();
-
- chrome.contentScripts.register({
- allFrames: options.allFrames,
- js: options.js,
- css: options.css,
- matches: options.matches
- }).then((registration) => void (contentScriptRegistrations[options.id] = registration));
+async function registerFirefoxContentScript(options: Registration) {
+ await unregisterFirefoxContentScript(options.id);
+
+ if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
+ await chrome.scripting.registerContentScripts([{
+ id: options.id,
+ runAt: "document_start",
+ matches: options.matches,
+ allFrames: options.allFrames,
+ js: options.js,
+ css: options.css,
+ persistAcrossSessions: true,
+ }]);
+ } else {
+ chrome.contentScripts.register({
+ allFrames: options.allFrames,
+ js: options.js?.map?.(file => ({file})),
+ css: options.css?.map?.(file => ({file})),
+ matches: options.matches
+ }).then((registration) => void (contentScriptRegistrations[options.id] = registration));
+ }
+
}
/**
* Only works on Firefox.
* Firefox requires that this is handled by the background script
- *
*/
-function unregisterFirefoxContentScript(id: string) {
- if (contentScriptRegistrations[id]) {
- contentScriptRegistrations[id].unregister();
- delete contentScriptRegistrations[id];
+async function unregisterFirefoxContentScript(id: string) {
+ if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
+ // Bug in Firefox where you need to use browser namespace for this call
+ const getContentScripts = async (filter: browser.scripting.ContentScriptFilter) => {
+ if (isFirefoxOrSafari()) {
+ return await browser.scripting.getRegisteredContentScripts(filter);
+ } else {
+ return await chrome.scripting.getRegisteredContentScripts(filter);
+ }
+ };
+
+ const existingRegistrations = await getContentScripts({
+ ids: [id]
+ });
+ if (existingRegistrations?.length > 0) {
+ await chrome.scripting.unregisterContentScripts({
+ ids: existingRegistrations.map((script) => script.id),
+ });
+ }
+ } else {
+ if (contentScriptRegistrations[id]) {
+ contentScriptRegistrations[id].unregister();
+ delete contentScriptRegistrations[id];
+ }
}
}