[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).
This commit is contained in:
Raymond Hill 2023-06-05 09:15:59 -04:00
parent 666cbd143d
commit 6d9bef28ff
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
11 changed files with 297 additions and 352 deletions

View File

@ -34,7 +34,8 @@
"permissions": [ "permissions": [
"activeTab", "activeTab",
"declarativeNetRequest", "declarativeNetRequest",
"scripting" "scripting",
"storage"
], ],
"short_name": "uBO Lite", "short_name": "uBO Lite",
"version": "0.1", "version": "0.1",

View File

@ -29,6 +29,8 @@ import {
browser, browser,
dnr, dnr,
runtime, runtime,
localRead, localWrite,
sessionRead, sessionWrite,
} from './ext.js'; } from './ext.js';
import { import {
@ -75,72 +77,61 @@ function getCurrentVersion() {
} }
async function loadRulesetConfig() { async function loadRulesetConfig() {
const dynamicRuleMap = await getDynamicRules(); let data = await sessionRead('rulesetConfig');
const configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID); if ( data ) {
if ( configRule === undefined ) { rulesetConfig.version = data.version;
rulesetConfig.enabledRulesets = await defaultRulesetsFromLanguage(); rulesetConfig.enabledRulesets = data.enabledRulesets;
rulesetConfig.firstRun = true; rulesetConfig.autoReload = data.autoReload;
return; 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; let rawConfig;
try { try {
rawConfig = JSON.parse(self.atob(configRule.condition.urlFilter)); rawConfig = JSON.parse(self.atob(configRule.condition.urlFilter));
} catch(ex) { } catch(ex) {
}
// New format
if ( Array.isArray(rawConfig) ) {
rulesetConfig.version = rawConfig[0];
rulesetConfig.enabledRulesets = rawConfig[1];
rulesetConfig.autoReload = rawConfig[2];
return; return;
} }
if ( rawConfig === undefined ) { return; }
// Legacy format. TODO: remove when next new format is widely in use. const config = {
const match = /^\|\|(?:example|ubolite)\.invalid\/([^\/]+)\/(?:([^\/]+)\/)?/.exec( version: rawConfig[0],
configRule.condition.urlFilter enabledRulesets: rawConfig[1],
); autoReload: rawConfig[2],
if ( match === null ) { return; } };
rulesetConfig.version = match[1]; localWrite('rulesetConfig', config);
if ( match[2] ) { sessionWrite('rulesetConfig', config);
rulesetConfig.enabledRulesets = dnr.updateDynamicRules({
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 ],
removeRuleIds: [ CURRENT_CONFIG_BASE_RULE_ID ], 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, css: request.css,
origin: 'USER', origin: 'USER',
target: { tabId, frameIds: [ frameId ] }, target: { tabId, frameIds: [ frameId ] },
}).catch(reason => {
console.log(reason);
}); });
return; return;
} }

View File

