mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 01:01:55 +01:00
Don't reregister contentscripts if not necessary
This commit is contained in:
parent
93d695e6c2
commit
6aeefaae64
2 changed files with 23 additions and 17 deletions
|
@ -153,6 +153,27 @@ chrome.runtime.onInstalled.addListener(function () {
|
|||
* @param {JSON} options
|
||||
*/
|
||||
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);
|
||||
|
||||
if ("scripting" in chrome && "getRegisteredContentScripts" in chrome.scripting) {
|
||||
|
@ -180,25 +201,11 @@ async function registerFirefoxContentScript(options: Registration) {
|
|||
* Only works on Firefox.
|
||||
* 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) {
|
||||
// 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({
|
||||
await chrome.scripting.unregisterContentScripts({
|
||||
ids: [id]
|
||||
});
|
||||
if (existingRegistrations?.length > 0) {
|
||||
await chrome.scripting.unregisterContentScripts({
|
||||
ids: existingRegistrations.map((script) => script.id),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (contentScriptRegistrations[id]) {
|
||||
contentScriptRegistrations[id].unregister();
|
||||
|
|
|
@ -54,7 +54,6 @@ export default class Utils {
|
|||
if (!isFirefoxOrSafari() || isSafari()) {
|
||||
permissions.push("webNavigation");
|
||||
}
|
||||
console.log(permissions)
|
||||
|
||||
chrome.permissions.request({
|
||||
origins: this.getPermissionRegex(),
|
||||
|
|
Loading…
Reference in a new issue