mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-20 01:12:38 +01:00
[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:
parent
666cbd143d
commit
6d9bef28ff
@ -34,7 +34,8 @@
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"declarativeNetRequest",
|
||||
"scripting"
|
||||
"scripting",
|
||||
"storage"
|
||||
],
|
||||
"short_name": "uBO Lite",
|
||||
"version": "0.1",
|
||||
|
@ -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',
|
||||
],
|
||||
},
|
||||
if ( rawConfig === undefined ) { return; }
|
||||
const config = {
|
||||
version: rawConfig[0],
|
||||
enabledRulesets: rawConfig[1],
|
||||
autoReload: rawConfig[2],
|
||||
};
|
||||
}
|
||||
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 ],
|
||||
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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -28,6 +28,8 @@
|
||||
import {
|
||||
browser,
|
||||
dnr,
|
||||
localRead, localWrite,
|
||||
sessionRead, sessionWrite,
|
||||
} from './ext.js';
|
||||
|
||||
import {
|
||||
@ -78,39 +80,61 @@ 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),
|
||||
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' ],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
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 ) {
|
||||
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;
|
||||
if ( rule === undefined ) { return; }
|
||||
dnr.updateDynamicRules({
|
||||
removeRuleIds: [
|
||||
BLOCKING_MODES_RULE_ID,
|
||||
],
|
||||
});
|
||||
return filteringModeDetailsPromise;
|
||||
}
|
||||
const details = {
|
||||
none: trustedSiteDirectives || [],
|
||||
network: rule.condition.excludedInitiatorDomains || [],
|
||||
extendedSpecific: rule.condition.excludedRequestDomains || [],
|
||||
extendedGeneric: rule.condition.initiatorDomains || [],
|
||||
};
|
||||
sessionWrite('filteringModeDetails', details);
|
||||
localWrite('filteringModeDetails', details);
|
||||
return details;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
@ -127,18 +151,16 @@ 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 addRules = [];
|
||||
if ( afterDetails.none.size !== 0 ) {
|
||||
const rule = {
|
||||
id: TRUSTED_DIRECTIVE_BASE_RULE_ID,
|
||||
action: { type: 'allowAllRequests' },
|
||||
@ -147,59 +169,16 @@ async function setFilteringModeDetails(afterDetails) {
|
||||
},
|
||||
priority: 100,
|
||||
};
|
||||
if ( actualDetails.none.size !== 0 ) {
|
||||
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 ) { return; }
|
||||
if ( addRules.length !== 0 || removeRuleIds.length !== 0 ) {
|
||||
const updateOptions = {};
|
||||
if ( addRules.length ) {
|
||||
updateOptions.addRules = addRules;
|
||||
@ -207,7 +186,18 @@ async function setFilteringModeDetails(afterDetails) {
|
||||
if ( removeRuleIds.length ) {
|
||||
updateOptions.removeRuleIds = removeRuleIds;
|
||||
}
|
||||
return dnr.updateDynamicRules(updateOptions);
|
||||
await 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,
|
||||
};
|
||||
|
@ -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', ( ) => {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -135,31 +135,28 @@ function registerGeneric(context, genericDetails) {
|
||||
const registered = before.get('css-generic');
|
||||
before.delete('css-generic'); // Important!
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
context.toAdd.push({
|
||||
const directive = {
|
||||
id: 'css-generic',
|
||||
js,
|
||||
matches,
|
||||
excludeMatches,
|
||||
runAt: 'document_idle',
|
||||
});
|
||||
};
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
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!
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
context.toAdd.push({
|
||||
const directive = {
|
||||
id: 'css-procedural',
|
||||
js,
|
||||
allFrames: true,
|
||||
matches,
|
||||
excludeMatches,
|
||||
runAt: 'document_end',
|
||||
});
|
||||
};
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
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!
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
context.toAdd.push({
|
||||
const directive = {
|
||||
id: 'css-declarative',
|
||||
js,
|
||||
allFrames: true,
|
||||
matches,
|
||||
excludeMatches,
|
||||
runAt: 'document_start',
|
||||
});
|
||||
};
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
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!
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
context.toAdd.push({
|
||||
const directive = {
|
||||
id: 'css-specific',
|
||||
js,
|
||||
allFrames: true,
|
||||
matches,
|
||||
excludeMatches,
|
||||
runAt: 'document_start',
|
||||
});
|
||||
};
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
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,9 +386,7 @@ function registerScriptlet(context, scriptletDetails) {
|
||||
|
||||
before.delete(id); // Important!
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
context.toAdd.push({
|
||||
const directive = {
|
||||
id,
|
||||
js: [ `/rulesets/scripting/scriptlet/${id}.js` ],
|
||||
allFrames: true,
|
||||
@ -408,20 +394,21 @@ function registerScriptlet(context, scriptletDetails) {
|
||||
excludeMatches,
|
||||
runAt: 'document_start',
|
||||
world: 'MAIN',
|
||||
});
|
||||
};
|
||||
|
||||
// register
|
||||
if ( registered === undefined ) {
|
||||
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;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -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) ) {
|
||||
localRead('hideUnusedFilterLists').then(value => {
|
||||
if ( Array.isArray(value) === false ) { return; }
|
||||
hideUnusedSet = new Set(value);
|
||||
}
|
||||
});
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
@ -41,7 +41,8 @@
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"declarativeNetRequest",
|
||||
"scripting"
|
||||
"scripting",
|
||||
"storage"
|
||||
],
|
||||
"short_name": "uBO Lite",
|
||||
"version": "0.1",
|
||||
|
Loading…
Reference in New Issue
Block a user