From 6d9bef28ff6acd66b910d319f0dd9f451807eb77 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 5 Jun 2023 09:15:59 -0400 Subject: [PATCH] [mv3] Fix issue with updateContentScripts API and other fixes Avoid using updateContentScripts() as it suffers from an unexpected behavior, causing injected content scripts to lose proper order at injection time. The order in which content scripts are injected is key for uBOL content scripts. Potential out of order injection was causing cosmetic filtering to be broken. Use actual storage API to persist data across service worker wake-ups and browser launches. uBOL was trying to avoid using storage API, at the cost of somewhat hacky code (using DNR API to persist settings). Make use of session storage if available, to speed up initialization of waking up the service worker (which at this point is necessary to properly implement cosmetic filtering). --- platform/mv3/chromium/manifest.json | 3 +- platform/mv3/extension/js/background.js | 105 +++++---- platform/mv3/extension/js/dashboard.js | 4 - platform/mv3/extension/js/ext.js | 48 +++- platform/mv3/extension/js/mode-manager.js | 194 ++++++++-------- platform/mv3/extension/js/popup.js | 15 +- platform/mv3/extension/js/ruleset-manager.js | 10 +- .../mv3/extension/js/scripting-manager.js | 208 ++++++++---------- platform/mv3/extension/js/settings.js | 15 +- platform/mv3/extension/js/storage.js | 44 ---- platform/mv3/firefox/manifest.json | 3 +- 11 files changed, 297 insertions(+), 352 deletions(-) delete mode 100644 platform/mv3/extension/js/storage.js diff --git a/platform/mv3/chromium/manifest.json b/platform/mv3/chromium/manifest.json index 03722b3b2..9f1f109b5 100644 --- a/platform/mv3/chromium/manifest.json +++ b/platform/mv3/chromium/manifest.json @@ -34,7 +34,8 @@ "permissions": [ "activeTab", "declarativeNetRequest", - "scripting" + "scripting", + "storage" ], "short_name": "uBO Lite", "version": "0.1", diff --git a/platform/mv3/extension/js/background.js b/platform/mv3/extension/js/background.js index 272ba4956..9951a1c47 100644 --- a/platform/mv3/extension/js/background.js +++ b/platform/mv3/extension/js/background.js @@ -29,6 +29,8 @@ import { browser, dnr, runtime, + localRead, localWrite, + sessionRead, sessionWrite, } from './ext.js'; import { @@ -75,72 +77,61 @@ function getCurrentVersion() { } async function loadRulesetConfig() { - const dynamicRuleMap = await getDynamicRules(); - const configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID); - if ( configRule === undefined ) { - rulesetConfig.enabledRulesets = await defaultRulesetsFromLanguage(); - rulesetConfig.firstRun = true; + let data = await sessionRead('rulesetConfig'); + if ( data ) { + rulesetConfig.version = data.version; + rulesetConfig.enabledRulesets = data.enabledRulesets; + rulesetConfig.autoReload = data.autoReload; return; } + data = await localRead('rulesetConfig'); + if ( data ) { + rulesetConfig.version = data.version; + rulesetConfig.enabledRulesets = data.enabledRulesets; + rulesetConfig.autoReload = data.autoReload; + return; + } + data = await loadRulesetConfig.convertLegacyStorage(); + if ( data ) { + rulesetConfig.version = data.version; + rulesetConfig.enabledRulesets = data.enabledRulesets; + rulesetConfig.autoReload = data.autoReload; + return; + } + rulesetConfig.enabledRulesets = await defaultRulesetsFromLanguage(); + rulesetConfig.firstRun = true; + sessionWrite('rulesetConfig', rulesetConfig); + localWrite('rulesetConfig', rulesetConfig); +} + +// TODO: To remove after next stable release is widespread (2023-06-04) +loadRulesetConfig.convertLegacyStorage = async function() { + const dynamicRuleMap = await getDynamicRules(); + const configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID); + if ( configRule === undefined ) { return; } let rawConfig; try { rawConfig = JSON.parse(self.atob(configRule.condition.urlFilter)); } catch(ex) { - } - - // New format - if ( Array.isArray(rawConfig) ) { - rulesetConfig.version = rawConfig[0]; - rulesetConfig.enabledRulesets = rawConfig[1]; - rulesetConfig.autoReload = rawConfig[2]; return; } - - // Legacy format. TODO: remove when next new format is widely in use. - const match = /^\|\|(?:example|ubolite)\.invalid\/([^\/]+)\/(?:([^\/]+)\/)?/.exec( - configRule.condition.urlFilter - ); - if ( match === null ) { return; } - rulesetConfig.version = match[1]; - if ( match[2] ) { - rulesetConfig.enabledRulesets = - decodeURIComponent(match[2] || '').split(' '); - } -} - -async function saveRulesetConfig() { - const dynamicRuleMap = await getDynamicRules(); - let configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID); - if ( configRule === undefined ) { - configRule = { - id: CURRENT_CONFIG_BASE_RULE_ID, - action: { - type: 'allow', - }, - condition: { - urlFilter: '', - initiatorDomains: [ - 'ubolite.invalid', - ], - resourceTypes: [ - 'main_frame', - ], - }, - }; - } - const rawConfig = [ - rulesetConfig.version, - rulesetConfig.enabledRulesets, - rulesetConfig.autoReload, - ]; - const urlFilter = self.btoa(JSON.stringify(rawConfig)); - if ( urlFilter === configRule.condition.urlFilter ) { return; } - configRule.condition.urlFilter = urlFilter; - - return dnr.updateDynamicRules({ - addRules: [ configRule ], + if ( rawConfig === undefined ) { return; } + const config = { + version: rawConfig[0], + enabledRulesets: rawConfig[1], + autoReload: rawConfig[2], + }; + localWrite('rulesetConfig', config); + sessionWrite('rulesetConfig', config); + dnr.updateDynamicRules({ removeRuleIds: [ CURRENT_CONFIG_BASE_RULE_ID ], }); + return config; +}; + +async function saveRulesetConfig() { + sessionWrite('rulesetConfig', rulesetConfig); + return localWrite('rulesetConfig', rulesetConfig); } /******************************************************************************/ @@ -185,6 +176,8 @@ function onMessage(request, sender, callback) { css: request.css, origin: 'USER', target: { tabId, frameIds: [ frameId ] }, + }).catch(reason => { + console.log(reason); }); return; } diff --git a/platform/mv3/extension/js/dashboard.js b/platform/mv3/extension/js/dashboard.js index b3301160b..34edf9c05 100644 --- a/platform/mv3/extension/js/dashboard.js +++ b/platform/mv3/extension/js/dashboard.js @@ -21,7 +21,6 @@ 'use strict'; -import { simpleStorage } from './storage.js'; import { dom, qs$ } from './dom.js'; /******************************************************************************/ @@ -82,9 +81,6 @@ const loadDashboardPanel = function(pane, first) { dom.cl.add(tabButton, 'selected'); tabButton.scrollIntoView(); document.querySelector('#iframe').contentWindow.location.replace(pane); - if ( pane !== 'no-dashboard.html' ) { - simpleStorage.setItem('dashboardLastVisitedPane', pane); - } }; if ( first ) { return loadPane(); diff --git a/platform/mv3/extension/js/ext.js b/platform/mv3/extension/js/ext.js index 9a5aa3750..a7edca1b3 100644 --- a/platform/mv3/extension/js/ext.js +++ b/platform/mv3/extension/js/ext.js @@ -61,4 +61,50 @@ function sendMessage(msg) { /******************************************************************************/ -export { browser, dnr, i18n, runtime, sendMessage }; +async function localRead(key) { + if ( browser.storage instanceof Object === false ) { return; } + if ( browser.storage.local instanceof Object === false ) { return; } + try { + const bin = await browser.storage.local.get(key); + if ( bin instanceof Object === false ) { return; } + return bin[key]; + } catch(ex) { + } +} + +async function localWrite(key, value) { + if ( browser.storage instanceof Object === false ) { return; } + if ( browser.storage.local instanceof Object === false ) { return; } + return browser.storage.local.set({ [key]: value }); +} + +/******************************************************************************/ + +async function sessionRead(key) { + if ( browser.storage instanceof Object === false ) { return; } + if ( browser.storage.session instanceof Object === false ) { return; } + try { + const bin = await browser.storage.session.get(key); + if ( bin instanceof Object === false ) { return; } + return bin[key]; + } catch(ex) { + } +} + +async function sessionWrite(key, value) { + if ( browser.storage instanceof Object === false ) { return; } + if ( browser.storage.session instanceof Object === false ) { return; } + return browser.storage.session.set({ [key]: value }); +} + +/******************************************************************************/ + +export { + browser, + dnr, + i18n, + runtime, + sendMessage, + localRead, localWrite, + sessionRead, sessionWrite, +}; diff --git a/platform/mv3/extension/js/mode-manager.js b/platform/mv3/extension/js/mode-manager.js index f5692da9b..0109630c5 100644 --- a/platform/mv3/extension/js/mode-manager.js +++ b/platform/mv3/extension/js/mode-manager.js @@ -28,6 +28,8 @@ import { browser, dnr, + localRead, localWrite, + sessionRead, sessionWrite, } from './ext.js'; import { @@ -78,40 +80,62 @@ const eqSets = (setBefore, setAfter) => { /******************************************************************************/ -// 0: no blocking => TRUSTED_DIRECTIVE_BASE_RULE_ID / requestDomains -// 1: network => BLOCKING_MODES_RULE_ID / excludedInitiatorDomains -// 2: specific content => BLOCKING_MODES_RULE_ID / excludedRequestDomains -// 3: generic content => BLOCKING_MODES_RULE_ID / initiatorDomains +// 0: no blocking +// 1: network +// 2: specific content +// 3: generic content -let filteringModeDetailsPromise; - -function getActualFilteringModeDetails() { - if ( filteringModeDetailsPromise !== undefined ) { - return filteringModeDetailsPromise; +async function getActualFilteringModeDetails() { + if ( getActualFilteringModeDetails.cache ) { + return getActualFilteringModeDetails.cache; } - filteringModeDetailsPromise = Promise.all([ - getDynamicRules(), - getAllTrustedSiteDirectives(), - ]).then(results => { - const [ dynamicRuleMap, trustedSiteDirectives ] = results; - const details = { - none: new Set(trustedSiteDirectives), - }; - const rule = dynamicRuleMap.get(BLOCKING_MODES_RULE_ID); - if ( rule ) { - details.network = new Set(rule.condition.excludedInitiatorDomains); - details.extendedSpecific = new Set(rule.condition.excludedRequestDomains); - details.extendedGeneric = new Set(rule.condition.initiatorDomains); - } else { - details.network = new Set([ 'all-urls' ]); - details.extendedSpecific = new Set(); - details.extendedGeneric = new Set(); + let details = await sessionRead('filteringModeDetails'); + if ( details === undefined ) { + details = await localRead('filteringModeDetails'); + if ( details === undefined ) { + details = await getActualFilteringModeDetails.convertLegacyStorage(); + if ( details === undefined ) { + details = { + network: [ 'all-urls' ], + }; + } } - return details; - }); - return filteringModeDetailsPromise; + } + const out = { + none: new Set(details.none), + network: new Set(details.network), + extendedSpecific: new Set(details.extendedSpecific), + extendedGeneric: new Set(details.extendedGeneric), + }; + getActualFilteringModeDetails.cache = out; + return out; } +// TODO: To remove after next stable release is widespread (2023-06-04) +getActualFilteringModeDetails.convertLegacyStorage = async function() { + const dynamicRuleMap = await getDynamicRules(); + const trustedSiteDirectives = (( ) => { + const rule = dynamicRuleMap.get(TRUSTED_DIRECTIVE_BASE_RULE_ID); + return rule ? rule.condition.requestDomains : []; + })(); + const rule = dynamicRuleMap.get(BLOCKING_MODES_RULE_ID); + if ( rule === undefined ) { return; } + dnr.updateDynamicRules({ + removeRuleIds: [ + BLOCKING_MODES_RULE_ID, + ], + }); + const details = { + none: trustedSiteDirectives || [], + network: rule.condition.excludedInitiatorDomains || [], + extendedSpecific: rule.condition.excludedRequestDomains || [], + extendedGeneric: rule.condition.initiatorDomains || [], + }; + sessionWrite('filteringModeDetails', details); + localWrite('filteringModeDetails', details); + return details; +}; + /******************************************************************************/ async function getFilteringModeDetails() { @@ -127,87 +151,53 @@ async function getFilteringModeDetails() { /******************************************************************************/ async function setFilteringModeDetails(afterDetails) { - const [ dynamicRuleMap, actualDetails ] = await Promise.all([ - getDynamicRules(), - getActualFilteringModeDetails(), - ]); - const addRules = []; - const removeRuleIds = []; + const actualDetails = await getActualFilteringModeDetails(); if ( eqSets(actualDetails.none, afterDetails.none) === false ) { - actualDetails.none = afterDetails.none; + const dynamicRuleMap = await getDynamicRules(); + const removeRuleIds = []; if ( dynamicRuleMap.has(TRUSTED_DIRECTIVE_BASE_RULE_ID) ) { removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID); dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID); } - const rule = { - id: TRUSTED_DIRECTIVE_BASE_RULE_ID, - action: { type: 'allowAllRequests' }, - condition: { - resourceTypes: [ 'main_frame' ], - }, - priority: 100, - }; - if ( actualDetails.none.size !== 0 ) { + const addRules = []; + if ( afterDetails.none.size !== 0 ) { + const rule = { + id: TRUSTED_DIRECTIVE_BASE_RULE_ID, + action: { type: 'allowAllRequests' }, + condition: { + resourceTypes: [ 'main_frame' ], + }, + priority: 100, + }; if ( - actualDetails.none.size !== 1 || - actualDetails.none.has('all-urls') === false + afterDetails.none.size !== 1 || + afterDetails.none.has('all-urls') === false ) { - rule.condition.requestDomains = Array.from(actualDetails.none); + rule.condition.requestDomains = Array.from(afterDetails.none); } addRules.push(rule); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID, rule); } - } - if ( - eqSets(actualDetails.network, afterDetails.network) === false || - eqSets(actualDetails.extendedSpecific, afterDetails.extendedSpecific) === false || - eqSets(actualDetails.extendedGeneric, afterDetails.extendedGeneric) === false - ) { - actualDetails.network = afterDetails.network; - actualDetails.extendedSpecific = afterDetails.extendedSpecific; - actualDetails.extendedGeneric = afterDetails.extendedGeneric; - if ( dynamicRuleMap.has(BLOCKING_MODES_RULE_ID) ) { - removeRuleIds.push(BLOCKING_MODES_RULE_ID); - dynamicRuleMap.delete(BLOCKING_MODES_RULE_ID); - } - const rule = { - id: BLOCKING_MODES_RULE_ID, - action: { type: 'allow' }, - condition: { - resourceTypes: [ 'main_frame' ], - urlFilter: '||ubol-blocking-modes.invalid^', - }, - }; - if ( actualDetails.network.size ) { - rule.condition.excludedInitiatorDomains = - Array.from(actualDetails.network); - } - if ( actualDetails.extendedSpecific.size ) { - rule.condition.excludedRequestDomains = - Array.from(actualDetails.extendedSpecific); - } - if ( actualDetails.extendedGeneric.size ) { - rule.condition.initiatorDomains = - Array.from(actualDetails.extendedGeneric); - } - if ( - actualDetails.network.size || - actualDetails.extendedSpecific.size || - actualDetails.extendedGeneric.size - ) { - addRules.push(rule); - dynamicRuleMap.set(BLOCKING_MODES_RULE_ID, rule); + if ( addRules.length !== 0 || removeRuleIds.length !== 0 ) { + const updateOptions = {}; + if ( addRules.length ) { + updateOptions.addRules = addRules; + } + if ( removeRuleIds.length ) { + updateOptions.removeRuleIds = removeRuleIds; + } + await dnr.updateDynamicRules(updateOptions); } } - if ( addRules.length === 0 && removeRuleIds.length === 0 ) { return; } - const updateOptions = {}; - if ( addRules.length ) { - updateOptions.addRules = addRules; - } - if ( removeRuleIds.length ) { - updateOptions.removeRuleIds = removeRuleIds; - } - return dnr.updateDynamicRules(updateOptions); + const data = { + none: Array.from(afterDetails.none), + network: Array.from(afterDetails.network), + extendedSpecific: Array.from(afterDetails.extendedSpecific), + extendedGeneric: Array.from(afterDetails.extendedGeneric), + }; + sessionWrite('filteringModeDetails', data); + localWrite('filteringModeDetails', data); + getActualFilteringModeDetails.cache = undefined; } /******************************************************************************/ @@ -393,21 +383,11 @@ async function syncWithBrowserPermissions() { /******************************************************************************/ -async function getAllTrustedSiteDirectives() { - const dynamicRuleMap = await getDynamicRules(); - const rule = dynamicRuleMap.get(TRUSTED_DIRECTIVE_BASE_RULE_ID); - if ( rule === undefined ) { return []; } - return rule.condition.requestDomains; -} - -/******************************************************************************/ - export { getFilteringMode, setFilteringMode, getDefaultFilteringMode, setDefaultFilteringMode, getFilteringModeDetails, - getAllTrustedSiteDirectives, syncWithBrowserPermissions, }; diff --git a/platform/mv3/extension/js/popup.js b/platform/mv3/extension/js/popup.js index 72cf4ba5b..17a615f39 100644 --- a/platform/mv3/extension/js/popup.js +++ b/platform/mv3/extension/js/popup.js @@ -25,10 +25,15 @@ /******************************************************************************/ -import { browser, runtime, sendMessage } from './ext.js'; +import { + browser, + runtime, + sendMessage, + localRead, localWrite, +} from './ext.js'; + import { dom, qs$ } from './dom.js'; import { i18n$ } from './i18n.js'; -import { simpleStorage } from './storage.js'; /******************************************************************************/ @@ -242,11 +247,11 @@ async function toggleSections(more) { } if ( newBits === currentBits ) { return; } sectionBitsToAttribute(newBits); - simpleStorage.setItem('popupPanelSections', newBits); + localWrite('popupPanelSections', newBits); } -simpleStorage.getItem('popupPanelSections').then(s => { - sectionBitsToAttribute(parseInt(s, 10) || 0); +localRead('popupPanelSections').then(bits => { + sectionBitsToAttribute(bits || 0); }); dom.on('#moreButton', 'click', ( ) => { diff --git a/platform/mv3/extension/js/ruleset-manager.js b/platform/mv3/extension/js/ruleset-manager.js index 0888eee2a..f3266693d 100644 --- a/platform/mv3/extension/js/ruleset-manager.js +++ b/platform/mv3/extension/js/ruleset-manager.js @@ -63,13 +63,11 @@ function getRulesetDetails() { /******************************************************************************/ -let dynamicRuleMapPromise; - function getDynamicRules() { - if ( dynamicRuleMapPromise !== undefined ) { - return dynamicRuleMapPromise; + if ( getDynamicRules.dynamicRuleMapPromise !== undefined ) { + return getDynamicRules.dynamicRuleMapPromise; } - dynamicRuleMapPromise = dnr.getDynamicRules().then(rules => { + getDynamicRules.dynamicRuleMapPromise = dnr.getDynamicRules().then(rules => { const map = new Map( rules.map(rule => [ rule.id, rule ]) ); @@ -77,7 +75,7 @@ function getDynamicRules() { ubolLog(`Available dynamic rule count: ${dnr.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES - map.size}`); return map; }); - return dynamicRuleMapPromise; + return getDynamicRules.dynamicRuleMapPromise; } /******************************************************************************/ diff --git a/platform/mv3/extension/js/scripting-manager.js b/platform/mv3/extension/js/scripting-manager.js index f60bb077c..0897e7d9a 100644 --- a/platform/mv3/extension/js/scripting-manager.js +++ b/platform/mv3/extension/js/scripting-manager.js @@ -135,31 +135,28 @@ function registerGeneric(context, genericDetails) { const registered = before.get('css-generic'); before.delete('css-generic'); // Important! + const directive = { + id: 'css-generic', + js, + matches, + excludeMatches, + runAt: 'document_idle', + }; + // register if ( registered === undefined ) { - context.toAdd.push({ - id: 'css-generic', - js, - matches, - excludeMatches, - runAt: 'document_idle', - }); + context.toAdd.push(directive); return; } // update - const directive = { id: 'css-generic' }; - if ( arrayEq(registered.js, js, false) === false ) { - directive.js = js; - } - if ( arrayEq(registered.matches, matches) === false ) { - directive.matches = matches; - } - if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { - directive.excludeMatches = excludeMatches; - } - if ( directive.js || directive.matches || directive.excludeMatches ) { - context.toUpdate.push(directive); + if ( + arrayEq(registered.js, js, false) === false || + arrayEq(registered.matches, matches) === false || + arrayEq(registered.excludeMatches, excludeMatches) === false + ) { + context.toRemove.push('css-generic'); + context.toAdd.push(directive); } } @@ -196,32 +193,29 @@ function registerProcedural(context) { const registered = before.get('css-procedural'); before.delete('css-procedural'); // Important! + const directive = { + id: 'css-procedural', + js, + allFrames: true, + matches, + excludeMatches, + runAt: 'document_end', + }; + // register if ( registered === undefined ) { - context.toAdd.push({ - id: 'css-procedural', - js, - allFrames: true, - matches, - excludeMatches, - runAt: 'document_end', - }); + context.toAdd.push(directive); return; } // update - const directive = { id: 'css-procedural' }; - if ( arrayEq(registered.js, js, false) === false ) { - directive.js = js; - } - if ( arrayEq(registered.matches, matches) === false ) { - directive.matches = matches; - } - if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { - directive.excludeMatches = excludeMatches; - } - if ( directive.js || directive.matches || directive.excludeMatches ) { - context.toUpdate.push(directive); + if ( + arrayEq(registered.js, js, false) === false || + arrayEq(registered.matches, matches) === false || + arrayEq(registered.excludeMatches, excludeMatches) === false + ) { + context.toRemove.push('css-procedural'); + context.toAdd.push(directive); } } @@ -258,32 +252,29 @@ function registerDeclarative(context) { const registered = before.get('css-declarative'); before.delete('css-declarative'); // Important! + const directive = { + id: 'css-declarative', + js, + allFrames: true, + matches, + excludeMatches, + runAt: 'document_start', + }; + // register if ( registered === undefined ) { - context.toAdd.push({ - id: 'css-declarative', - js, - allFrames: true, - matches, - excludeMatches, - runAt: 'document_start', - }); + context.toAdd.push(directive); return; } // update - const directive = { id: 'css-declarative' }; - if ( arrayEq(registered.js, js, false) === false ) { - directive.js = js; - } - if ( arrayEq(registered.matches, matches) === false ) { - directive.matches = matches; - } - if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { - directive.excludeMatches = excludeMatches; - } - if ( directive.js || directive.matches || directive.excludeMatches ) { - context.toUpdate.push(directive); + if ( + arrayEq(registered.js, js, false) === false || + arrayEq(registered.matches, matches) === false || + arrayEq(registered.excludeMatches, excludeMatches) === false + ) { + context.toRemove.push('css-declarative'); + context.toAdd.push(directive); } } @@ -320,32 +311,29 @@ function registerSpecific(context) { const registered = before.get('css-specific'); before.delete('css-specific'); // Important! + const directive = { + id: 'css-specific', + js, + allFrames: true, + matches, + excludeMatches, + runAt: 'document_start', + }; + // register if ( registered === undefined ) { - context.toAdd.push({ - id: 'css-specific', - js, - allFrames: true, - matches, - excludeMatches, - runAt: 'document_start', - }); + context.toAdd.push(directive); return; } // update - const directive = { id: 'css-specific' }; - if ( arrayEq(registered.js, js, false) === false ) { - directive.js = js; - } - if ( arrayEq(registered.matches, matches) === false ) { - directive.matches = matches; - } - if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { - directive.excludeMatches = excludeMatches; - } - if ( directive.js || directive.matches || directive.excludeMatches ) { - context.toUpdate.push(directive); + if ( + arrayEq(registered.js, js, false) === false || + arrayEq(registered.matches, matches) === false || + arrayEq(registered.excludeMatches, excludeMatches) === false + ) { + context.toRemove.push('css-specific'); + context.toAdd.push(directive); } } @@ -398,30 +386,29 @@ function registerScriptlet(context, scriptletDetails) { before.delete(id); // Important! + const directive = { + id, + js: [ `/rulesets/scripting/scriptlet/${id}.js` ], + allFrames: true, + matches, + excludeMatches, + runAt: 'document_start', + world: 'MAIN', + }; + // register if ( registered === undefined ) { - context.toAdd.push({ - id, - js: [ `/rulesets/scripting/scriptlet/${id}.js` ], - allFrames: true, - matches, - excludeMatches, - runAt: 'document_start', - world: 'MAIN', - }); + context.toAdd.push(directive); continue; } // update - const directive = { id }; - if ( arrayEq(registered.matches, matches) === false ) { - directive.matches = matches; - } - if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { - directive.excludeMatches = excludeMatches; - } - if ( directive.matches || directive.excludeMatches ) { - context.toUpdate.push(directive); + if ( + arrayEq(registered.matches, matches) === false || + arrayEq(registered.excludeMatches, excludeMatches) === false + ) { + context.toRemove.push(id); + context.toAdd.push(directive); } } } @@ -452,14 +439,12 @@ async function registerInjectables(origins) { entry => [ entry.id, entry ] ) ); - const toAdd = [], toUpdate = [], toRemove = []; - const promises = []; + const toAdd = [], toRemove = []; const context = { filteringModeDetails, rulesetsDetails, before, toAdd, - toUpdate, toRemove, }; @@ -473,28 +458,17 @@ async function registerInjectables(origins) { if ( toRemove.length !== 0 ) { ut.ubolLog(`Unregistered ${toRemove} content (css/js)`); - promises.push( - browser.scripting.unregisterContentScripts({ ids: toRemove }) - .catch(reason => { console.info(reason); }) - ); + await browser.scripting.unregisterContentScripts({ ids: toRemove }) + .catch(reason => { console.info(reason); }); } + if ( toAdd.length !== 0 ) { ut.ubolLog(`Registered ${toAdd.map(v => v.id)} content (css/js)`); - promises.push( - browser.scripting.registerContentScripts(toAdd) - .catch(reason => { console.info(reason); }) - ); + await browser.scripting.registerContentScripts(toAdd) + .catch(reason => { console.info(reason); }); } - if ( toUpdate.length !== 0 ) { - ut.ubolLog(`Updated ${toUpdate.map(v => v.id)} content (css/js)`); - promises.push( - browser.scripting.updateContentScripts(toUpdate) - .catch(reason => { console.info(reason); }) - ); - } - if ( promises.length === 0 ) { return; } - return Promise.all(promises); + return true; } /******************************************************************************/ diff --git a/platform/mv3/extension/js/settings.js b/platform/mv3/extension/js/settings.js index 5008253d3..c253bd554 100644 --- a/platform/mv3/extension/js/settings.js +++ b/platform/mv3/extension/js/settings.js @@ -21,10 +21,9 @@ 'use strict'; -import { browser, sendMessage } from './ext.js'; +import { browser, sendMessage, localRead, localWrite } from './ext.js'; import { i18n$ } from './i18n.js'; import { dom, qs$, qsa$ } from './dom.js'; -import { simpleStorage } from './storage.js'; /******************************************************************************/ @@ -352,10 +351,7 @@ function toggleHideUnusedLists(which) { ); } - simpleStorage.setItem( - 'hideUnusedFilterLists', - Array.from(hideUnusedSet) - ); + localWrite('hideUnusedFilterLists', Array.from(hideUnusedSet)); } dom.on('#lists', 'click', '.groupEntry[data-groupkey] > .geDetails', ev => { @@ -365,10 +361,9 @@ dom.on('#lists', 'click', '.groupEntry[data-groupkey] > .geDetails', ev => { }); // Initialize from saved state. -simpleStorage.getItem('hideUnusedFilterLists').then(value => { - if ( Array.isArray(value) ) { - hideUnusedSet = new Set(value); - } +localRead('hideUnusedFilterLists').then(value => { + if ( Array.isArray(value) === false ) { return; } + hideUnusedSet = new Set(value); }); /******************************************************************************/ diff --git a/platform/mv3/extension/js/storage.js b/platform/mv3/extension/js/storage.js deleted file mode 100644 index 221a91ac3..000000000 --- a/platform/mv3/extension/js/storage.js +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* - - uBlock Origin - a browser extension to block requests. - Copyright (C) 2022-present Raymond Hill - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see {http://www.gnu.org/licenses/}. - - Home: https://github.com/gorhill/uBlock -*/ - -/* jshint esversion:11 */ - -'use strict'; - -/******************************************************************************/ - -export class simpleStorage { - static getItem(k) { - try { - return Promise.resolve(JSON.parse(self.localStorage.getItem(k))); - } - catch(ex) { - } - return Promise.resolve(null); - } - static setItem(k, v) { - try { - self.localStorage.setItem(k, JSON.stringify(v)); - } - catch(ex) { - } - } -} diff --git a/platform/mv3/firefox/manifest.json b/platform/mv3/firefox/manifest.json index f611906c7..a675928c9 100644 --- a/platform/mv3/firefox/manifest.json +++ b/platform/mv3/firefox/manifest.json @@ -41,7 +41,8 @@ "permissions": [ "activeTab", "declarativeNetRequest", - "scripting" + "scripting", + "storage" ], "short_name": "uBO Lite", "version": "0.1",