mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 13:07:05 +03:00
Don't reregister contentscripts if not necessary
This commit is contained in:
@@ -153,6 +153,27 @@ chrome.runtime.onInstalled.addListener(function () {
|
|||||||
* @param {JSON} options
|
* @param {JSON} options
|
||||||
*/
|
*/
|
||||||
async function registerFirefoxContentScript(options: Registration) {
|
async function registerFirefoxContentScript(options: Registration) {
|
||||||
|
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: [options.id]
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingRegistrations.length > 0
|
||||||
|
&& existingRegistrations[0].matches.every((match) => options.matches.includes(match))) {
|
||||||
|
// No need to register another script, already registered
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await unregisterFirefoxContentScript(options.id);
|
await unregisterFirefoxContentScript(options.id);
|
||||||
|
|
||||||
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
||||||
@@ -180,25 +201,11 @@ async function registerFirefoxContentScript(options: 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
|
||||||
*/
|
*/
|
||||||
async function unregisterFirefoxContentScript(id: string) {
|
async function unregisterFirefoxContentScript(id: string) {
|
||||||
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
||||||
// Bug in Firefox where you need to use browser namespace for this call
|
await chrome.scripting.unregisterContentScripts({
|
||||||
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]
|
ids: [id]
|
||||||
});
|
});
|
||||||
if (existingRegistrations?.length > 0) {
|
|
||||||
await chrome.scripting.unregisterContentScripts({
|
|
||||||
ids: existingRegistrations.map((script) => script.id),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (contentScriptRegistrations[id]) {
|
if (contentScriptRegistrations[id]) {
|
||||||
contentScriptRegistrations[id].unregister();
|
contentScriptRegistrations[id].unregister();
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ export default class Utils {
|
|||||||
if (!isFirefoxOrSafari() || isSafari()) {
|
if (!isFirefoxOrSafari() || isSafari()) {
|
||||||
permissions.push("webNavigation");
|
permissions.push("webNavigation");
|
||||||
}
|
}
|
||||||
console.log(permissions)
|
|
||||||
|
|
||||||
chrome.permissions.request({
|
chrome.permissions.request({
|
||||||
origins: this.getPermissionRegex(),
|
origins: this.getPermissionRegex(),
|
||||||
|
|||||||
Reference in New Issue
Block a user