@ -21,7 +21,6 @@
'use strict'; 'use strict';
import { simpleStorage } from './storage.js';
import { dom, qs$ } from './dom.js'; import { dom, qs$ } from './dom.js';
/******************************************************************************/ /******************************************************************************/
@ -82,9 +81,6 @@ const loadDashboardPanel = function(pane, first) {
dom.cl.add(tabButton, 'selected'); dom.cl.add(tabButton, 'selected');
tabButton.scrollIntoView(); tabButton.scrollIntoView();
document.querySelector('#iframe').contentWindow.location.replace(pane); document.querySelector('#iframe').contentWindow.location.replace(pane);
if ( pane !== 'no-dashboard.html' ) {
simpleStorage.setItem('dashboardLastVisitedPane', pane);
}
}; };
if ( first ) { if ( first ) {
return loadPane(); return loadPane();

View File

@ -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,
};

View File

@ -28,6 +28,8 @@
import { import {
browser, browser,
dnr, dnr,
localRead, localWrite,
sessionRead, sessionWrite,
} from './ext.js'; } from './ext.js';
import { import {
@ -78,40 +80,62 @@ const eqSets = (setBefore, setAfter) => {
/******************************************************************************/ /******************************************************************************/
// 0: no blocking => TRUSTED_DIRECTIVE_BASE_RULE_ID / requestDomains // 0: no blocking
// 1: network => BLOCKING_MODES_RULE_ID / excludedInitiatorDomains // 1: network
// 2: specific content => BLOCKING_MODES_RULE_ID / excludedRequestDomains // 2: specific content
// 3: generic content => BLOCKING_MODES_RULE_ID / initiatorDomains // 3: generic content
let filteringModeDetailsPromise; async function getActualFilteringModeDetails() {
if ( getActualFilteringModeDetails.cache ) {
function getActualFilteringModeDetails() { return getActualFilteringModeDetails.cache;
if ( filteringModeDetailsPromise !== undefined ) {
return filteringModeDetailsPromise;
} }
filteringModeDetailsPromise = Promise.all([ let details = await sessionRead('filteringModeDetails');
getDynamicRules(), if ( details === undefined ) {
getAllTrustedSiteDirectives(), details = await localRead('filteringModeDetails');
]).then(results => { if ( details === undefined ) {
const [ dynamicRuleMap, trustedSiteDirectives ] = results; details = await getActualFilteringModeDetails.convertLegacyStorage();
const details = { if ( details === undefined ) {
none: new Set(trustedSiteDirectives), details = {
}; network: [ 'all-urls' ],
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();
} }
return details; }
}); const out = {
return filteringModeDetailsPromise; 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() { async function getFilteringModeDetails() {
@ -127,87 +151,53 @@ async function getFilteringModeDetails() {
/******************************************************************************/ /******************************************************************************/
async function setFilteringModeDetails(afterDetails) { async function setFilteringModeDetails(afterDetails) {
const [ dynamicRuleMap, actualDetails ] = await Promise.all([ const actualDetails = await getActualFilteringModeDetails();
getDynamicRules(),
getActualFilteringModeDetails(),
]);
const addRules = [];
const removeRuleIds = [];
if ( eqSets(actualDetails.none, afterDetails.none) === false ) { 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) ) { if ( dynamicRuleMap.has(TRUSTED_DIRECTIVE_BASE_RULE_ID) ) {
removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID); removeRuleIds.push(TRUSTED_DIRECTIVE_BASE_RULE_ID);
dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID); dynamicRuleMap.delete(TRUSTED_DIRECTIVE_BASE_RULE_ID);
} }
const rule = { const addRules = [];
id: TRUSTED_DIRECTIVE_BASE_RULE_ID, if ( afterDetails.none.size !== 0 ) {
action: { type: 'allowAllRequests' }, const rule = {
condition: { id: TRUSTED_DIRECTIVE_BASE_RULE_ID,
resourceTypes: [ 'main_frame' ], action: { type: 'allowAllRequests' },
}, condition: {
priority: 100, resourceTypes: [ 'main_frame' ],
}; },
if ( actualDetails.none.size !== 0 ) { priority: 100,
};
if ( if (
actualDetails.none.size !== 1 || afterDetails.none.size !== 1 ||
actualDetails.none.has('all-urls') === false afterDetails.none.has('all-urls') === false
) { ) {
rule.condition.requestDomains = Array.from(actualDetails.none); rule.condition.requestDomains = Array.from(afterDetails.none);
} }
addRules.push(rule); addRules.push(rule);
dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID, rule); dynamicRuleMap.set(TRUSTED_DIRECTIVE_BASE_RULE_ID, rule);
} }
} if ( addRules.length !== 0 || removeRuleIds.length !== 0 ) {
if ( const updateOptions = {};
eqSets(actualDetails.network, afterDetails.network) === false || if ( addRules.length ) {
eqSets(actualDetails.extendedSpecific, afterDetails.extendedSpecific) === false || updateOptions.addRules = addRules;
eqSets(actualDetails.extendedGeneric, afterDetails.extendedGeneric) === false }
) { if ( removeRuleIds.length ) {
actualDetails.network = afterDetails.network; updateOptions.removeRuleIds = removeRuleIds;
actualDetails.extendedSpecific = afterDetails.extendedSpecific; }
actualDetails.extendedGeneric = afterDetails.extendedGeneric; await dnr.updateDynamicRules(updateOptions);
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 ) { return; } const data = {
const updateOptions = {}; none: Array.from(afterDetails.none),
if ( addRules.length ) { network: Array.from(afterDetails.network),
updateOptions.addRules = addRules; extendedSpecific: Array.from(afterDetails.extendedSpecific),
} extendedGeneric: Array.from(afterDetails.extendedGeneric),
if ( removeRuleIds.length ) { };
updateOptions.removeRuleIds = removeRuleIds; sessionWrite('filteringModeDetails', data);
} localWrite('filteringModeDetails', data);
return dnr.updateDynamicRules(updateOptions); 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 { export {
getFilteringMode, getFilteringMode,
setFilteringMode, setFilteringMode,
getDefaultFilteringMode, getDefaultFilteringMode,
setDefaultFilteringMode, setDefaultFilteringMode,
getFilteringModeDetails, getFilteringModeDetails,
getAllTrustedSiteDirectives,
syncWithBrowserPermissions, syncWithBrowserPermissions,
}; };

View File

@ -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 { dom, qs$ } from './dom.js';
import { i18n$ } from './i18n.js'; import { i18n$ } from './i18n.js';
import { simpleStorage } from './storage.js';
/******************************************************************************/ /******************************************************************************/
@ -242,11 +247,11 @@ async function toggleSections(more) {
} }
if ( newBits === currentBits ) { return; } if ( newBits === currentBits ) { return; }
sectionBitsToAttribute(newBits); sectionBitsToAttribute(newBits);
simpleStorage.setItem('popupPanelSections', newBits); localWrite('popupPanelSections', newBits);
} }
simpleStorage.getItem('popupPanelSections').then(s => { localRead('popupPanelSections').then(bits => {
sectionBitsToAttribute(parseInt(s, 10) || 0); sectionBitsToAttribute(bits || 0);
}); });
dom.on('#moreButton', 'click', ( ) => { dom.on('#moreButton', 'click', ( ) => {

View File

@ -63,13 +63,11 @@ function getRulesetDetails() {
/******************************************************************************/ /******************************************************************************/
let dynamicRuleMapPromise;
function getDynamicRules() { function getDynamicRules() {
if ( dynamicRuleMapPromise !== undefined ) { if ( getDynamicRules.dynamicRuleMapPromise !== undefined ) {
return dynamicRuleMapPromise; return getDynamicRules.dynamicRuleMapPromise;
} }
dynamicRuleMapPromise = dnr.getDynamicRules().then(rules => { getDynamicRules.dynamicRuleMapPromise = dnr.getDynamicRules().then(rules => {
const map = new Map( const map = new Map(
rules.map(rule => [ rule.id, rule ]) 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}`); ubolLog(`Available dynamic rule count: ${dnr.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES - map.size}`);
return map; return map;
}); });
return dynamicRuleMapPromise; return getDynamicRules.dynamicRuleMapPromise;
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -135,31 +135,28 @@ function registerGeneric(context, genericDetails) {
const registered = before.get('css-generic'); const registered = before.get('css-generic');
before.delete('css-generic'); // Important! before.delete('css-generic'); // Important!
const directive = {
id: 'css-generic',
js,
matches,
excludeMatches,
runAt: 'document_idle',
};
// register // register
if ( registered === undefined ) { if ( registered === undefined ) {
context.toAdd.push({ context.toAdd.push(directive);
id: 'css-generic',
js,
matches,
excludeMatches,
runAt: 'document_idle',
});
return; return;
} }
// update // update
const directive = { id: 'css-generic' }; if (
if ( arrayEq(registered.js, js, false) === false ) { arrayEq(registered.js, js, false) === false ||
directive.js = js; arrayEq(registered.matches, matches) === false ||
} arrayEq(registered.excludeMatches, excludeMatches) === false
if ( arrayEq(registered.matches, matches) === false ) { ) {
directive.matches = matches; context.toRemove.push('css-generic');
} context.toAdd.push(directive);
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
} }
} }
@ -196,32 +193,29 @@ function registerProcedural(context) {
const registered = before.get('css-procedural'); const registered = before.get('css-procedural');
before.delete('css-procedural'); // Important! before.delete('css-procedural'); // Important!
const directive = {
id: 'css-procedural',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_end',
};
// register // register
if ( registered === undefined ) { if ( registered === undefined ) {
context.toAdd.push({ context.toAdd.push(directive);
id: 'css-procedural',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_end',
});
return; return;
} }
// update // update
const directive = { id: 'css-procedural' }; if (
if ( arrayEq(registered.js, js, false) === false ) { arrayEq(registered.js, js, false) === false ||
directive.js = js; arrayEq(registered.matches, matches) === false ||
} arrayEq(registered.excludeMatches, excludeMatches) === false
if ( arrayEq(registered.matches, matches) === false ) { ) {
directive.matches = matches; context.toRemove.push('css-procedural');
} context.toAdd.push(directive);
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
} }
} }
@ -258,32 +252,29 @@ function registerDeclarative(context) {
const registered = before.get('css-declarative'); const registered = before.get('css-declarative');
before.delete('css-declarative'); // Important! before.delete('css-declarative'); // Important!
const directive = {
id: 'css-declarative',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
};
// register // register
if ( registered === undefined ) { if ( registered === undefined ) {
context.toAdd.push({ context.toAdd.push(directive);
id: 'css-declarative',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
});
return; return;
} }
// update // update
const directive = { id: 'css-declarative' }; if (
if ( arrayEq(registered.js, js, false) === false ) { arrayEq(registered.js, js, false) === false ||
directive.js = js; arrayEq(registered.matches, matches) === false ||
} arrayEq(registered.excludeMatches, excludeMatches) === false
if ( arrayEq(registered.matches, matches) === false ) { ) {
directive.matches = matches; context.toRemove.push('css-declarative');
} context.toAdd.push(directive);
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
} }
} }
@ -320,32 +311,29 @@ function registerSpecific(context) {
const registered = before.get('css-specific'); const registered = before.get('css-specific');
before.delete('css-specific'); // Important! before.delete('css-specific'); // Important!
const directive = {
id: 'css-specific',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
};
// register // register
if ( registered === undefined ) { if ( registered === undefined ) {
context.toAdd.push({ context.toAdd.push(directive);
id: 'css-specific',
js,
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
});
return; return;
} }
// update // update
const directive = { id: 'css-specific' }; if (
if ( arrayEq(registered.js, js, false) === false ) { arrayEq(registered.js, js, false) === false ||
directive.js = js; arrayEq(registered.matches, matches) === false ||
} arrayEq(registered.excludeMatches, excludeMatches) === false
if ( arrayEq(registered.matches, matches) === false ) { ) {
directive.matches = matches; context.toRemove.push('css-specific');
} context.toAdd.push(directive);
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
} }
} }
@ -398,30 +386,29 @@ function registerScriptlet(context, scriptletDetails) {
before.delete(id); // Important! before.delete(id); // Important!
const directive = {
id,
js: [ `/rulesets/scripting/scriptlet/${id}.js` ],
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
world: 'MAIN',
};
// register // register
if ( registered === undefined ) { if ( registered === undefined ) {
context.toAdd.push({ context.toAdd.push(directive);
id,
js: [ `/rulesets/scripting/scriptlet/${id}.js` ],
allFrames: true,
matches,
excludeMatches,
runAt: 'document_start',
world: 'MAIN',
});
continue; continue;
} }
// update // update
const directive = { id }; if (
if ( arrayEq(registered.matches, matches) === false ) { arrayEq(registered.matches, matches) === false ||
directive.matches = matches; arrayEq(registered.excludeMatches, excludeMatches) === false
} ) {
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) { context.toRemove.push(id);
directive.excludeMatches = excludeMatches; context.toAdd.push(directive);
}
if ( directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
} }
} }
} }
@ -452,14 +439,12 @@ async function registerInjectables(origins) {
entry => [ entry.id, entry ] entry => [ entry.id, entry ]
) )
); );
const toAdd = [], toUpdate = [], toRemove = []; const toAdd = [], toRemove = [];
const promises = [];
const context = { const context = {
filteringModeDetails, filteringModeDetails,
rulesetsDetails, rulesetsDetails,
before, before,
toAdd, toAdd,
toUpdate,
toRemove, toRemove,
}; };
@ -473,28 +458,17 @@ async function registerInjectables(origins) {
if ( toRemove.length !== 0 ) { if ( toRemove.length !== 0 ) {
ut.ubolLog(`Unregistered ${toRemove} content (css/js)`); ut.ubolLog(`Unregistered ${toRemove} content (css/js)`);
promises.push( await browser.scripting.unregisterContentScripts({ ids: toRemove })
browser.scripting.unregisterContentScripts({ ids: toRemove }) .catch(reason => { console.info(reason); });
.catch(reason => { console.info(reason); })
);
} }
if ( toAdd.length !== 0 ) { if ( toAdd.length !== 0 ) {
ut.ubolLog(`Registered ${toAdd.map(v => v.id)} content (css/js)`); ut.ubolLog(`Registered ${toAdd.map(v => v.id)} content (css/js)`);
promises.push( await browser.scripting.registerContentScripts(toAdd)
browser.scripting.registerContentScripts(toAdd) .catch(reason => { console.info(reason); });
.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;
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -21,10 +21,9 @@
'use strict'; 'use strict';
import { browser, sendMessage } from './ext.js'; import { browser, sendMessage, localRead, localWrite } from './ext.js';
import { i18n$ } from './i18n.js'; import { i18n$ } from './i18n.js';
import { dom, qs$, qsa$ } from './dom.js'; import { dom, qs$, qsa$ } from './dom.js';
import { simpleStorage } from './storage.js';
/******************************************************************************/ /******************************************************************************/
@ -352,10 +351,7 @@ function toggleHideUnusedLists(which) {
); );
} }
simpleStorage.setItem( localWrite('hideUnusedFilterLists', Array.from(hideUnusedSet));
'hideUnusedFilterLists',
Array.from(hideUnusedSet)
);
} }
dom.on('#lists', 'click', '.groupEntry[data-groupkey] > .geDetails', ev => { 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. // Initialize from saved state.
simpleStorage.getItem('hideUnusedFilterLists').then(value => { localRead('hideUnusedFilterLists').then(value => {
if ( Array.isArray(value) ) { if ( Array.isArray(value) === false ) { return; }
hideUnusedSet = new Set(value); hideUnusedSet = new Set(value);
}
}); });
/******************************************************************************/ /******************************************************************************/

View File

@ -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) {
}
}
}

View File

@ -41,7 +41,8 @@
"permissions": [ "permissions": [
"activeTab", "activeTab",
"declarativeNetRequest", "declarativeNetRequest",
"scripting" "scripting",
"storage"
], ],
"short_name": "uBO Lite", "short_name": "uBO Lite",
"version": "0.1", "version": "0.1",