mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-08 12:37:05 +03:00
Enable non persistent background page on Firefox
This commit is contained in:
@@ -3,5 +3,11 @@
|
|||||||
"gecko": {
|
"gecko": {
|
||||||
"id": "sponsorBlocker@ajay.app"
|
"id": "sponsorBlocker@ajay.app"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"background": {
|
||||||
|
"persistent": false
|
||||||
|
},
|
||||||
|
"permissions": [
|
||||||
|
"scripting"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"background": {
|
"background": {
|
||||||
"persistent": false
|
"persistent": false
|
||||||
}
|
},
|
||||||
|
"permissions": [
|
||||||
|
"scripting"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,28 +148,59 @@ chrome.runtime.onInstalled.addListener(function () {
|
|||||||
*
|
*
|
||||||
* @param {JSON} options
|
* @param {JSON} options
|
||||||
*/
|
*/
|
||||||
function registerFirefoxContentScript(options: Registration) {
|
async function registerFirefoxContentScript(options: Registration) {
|
||||||
const oldRegistration = contentScriptRegistrations[options.id];
|
await unregisterFirefoxContentScript(options.id);
|
||||||
if (oldRegistration) oldRegistration.unregister();
|
|
||||||
|
|
||||||
chrome.contentScripts.register({
|
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
||||||
|
await chrome.scripting.registerContentScripts([{
|
||||||
|
id: options.id,
|
||||||
|
runAt: "document_start",
|
||||||
|
matches: options.matches,
|
||||||
allFrames: options.allFrames,
|
allFrames: options.allFrames,
|
||||||
js: options.js,
|
js: options.js,
|
||||||
css: options.css,
|
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
|
matches: options.matches
|
||||||
}).then((registration) => void (contentScriptRegistrations[options.id] = registration));
|
}).then((registration) => void (contentScriptRegistrations[options.id] = registration));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only works on Firefox.
|
* Only works on Firefox.
|
||||||
* Firefox requires that this is handled by the background script
|
* Firefox requires that this is handled by the background script
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
function unregisterFirefoxContentScript(id: string) {
|
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]) {
|
if (contentScriptRegistrations[id]) {
|
||||||
contentScriptRegistrations[id].unregister();
|
contentScriptRegistrations[id].unregister();
|
||||||
delete contentScriptRegistrations[id];
|
delete contentScriptRegistrations[id];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submitVote(type: number, UUID: string, category: string) {
|
async function submitVote(type: number, UUID: string, category: string) {
|
||||||
|
|||||||
@@ -99,8 +99,8 @@ export interface Registration {
|
|||||||
message: string;
|
message: string;
|
||||||
id: string;
|
id: string;
|
||||||
allFrames: boolean;
|
allFrames: boolean;
|
||||||
js: browser.extensionTypes.ExtensionFileOrCode[];
|
js: string[];
|
||||||
css: browser.extensionTypes.ExtensionFileOrCode[];
|
css: string[];
|
||||||
matches: string[];
|
matches: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
src/utils.ts
24
src/utils.ts
@@ -47,9 +47,14 @@ export default class Utils {
|
|||||||
* @param {CallableFunction} callback
|
* @param {CallableFunction} callback
|
||||||
*/
|
*/
|
||||||
setupExtraSitePermissions(callback: (granted: boolean) => void): void {
|
setupExtraSitePermissions(callback: (granted: boolean) => void): void {
|
||||||
let permissions = ["webNavigation"];
|
const permissions = [];
|
||||||
if (!isSafari()) permissions.push("declarativeContent");
|
if (!isFirefoxOrSafari()) {
|
||||||
if (isFirefoxOrSafari() && !isSafari()) permissions = [];
|
permissions.push("declarativeContent");
|
||||||
|
}
|
||||||
|
if (!isFirefoxOrSafari() || isSafari()) {
|
||||||
|
permissions.push("webNavigation");
|
||||||
|
}
|
||||||
|
console.log(permissions)
|
||||||
|
|
||||||
chrome.permissions.request({
|
chrome.permissions.request({
|
||||||
origins: this.getPermissionRegex(),
|
origins: this.getPermissionRegex(),
|
||||||
@@ -73,21 +78,12 @@ export default class Utils {
|
|||||||
* For now, it is just SB.config.invidiousInstances.
|
* For now, it is just SB.config.invidiousInstances.
|
||||||
*/
|
*/
|
||||||
setupExtraSiteContentScripts(): void {
|
setupExtraSiteContentScripts(): void {
|
||||||
const firefoxJS = [];
|
|
||||||
for (const file of this.js) {
|
|
||||||
firefoxJS.push({file});
|
|
||||||
}
|
|
||||||
const firefoxCSS = [];
|
|
||||||
for (const file of this.css) {
|
|
||||||
firefoxCSS.push({file});
|
|
||||||
}
|
|
||||||
|
|
||||||
const registration: Registration = {
|
const registration: Registration = {
|
||||||
message: "registerContentScript",
|
message: "registerContentScript",
|
||||||
id: "invidious",
|
id: "invidious",
|
||||||
allFrames: true,
|
allFrames: true,
|
||||||
js: firefoxJS,
|
js: this.js,
|
||||||
css: firefoxCSS,
|
css: this.css,
|
||||||
matches: this.getPermissionRegex()
|
matches: this.getPermissionRegex()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user