2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-10-30 18:06:23 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-07-24 15:17:18 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2021-08-23 15:42:27 +02:00
|
|
|
/* globals WebAssembly */
|
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-23 15:42:27 +02:00
|
|
|
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
|
2021-08-22 18:03:59 +02:00
|
|
|
import punycode from '../lib/punycode.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import cosmeticFilteringEngine from './cosmetic-filtering.js';
|
|
|
|
import io from './assets.js';
|
|
|
|
import logger from './logger.js';
|
|
|
|
import lz4Codec from './lz4.js';
|
|
|
|
import staticExtFilteringEngine from './static-ext-filtering.js';
|
|
|
|
import staticFilteringReverseLookup from './reverselookup.js';
|
|
|
|
import staticNetFilteringEngine from './static-net-filtering.js';
|
|
|
|
import µb from './background.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
import { hostnameFromURI } from './uri-utils.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
import { redirectEngine } from './redirect-engine.js';
|
|
|
|
import { sparseBase64 } from './base64-custom.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
import { StaticFilteringParser } from './static-filtering-parser.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
import { ubolog, ubologSet } from './console.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
2021-08-03 18:19:25 +02:00
|
|
|
import {
|
|
|
|
permanentFirewall,
|
|
|
|
permanentSwitches,
|
|
|
|
permanentURLFiltering,
|
|
|
|
} from './filtering-engines.js';
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
import {
|
|
|
|
CompiledListReader,
|
|
|
|
CompiledListWriter,
|
|
|
|
} from './static-filtering-io.js';
|
|
|
|
|
2021-07-31 14:38:33 +02:00
|
|
|
import {
|
|
|
|
LineIterator,
|
|
|
|
orphanizeString,
|
|
|
|
} from './text-utils.js';
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.getBytesInUse = async function() {
|
2019-09-15 13:58:28 +02:00
|
|
|
const promises = [];
|
2018-08-11 16:39:43 +02:00
|
|
|
let bytesInUse;
|
|
|
|
|
2016-10-30 18:06:23 +01:00
|
|
|
// Not all platforms implement this method.
|
2019-09-15 13:58:28 +02:00
|
|
|
promises.push(
|
|
|
|
vAPI.storage.getBytesInUse instanceof Function
|
|
|
|
? vAPI.storage.getBytesInUse(null)
|
|
|
|
: undefined
|
|
|
|
);
|
|
|
|
|
2019-03-23 02:09:27 +01:00
|
|
|
if (
|
|
|
|
navigator.storage instanceof Object &&
|
|
|
|
navigator.storage.estimate instanceof Function
|
|
|
|
) {
|
2019-09-15 13:58:28 +02:00
|
|
|
promises.push(navigator.storage.estimate());
|
2018-08-11 16:39:43 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
|
|
|
|
const results = await Promise.all(promises);
|
|
|
|
|
|
|
|
const processCount = count => {
|
|
|
|
if ( typeof count !== 'number' ) { return; }
|
|
|
|
if ( bytesInUse === undefined ) { bytesInUse = 0; }
|
|
|
|
bytesInUse += count;
|
|
|
|
return bytesInUse;
|
|
|
|
};
|
|
|
|
|
|
|
|
processCount(results[0]);
|
|
|
|
if ( results.length > 1 && results[1] instanceof Object ) {
|
|
|
|
processCount(results[1].usage);
|
2016-10-30 18:06:23 +01:00
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.storageUsed = bytesInUse;
|
2019-09-15 13:58:28 +02:00
|
|
|
return bytesInUse;
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveLocalSettings = (( ) => {
|
2019-05-15 20:49:12 +02:00
|
|
|
const saveAfter = 4 * 60 * 1000;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-05-15 20:49:12 +02:00
|
|
|
const onTimeout = ( ) => {
|
2016-10-08 16:15:31 +02:00
|
|
|
if ( µb.localSettingsLastModified > µb.localSettingsLastSaved ) {
|
2018-07-24 15:17:18 +02:00
|
|
|
µb.saveLocalSettings();
|
2015-11-29 23:06:58 +01:00
|
|
|
}
|
|
|
|
vAPI.setTimeout(onTimeout, saveAfter);
|
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
vAPI.setTimeout(onTimeout, saveAfter);
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
return function() {
|
2018-07-26 00:24:14 +02:00
|
|
|
this.localSettingsLastSaved = Date.now();
|
2019-09-15 13:58:28 +02:00
|
|
|
return vAPI.storage.set(this.localSettings);
|
2018-07-26 00:24:14 +02:00
|
|
|
};
|
2015-11-29 23:06:58 +01:00
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadUserSettings = async function() {
|
2021-01-16 16:35:56 +01:00
|
|
|
const usDefault = this.userSettingsDefault;
|
|
|
|
|
|
|
|
const results = await Promise.all([
|
|
|
|
vAPI.storage.get(Object.assign(usDefault)),
|
|
|
|
vAPI.adminStorage.get('userSettings'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const usUser = results[0] instanceof Object && results[0] ||
|
|
|
|
Object.assign(usDefault);
|
|
|
|
|
|
|
|
if ( Array.isArray(results[1]) ) {
|
|
|
|
const adminSettings = results[1];
|
|
|
|
for ( const entry of adminSettings ) {
|
|
|
|
if ( entry.length < 1 ) { continue; }
|
|
|
|
const name = entry[0];
|
|
|
|
if ( usDefault.hasOwnProperty(name) === false ) { continue; }
|
|
|
|
const value = entry.length < 2
|
|
|
|
? usDefault[name]
|
|
|
|
: this.settingValueFromString(usDefault, name, entry[1]);
|
|
|
|
if ( value === undefined ) { continue; }
|
|
|
|
usUser[name] = usDefault[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usUser;
|
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveUserSettings = function() {
|
2021-01-31 16:30:12 +01:00
|
|
|
// `externalLists` will be deprecated in some future, it is kept around
|
|
|
|
// for forward compatibility purpose, and should reflect the content of
|
|
|
|
// `importedLists`.
|
2021-11-08 18:49:03 +01:00
|
|
|
//
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1803
|
|
|
|
// Do this before computing modified settings.
|
2021-01-31 16:30:12 +01:00
|
|
|
this.userSettings.externalLists =
|
|
|
|
this.userSettings.importedLists.join('\n');
|
|
|
|
|
2021-11-08 18:49:03 +01:00
|
|
|
const toSave = this.getModifiedSettings(
|
|
|
|
this.userSettings,
|
|
|
|
this.userSettingsDefault
|
|
|
|
);
|
|
|
|
|
2021-01-16 16:35:56 +01:00
|
|
|
const toRemove = [];
|
|
|
|
for ( const key in this.userSettings ) {
|
|
|
|
if ( this.userSettings.hasOwnProperty(key) === false ) { continue; }
|
2021-01-31 16:30:12 +01:00
|
|
|
if ( toSave.hasOwnProperty(key) ) { continue; }
|
2021-01-16 16:35:56 +01:00
|
|
|
toRemove.push(key);
|
|
|
|
}
|
|
|
|
if ( toRemove.length !== 0 ) {
|
|
|
|
vAPI.storage.remove(toRemove);
|
|
|
|
}
|
|
|
|
vAPI.storage.set(toSave);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-01-05 18:16:50 +01:00
|
|
|
// Admin hidden settings have precedence over user hidden settings.
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadHiddenSettings = async function() {
|
2021-01-05 18:16:50 +01:00
|
|
|
const hsDefault = this.hiddenSettingsDefault;
|
|
|
|
const hsAdmin = this.hiddenSettingsAdmin;
|
|
|
|
const hsUser = this.hiddenSettings;
|
2019-02-17 21:40:09 +01:00
|
|
|
|
2021-01-05 18:16:50 +01:00
|
|
|
const results = await Promise.all([
|
2021-01-06 17:39:24 +01:00
|
|
|
vAPI.adminStorage.get([
|
|
|
|
'advancedSettings',
|
|
|
|
'disableDashboard',
|
|
|
|
'disabledPopupPanelParts',
|
|
|
|
]),
|
2021-01-05 18:16:50 +01:00
|
|
|
vAPI.storage.get('hiddenSettings'),
|
|
|
|
]);
|
|
|
|
|
2021-01-06 17:39:24 +01:00
|
|
|
if ( results[0] instanceof Object ) {
|
|
|
|
const {
|
|
|
|
advancedSettings,
|
|
|
|
disableDashboard,
|
|
|
|
disabledPopupPanelParts
|
|
|
|
} = results[0];
|
|
|
|
if ( Array.isArray(advancedSettings) ) {
|
|
|
|
for ( const entry of advancedSettings ) {
|
|
|
|
if ( entry.length < 1 ) { continue; }
|
|
|
|
const name = entry[0];
|
|
|
|
if ( hsDefault.hasOwnProperty(name) === false ) { continue; }
|
|
|
|
const value = entry.length < 2
|
|
|
|
? hsDefault[name]
|
|
|
|
: this.hiddenSettingValueFromString(name, entry[1]);
|
|
|
|
if ( value === undefined ) { continue; }
|
|
|
|
hsDefault[name] = hsAdmin[name] = hsUser[name] = value;
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.noDashboard = disableDashboard === true;
|
2021-01-06 17:39:24 +01:00
|
|
|
if ( Array.isArray(disabledPopupPanelParts) ) {
|
|
|
|
const partNameToBit = new Map([
|
2021-01-07 14:19:47 +01:00
|
|
|
[ 'globalStats', 0b00010 ],
|
|
|
|
[ 'basicTools', 0b00100 ],
|
|
|
|
[ 'extraTools', 0b01000 ],
|
|
|
|
[ 'overviewPane', 0b10000 ],
|
2021-01-06 17:39:24 +01:00
|
|
|
]);
|
|
|
|
let bits = hsDefault.popupPanelDisabledSections;
|
|
|
|
for ( const part of disabledPopupPanelParts ) {
|
|
|
|
const bit = partNameToBit.get(part);
|
|
|
|
if ( bit === undefined ) { continue; }
|
|
|
|
bits |= bit;
|
|
|
|
}
|
|
|
|
hsDefault.popupPanelDisabledSections =
|
|
|
|
hsAdmin.popupPanelDisabledSections =
|
|
|
|
hsUser.popupPanelDisabledSections = bits;
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 18:16:50 +01:00
|
|
|
|
|
|
|
const hs = results[1] instanceof Object && results[1].hiddenSettings || {};
|
|
|
|
if ( Object.keys(hsAdmin).length === 0 && Object.keys(hs).length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const key in hsDefault ) {
|
|
|
|
if ( hsDefault.hasOwnProperty(key) === false ) { continue; }
|
|
|
|
if ( hsAdmin.hasOwnProperty(name) ) { continue; }
|
|
|
|
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
|
|
|
|
this.hiddenSettings[key] = hs[key];
|
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
this.fireDOMEvent('hiddenSettingsChanged');
|
2018-02-21 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
2018-04-09 23:46:29 +02:00
|
|
|
// Note: Save only the settings which values differ from the default ones.
|
2021-01-16 16:35:56 +01:00
|
|
|
// This way the new default values in the future will properly apply for
|
|
|
|
// those which were not modified by the user.
|
2020-10-03 18:34:21 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveHiddenSettings = function() {
|
2021-01-16 16:35:56 +01:00
|
|
|
vAPI.storage.set({
|
|
|
|
hiddenSettings: this.getModifiedSettings(
|
|
|
|
this.hiddenSettings,
|
|
|
|
this.hiddenSettingsDefault
|
|
|
|
)
|
|
|
|
});
|
2018-02-21 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
2019-11-19 18:05:33 +01:00
|
|
|
self.addEventListener('hiddenSettingsChanged', ( ) => {
|
2021-07-29 01:48:38 +02:00
|
|
|
const µbhs = µb.hiddenSettings;
|
|
|
|
ubologSet(µbhs.consoleLogLevel === 'info');
|
2019-11-19 18:05:33 +01:00
|
|
|
vAPI.net.setOptions({
|
2020-03-23 18:31:43 +01:00
|
|
|
cnameIgnoreList: µbhs.cnameIgnoreList,
|
|
|
|
cnameIgnore1stParty: µbhs.cnameIgnore1stParty,
|
|
|
|
cnameIgnoreExceptions: µbhs.cnameIgnoreExceptions,
|
|
|
|
cnameIgnoreRootDocument: µbhs.cnameIgnoreRootDocument,
|
|
|
|
cnameMaxTTL: µbhs.cnameMaxTTL,
|
|
|
|
cnameReplayFullURL: µbhs.cnameReplayFullURL,
|
|
|
|
cnameUncloakProxied: µbhs.cnameUncloakProxied,
|
2019-11-19 18:05:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-21 19:29:36 +01:00
|
|
|
/******************************************************************************/
|
2016-11-03 16:20:47 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.hiddenSettingsFromString = function(raw) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const out = Object.assign({}, this.hiddenSettingsDefault);
|
2021-07-25 16:55:35 +02:00
|
|
|
const lineIter = new LineIterator(raw);
|
2016-11-03 16:20:47 +01:00
|
|
|
while ( lineIter.eot() === false ) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const line = lineIter.next();
|
|
|
|
const matches = /^\s*(\S+)\s+(.+)$/.exec(line);
|
2016-11-03 16:20:47 +01:00
|
|
|
if ( matches === null || matches.length !== 3 ) { continue; }
|
2019-05-15 20:49:12 +02:00
|
|
|
const name = matches[1];
|
2016-11-03 16:20:47 +01:00
|
|
|
if ( out.hasOwnProperty(name) === false ) { continue; }
|
2021-01-05 18:16:50 +01:00
|
|
|
if ( this.hiddenSettingsAdmin.hasOwnProperty(name) ) { continue; }
|
|
|
|
const value = this.hiddenSettingValueFromString(name, matches[2]);
|
|
|
|
if ( value !== undefined ) {
|
|
|
|
out[name] = value;
|
2016-11-03 16:20:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-21 19:29:36 +01:00
|
|
|
return out;
|
2016-11-03 16:20:47 +01:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.hiddenSettingValueFromString = function(name, value) {
|
2021-01-05 18:16:50 +01:00
|
|
|
if ( typeof name !== 'string' || typeof value !== 'string' ) { return; }
|
|
|
|
const hsDefault = this.hiddenSettingsDefault;
|
|
|
|
if ( hsDefault.hasOwnProperty(name) === false ) { return; }
|
|
|
|
let r;
|
|
|
|
switch ( typeof hsDefault[name] ) {
|
|
|
|
case 'boolean':
|
|
|
|
if ( value === 'true' ) {
|
|
|
|
r = true;
|
|
|
|
} else if ( value === 'false' ) {
|
|
|
|
r = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
r = value.trim();
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
if ( value.startsWith('0b') ) {
|
|
|
|
r = parseInt(value.slice(2), 2);
|
|
|
|
} else if ( value.startsWith('0x') ) {
|
|
|
|
r = parseInt(value.slice(2), 16);
|
|
|
|
} else {
|
|
|
|
r = parseInt(value, 10);
|
|
|
|
}
|
|
|
|
if ( isNaN(r) ) { r = undefined; }
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.stringFromHiddenSettings = function() {
|
2019-05-15 20:49:12 +02:00
|
|
|
const out = [];
|
|
|
|
for ( const key of Object.keys(this.hiddenSettings).sort() ) {
|
2016-11-03 16:20:47 +01:00
|
|
|
out.push(key + ' ' + this.hiddenSettings[key]);
|
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.savePermanentFirewallRules = function() {
|
2018-09-03 20:06:49 +02:00
|
|
|
vAPI.storage.set({
|
2021-07-29 01:48:38 +02:00
|
|
|
dynamicFilteringString: permanentFirewall.toString()
|
2018-09-03 20:06:49 +02:00
|
|
|
});
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.savePermanentURLFilteringRules = function() {
|
2018-09-03 20:06:49 +02:00
|
|
|
vAPI.storage.set({
|
2021-07-29 01:48:38 +02:00
|
|
|
urlFilteringString: permanentURLFiltering.toString()
|
2018-09-03 20:06:49 +02:00
|
|
|
});
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveHostnameSwitches = function() {
|
2018-09-03 20:06:49 +02:00
|
|
|
vAPI.storage.set({
|
2021-07-29 01:48:38 +02:00
|
|
|
hostnameSwitchesString: permanentSwitches.toString()
|
2018-09-03 20:06:49 +02:00
|
|
|
});
|
2014-12-31 23:26:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveWhitelist = function() {
|
2018-09-03 20:06:49 +02:00
|
|
|
vAPI.storage.set({
|
2019-06-25 17:57:14 +02:00
|
|
|
netWhitelist: this.arrayFromWhitelist(this.netWhitelist)
|
2018-09-03 20:06:49 +02:00
|
|
|
});
|
2014-08-25 02:52:34 +02:00
|
|
|
this.netWhitelistModifyTime = Date.now();
|
|
|
|
};
|
|
|
|
|
2021-07-17 18:03:56 +02:00
|
|
|
/******************************************************************************/
|
2015-03-30 14:12:41 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadSelectedFilterLists = async function() {
|
2019-09-15 13:58:28 +02:00
|
|
|
const bin = await vAPI.storage.get('selectedFilterLists');
|
|
|
|
if ( bin instanceof Object && Array.isArray(bin.selectedFilterLists) ) {
|
2019-02-17 21:40:09 +01:00
|
|
|
this.selectedFilterLists = bin.selectedFilterLists;
|
2019-09-15 13:58:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
|
2019-09-16 22:17:48 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/747
|
|
|
|
// Select default filter lists if first-time launch.
|
2021-07-29 01:48:38 +02:00
|
|
|
const lists = await io.metadata();
|
2019-09-15 13:58:28 +02:00
|
|
|
this.saveSelectedFilterLists(this.autoSelectRegionalFilterLists(lists));
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
2015-03-30 14:12:41 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveSelectedFilterLists = function(newKeys, append = false) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const oldKeys = this.selectedFilterLists.slice();
|
2017-01-26 16:17:38 +01:00
|
|
|
if ( append ) {
|
|
|
|
newKeys = newKeys.concat(oldKeys);
|
|
|
|
}
|
2019-05-15 20:49:12 +02:00
|
|
|
const newSet = new Set(newKeys);
|
2017-01-26 16:17:38 +01:00
|
|
|
// Purge unused filter lists from cache.
|
2019-09-17 13:44:19 +02:00
|
|
|
for ( const oldKey of oldKeys ) {
|
|
|
|
if ( newSet.has(oldKey) === false ) {
|
|
|
|
this.removeFilterList(oldKey);
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
2017-01-26 16:17:38 +01:00
|
|
|
}
|
2018-06-01 13:54:31 +02:00
|
|
|
newKeys = Array.from(newSet);
|
2017-01-26 16:17:38 +01:00
|
|
|
this.selectedFilterLists = newKeys;
|
2019-09-15 15:36:50 +02:00
|
|
|
return vAPI.storage.set({ selectedFilterLists: newKeys });
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
|
2015-03-30 14:12:41 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.applyFilterListSelection = function(details) {
|
2019-05-15 20:49:12 +02:00
|
|
|
let selectedListKeySet = new Set(this.selectedFilterLists);
|
2021-01-31 16:30:12 +01:00
|
|
|
let importedLists = this.userSettings.importedLists.slice();
|
2017-01-22 22:05:16 +01:00
|
|
|
|
|
|
|
// Filter lists to select
|
|
|
|
if ( Array.isArray(details.toSelect) ) {
|
|
|
|
if ( details.merge ) {
|
2019-05-15 20:49:12 +02:00
|
|
|
for ( let i = 0, n = details.toSelect.length; i < n; i++ ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
selectedListKeySet.add(details.toSelect[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedListKeySet = new Set(details.toSelect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imported filter lists to remove
|
|
|
|
if ( Array.isArray(details.toRemove) ) {
|
2019-05-15 20:49:12 +02:00
|
|
|
for ( let i = 0, n = details.toRemove.length; i < n; i++ ) {
|
|
|
|
const assetKey = details.toRemove[i];
|
2017-01-22 22:05:16 +01:00
|
|
|
selectedListKeySet.delete(assetKey);
|
2021-01-31 16:30:12 +01:00
|
|
|
const pos = importedLists.indexOf(assetKey);
|
2021-01-08 15:18:26 +01:00
|
|
|
if ( pos !== -1 ) {
|
2021-01-31 16:30:12 +01:00
|
|
|
importedLists.splice(pos, 1);
|
2021-01-08 15:18:26 +01:00
|
|
|
}
|
2017-01-22 22:05:16 +01:00
|
|
|
this.removeFilterList(assetKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter lists to import
|
|
|
|
if ( typeof details.toImport === 'string' ) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1181
|
2019-05-15 20:49:12 +02:00
|
|
|
// Try mapping the URL of an imported filter list to the assetKey
|
|
|
|
// of an existing stock list.
|
|
|
|
const assetKeyFromURL = url => {
|
|
|
|
const needle = url.replace(/^https?:/, '');
|
|
|
|
const assets = this.availableFilterLists;
|
|
|
|
for ( const assetKey in assets ) {
|
|
|
|
const asset = assets[assetKey];
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( asset.content !== 'filters' ) { continue; }
|
|
|
|
if ( typeof asset.contentURL === 'string' ) {
|
|
|
|
if ( asset.contentURL.endsWith(needle) ) { return assetKey; }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( Array.isArray(asset.contentURL) === false ) { continue; }
|
2019-05-15 20:49:12 +02:00
|
|
|
for ( let i = 0, n = asset.contentURL.length; i < n; i++ ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( asset.contentURL[i].endsWith(needle) ) {
|
|
|
|
return assetKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
2021-01-31 16:30:12 +01:00
|
|
|
const importedSet = new Set(this.listKeysFromCustomFilterLists(importedLists));
|
2019-05-15 20:49:12 +02:00
|
|
|
const toImportSet = new Set(this.listKeysFromCustomFilterLists(details.toImport));
|
|
|
|
for ( const urlKey of toImportSet ) {
|
2020-09-13 17:44:42 +02:00
|
|
|
if ( importedSet.has(urlKey) ) {
|
|
|
|
selectedListKeySet.add(urlKey);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-15 20:49:12 +02:00
|
|
|
const assetKey = assetKeyFromURL(urlKey);
|
2017-05-19 16:12:55 +02:00
|
|
|
if ( assetKey === urlKey ) {
|
|
|
|
importedSet.add(urlKey);
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
selectedListKeySet.add(assetKey);
|
|
|
|
}
|
2021-01-31 16:30:12 +01:00
|
|
|
importedLists = Array.from(importedSet).sort();
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:49:12 +02:00
|
|
|
const result = Array.from(selectedListKeySet);
|
2021-01-31 16:30:12 +01:00
|
|
|
if ( importedLists.join() !== this.userSettings.importedLists.join() ) {
|
|
|
|
this.userSettings.importedLists = importedLists;
|
2021-01-16 16:35:56 +01:00
|
|
|
this.saveUserSettings();
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
this.saveSelectedFilterLists(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.listKeysFromCustomFilterLists = function(raw) {
|
2021-01-08 15:18:26 +01:00
|
|
|
const urls = typeof raw === 'string'
|
|
|
|
? raw.trim().split(/[\n\r]+/)
|
|
|
|
: raw;
|
2019-05-15 20:49:12 +02:00
|
|
|
const out = new Set();
|
|
|
|
const reIgnore = /^[!#]/;
|
|
|
|
const reValid = /^[a-z-]+:\/\/\S+/;
|
2021-01-08 15:18:26 +01:00
|
|
|
for ( const url of urls ) {
|
|
|
|
if ( reIgnore.test(url) || !reValid.test(url) ) { continue; }
|
2020-09-09 15:57:29 +02:00
|
|
|
// Ignore really bad lists.
|
2021-01-08 15:18:26 +01:00
|
|
|
if ( this.badLists.get(url) === true ) { continue; }
|
|
|
|
out.add(url);
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
2018-06-01 13:54:31 +02:00
|
|
|
return Array.from(out);
|
2017-01-22 22:05:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveUserFilters = function(content) {
|
2015-12-07 14:59:22 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1022
|
2019-05-15 20:49:12 +02:00
|
|
|
// Be sure to end with an empty line.
|
2015-12-07 14:59:22 +01:00
|
|
|
content = content.trim();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( content !== '' ) { content += '\n'; }
|
|
|
|
this.removeCompiledFilterList(this.userFiltersPath);
|
2021-07-29 01:48:38 +02:00
|
|
|
return io.put(this.userFiltersPath, content);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadUserFilters = function() {
|
|
|
|
return io.get(this.userFiltersPath);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.appendUserFilters = async function(filters, options) {
|
2019-01-08 13:37:50 +01:00
|
|
|
filters = filters.trim();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( filters.length === 0 ) { return; }
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2019-01-08 13:37:50 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/372
|
|
|
|
// Auto comment using user-defined template.
|
|
|
|
let comment = '';
|
|
|
|
if (
|
|
|
|
options instanceof Object &&
|
|
|
|
options.autoComment === true &&
|
|
|
|
this.hiddenSettings.autoCommentFilterTemplate.indexOf('{{') !== -1
|
|
|
|
) {
|
|
|
|
const d = new Date();
|
2020-07-27 14:41:14 +02:00
|
|
|
// Date in YYYY-MM-DD format - https://stackoverflow.com/a/50130338
|
|
|
|
const ISO8061Date = new Date(d.getTime() +
|
|
|
|
(d.getTimezoneOffset()*60000)).toISOString().split('T')[0];
|
2020-10-07 17:52:38 +02:00
|
|
|
const url = new URL(options.docURL);
|
2019-01-08 13:37:50 +01:00
|
|
|
comment =
|
|
|
|
'! ' +
|
|
|
|
this.hiddenSettings.autoCommentFilterTemplate
|
2020-07-27 14:41:14 +02:00
|
|
|
.replace('{{date}}', ISO8061Date)
|
2019-01-08 13:37:50 +01:00
|
|
|
.replace('{{time}}', d.toLocaleTimeString())
|
2020-10-07 17:52:38 +02:00
|
|
|
.replace('{{hostname}}', url.hostname)
|
|
|
|
.replace('{{origin}}', url.origin)
|
|
|
|
.replace('{{url}}', url.href);
|
2019-01-08 13:37:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const details = await this.loadUserFilters();
|
|
|
|
if ( details.error ) { return; }
|
2019-08-09 15:31:20 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// The comment, if any, will be applied if and only if it is different
|
|
|
|
// from the last comment found in the user filter list.
|
|
|
|
if ( comment !== '' ) {
|
2020-10-07 20:23:57 +02:00
|
|
|
const beg = details.content.lastIndexOf(comment);
|
|
|
|
const end = beg === -1 ? -1 : beg + comment.length;
|
|
|
|
if (
|
|
|
|
end === -1 ||
|
|
|
|
details.content.startsWith('\n', end) === false ||
|
|
|
|
details.content.includes('\n!', end)
|
|
|
|
) {
|
2019-09-15 13:58:28 +02:00
|
|
|
filters = '\n' + comment + '\n' + filters;
|
2019-08-09 15:31:20 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/976
|
|
|
|
// If we reached this point, the filter quite probably needs to be
|
|
|
|
// added for sure: do not try to be too smart, trying to avoid
|
|
|
|
// duplicates at this point may lead to more issues.
|
|
|
|
await this.saveUserFilters(details.content.trim() + '\n' + filters);
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2020-10-07 20:23:57 +02:00
|
|
|
const compiledFilters = this.compileFilters(filters, {
|
|
|
|
assetKey: this.userFiltersPath
|
|
|
|
});
|
2021-07-29 01:48:38 +02:00
|
|
|
const snfe = staticNetFilteringEngine;
|
|
|
|
const cfe = cosmeticFilteringEngine;
|
2019-09-15 13:58:28 +02:00
|
|
|
const acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
|
|
|
|
const discardedCount = snfe.discardedCount + cfe.discardedCount;
|
|
|
|
this.applyCompiledFilters(compiledFilters, true);
|
|
|
|
const entry = this.availableFilterLists[this.userFiltersPath];
|
|
|
|
const deltaEntryCount =
|
|
|
|
snfe.acceptedCount +
|
|
|
|
cfe.acceptedCount - acceptedCount;
|
|
|
|
const deltaEntryUsedCount =
|
|
|
|
deltaEntryCount -
|
|
|
|
(snfe.discardedCount + cfe.discardedCount - discardedCount);
|
|
|
|
entry.entryCount += deltaEntryCount;
|
|
|
|
entry.entryUsedCount += deltaEntryUsedCount;
|
|
|
|
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.freeze();
|
|
|
|
redirectEngine.freeze();
|
|
|
|
staticExtFilteringEngine.freeze();
|
2019-09-15 13:58:28 +02:00
|
|
|
this.selfieManager.destroy();
|
|
|
|
|
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/cj7g7m/
|
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/cnq0bi/
|
|
|
|
if ( options.killCache ) {
|
|
|
|
browser.webRequest.handlerBehaviorChanged();
|
|
|
|
}
|
2021-07-17 18:03:56 +02:00
|
|
|
|
|
|
|
vAPI.messaging.broadcast({ what: 'userFiltersUpdated' });
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.createUserFilters = function(details) {
|
2019-09-01 18:43:12 +02:00
|
|
|
this.appendUserFilters(details.filters, details);
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1786
|
2020-10-07 17:52:38 +02:00
|
|
|
if ( details.docURL === undefined ) { return; }
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.removeFromSelectorCache(
|
2021-07-25 16:55:35 +02:00
|
|
|
hostnameFromURI(details.docURL)
|
2020-10-07 17:52:38 +02:00
|
|
|
);
|
2019-09-01 18:43:12 +02:00
|
|
|
};
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.autoSelectRegionalFilterLists = function(lists) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const selectedListKeys = [ this.userFiltersPath ];
|
|
|
|
for ( const key in lists ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( lists.hasOwnProperty(key) === false ) { continue; }
|
2019-05-15 20:49:12 +02:00
|
|
|
const list = lists[key];
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( list.off !== true ) {
|
|
|
|
selectedListKeys.push(key);
|
|
|
|
continue;
|
2015-02-25 22:51:04 +01:00
|
|
|
}
|
2017-11-09 18:53:05 +01:00
|
|
|
if ( this.listMatchesEnvironment(list) ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
selectedListKeys.push(key);
|
|
|
|
list.off = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selectedListKeys;
|
|
|
|
};
|
2015-02-25 22:51:04 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.getAvailableLists = async function() {
|
2019-05-15 20:49:12 +02:00
|
|
|
let oldAvailableLists = {},
|
2017-01-18 19:17:47 +01:00
|
|
|
newAvailableLists = {};
|
|
|
|
|
|
|
|
// User filter list.
|
|
|
|
newAvailableLists[this.userFiltersPath] = {
|
2022-01-11 13:55:37 +01:00
|
|
|
content: 'filters',
|
2018-04-09 21:45:25 +02:00
|
|
|
group: 'user',
|
2022-01-11 13:55:37 +01:00
|
|
|
title: vAPI.i18n('1pPageName'),
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Custom filter lists.
|
2019-05-15 20:49:12 +02:00
|
|
|
const importedListKeys = this.listKeysFromCustomFilterLists(
|
2021-01-31 16:30:12 +01:00
|
|
|
this.userSettings.importedLists
|
2019-05-15 20:49:12 +02:00
|
|
|
);
|
|
|
|
for ( const listKey of importedListKeys ) {
|
|
|
|
const entry = {
|
2017-01-18 19:17:47 +01:00
|
|
|
content: 'filters',
|
2017-07-03 15:08:46 +02:00
|
|
|
contentURL: listKey,
|
2017-01-18 19:17:47 +01:00
|
|
|
external: true,
|
|
|
|
group: 'custom',
|
|
|
|
submitter: 'user',
|
2020-08-21 17:57:20 +02:00
|
|
|
title: '',
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
newAvailableLists[listKey] = entry;
|
2021-07-29 01:48:38 +02:00
|
|
|
io.registerAssetSource(listKey, entry);
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 18:27:07 +01:00
|
|
|
// Convert a no longer existing stock list into an imported list, except
|
|
|
|
// when the removed stock list is deemed a "bad list".
|
2019-05-15 20:49:12 +02:00
|
|
|
const customListFromStockList = assetKey => {
|
|
|
|
const oldEntry = oldAvailableLists[assetKey];
|
2017-07-03 15:08:46 +02:00
|
|
|
if ( oldEntry === undefined || oldEntry.off === true ) { return; }
|
2019-05-15 20:49:12 +02:00
|
|
|
let listURL = oldEntry.contentURL;
|
2017-07-03 15:08:46 +02:00
|
|
|
if ( Array.isArray(listURL) ) {
|
|
|
|
listURL = listURL[0];
|
|
|
|
}
|
2022-03-18 18:27:07 +01:00
|
|
|
if ( this.badLists.has(listURL) ) { return; }
|
2019-05-15 20:49:12 +02:00
|
|
|
const newEntry = {
|
2017-07-03 15:08:46 +02:00
|
|
|
content: 'filters',
|
|
|
|
contentURL: listURL,
|
|
|
|
external: true,
|
|
|
|
group: 'custom',
|
|
|
|
submitter: 'user',
|
|
|
|
title: oldEntry.title || ''
|
|
|
|
};
|
|
|
|
newAvailableLists[listURL] = newEntry;
|
2021-07-29 01:48:38 +02:00
|
|
|
io.registerAssetSource(listURL, newEntry);
|
2017-07-03 15:08:46 +02:00
|
|
|
importedListKeys.push(listURL);
|
2021-01-31 16:30:12 +01:00
|
|
|
this.userSettings.importedLists.push(listURL.trim());
|
2021-01-16 16:35:56 +01:00
|
|
|
this.saveUserSettings();
|
2019-05-15 20:49:12 +02:00
|
|
|
this.saveSelectedFilterLists([ listURL ], true);
|
2017-07-03 15:08:46 +02:00
|
|
|
};
|
|
|
|
|
2020-08-22 14:43:16 +02:00
|
|
|
const promises = [
|
2019-09-15 13:58:28 +02:00
|
|
|
vAPI.storage.get('availableFilterLists'),
|
2021-07-29 01:48:38 +02:00
|
|
|
io.metadata(),
|
|
|
|
this.badLists.size === 0 ? io.get('ublock-badlists') : false,
|
2020-08-22 14:43:16 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// Load previously saved available lists -- these contains data
|
|
|
|
// computed at run-time, we will reuse this data if possible.
|
|
|
|
const [ bin, entries, badlists ] = await Promise.all(promises);
|
|
|
|
|
|
|
|
if ( badlists instanceof Object ) {
|
|
|
|
for ( const line of badlists.content.split(/\s*[\n\r]+\s*/) ) {
|
|
|
|
if ( line === '' || line.startsWith('#') ) { continue; }
|
|
|
|
const fields = line.split(/\s+/);
|
2020-09-09 15:57:29 +02:00
|
|
|
const remove = fields.length === 2;
|
|
|
|
this.badLists.set(fields[0], remove);
|
2020-08-22 14:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 14:41:14 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
oldAvailableLists = bin && bin.availableFilterLists || {};
|
|
|
|
|
|
|
|
for ( const assetKey in entries ) {
|
|
|
|
if ( entries.hasOwnProperty(assetKey) === false ) { continue; }
|
|
|
|
const entry = entries[assetKey];
|
|
|
|
if ( entry.content !== 'filters' ) { continue; }
|
|
|
|
newAvailableLists[assetKey] = Object.assign({}, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load set of currently selected filter lists.
|
|
|
|
const listKeySet = new Set(this.selectedFilterLists);
|
|
|
|
for ( const listKey in newAvailableLists ) {
|
|
|
|
if ( newAvailableLists.hasOwnProperty(listKey) ) {
|
|
|
|
newAvailableLists[listKey].off = !listKeySet.has(listKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//finalize();
|
2017-01-18 19:17:47 +01:00
|
|
|
// Final steps:
|
|
|
|
// - reuse existing list metadata if any;
|
|
|
|
// - unregister unreferenced imported filter lists if any.
|
2019-09-15 13:58:28 +02:00
|
|
|
// Reuse existing metadata.
|
|
|
|
for ( const assetKey in oldAvailableLists ) {
|
|
|
|
const oldEntry = oldAvailableLists[assetKey];
|
|
|
|
const newEntry = newAvailableLists[assetKey];
|
|
|
|
// List no longer exists. If a stock list, try to convert to
|
|
|
|
// imported list if it was selected.
|
|
|
|
if ( newEntry === undefined ) {
|
|
|
|
this.removeFilterList(assetKey);
|
|
|
|
if ( assetKey.indexOf('://') === -1 ) {
|
|
|
|
customListFromStockList(assetKey);
|
2014-09-26 02:21:21 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
continue;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( oldEntry.entryCount !== undefined ) {
|
|
|
|
newEntry.entryCount = oldEntry.entryCount;
|
2016-01-03 19:58:25 +01:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( oldEntry.entryUsedCount !== undefined ) {
|
|
|
|
newEntry.entryUsedCount = oldEntry.entryUsedCount;
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
// This may happen if the list name was pulled from the list
|
|
|
|
// content.
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/982
|
|
|
|
// There is no guarantee the title was successfully extracted from
|
|
|
|
// the list content.
|
|
|
|
if (
|
|
|
|
newEntry.title === '' &&
|
|
|
|
typeof oldEntry.title === 'string' &&
|
|
|
|
oldEntry.title !== ''
|
|
|
|
) {
|
|
|
|
newEntry.title = oldEntry.title;
|
2017-01-26 16:17:38 +01:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2017-01-26 16:17:38 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// Remove unreferenced imported filter lists.
|
|
|
|
for ( const assetKey in newAvailableLists ) {
|
|
|
|
const newEntry = newAvailableLists[assetKey];
|
|
|
|
if ( newEntry.submitter !== 'user' ) { continue; }
|
|
|
|
if ( importedListKeys.indexOf(assetKey) !== -1 ) { continue; }
|
|
|
|
delete newAvailableLists[assetKey];
|
2021-07-29 01:48:38 +02:00
|
|
|
io.unregisterAssetSource(assetKey);
|
2019-09-15 13:58:28 +02:00
|
|
|
this.removeFilterList(assetKey);
|
|
|
|
}
|
2016-01-03 19:58:25 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
return newAvailableLists;
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadFilterLists = (( ) => {
|
2018-12-06 01:18:20 +01:00
|
|
|
const loadedListKeys = [];
|
2019-09-15 13:58:28 +02:00
|
|
|
let loadingPromise;
|
2020-02-23 18:18:45 +01:00
|
|
|
let t0 = 0;
|
2019-10-29 15:26:34 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const onDone = function() {
|
2021-07-29 01:48:38 +02:00
|
|
|
ubolog(`loadFilterLists() took ${Date.now()-t0} ms`);
|
2019-10-29 15:26:34 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.freeze();
|
|
|
|
staticExtFilteringEngine.freeze();
|
|
|
|
redirectEngine.freeze();
|
2019-06-30 16:09:27 +02:00
|
|
|
vAPI.net.unsuspend();
|
|
|
|
|
2019-05-15 20:49:12 +02:00
|
|
|
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
|
2015-02-24 19:48:03 +01:00
|
|
|
|
2017-01-20 21:17:11 +01:00
|
|
|
vAPI.messaging.broadcast({
|
|
|
|
what: 'staticFilteringDataChanged',
|
2019-05-15 20:49:12 +02:00
|
|
|
parseCosmeticFilters: this.userSettings.parseAllABPHideFilters,
|
|
|
|
ignoreGenericCosmeticFilters: this.userSettings.ignoreGenericCosmeticFilters,
|
2017-01-20 21:17:11 +01:00
|
|
|
listKeys: loadedListKeys
|
|
|
|
});
|
2016-09-24 20:36:08 +02:00
|
|
|
|
2019-05-15 20:49:12 +02:00
|
|
|
this.selfieManager.destroy();
|
2021-07-29 01:48:38 +02:00
|
|
|
lz4Codec.relinquish();
|
2019-10-27 16:49:05 +01:00
|
|
|
this.compiledFormatChanged = false;
|
2018-12-06 01:18:20 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
loadingPromise = undefined;
|
2014-07-25 22:12:20 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const applyCompiledFilters = function(assetKey, compiled) {
|
2021-07-29 01:48:38 +02:00
|
|
|
const snfe = staticNetFilteringEngine;
|
|
|
|
const sxfe = staticExtFilteringEngine;
|
2018-12-06 01:18:20 +01:00
|
|
|
let acceptedCount = snfe.acceptedCount + sxfe.acceptedCount,
|
2017-12-29 19:31:37 +01:00
|
|
|
discardedCount = snfe.discardedCount + sxfe.discardedCount;
|
2019-05-15 20:49:12 +02:00
|
|
|
this.applyCompiledFilters(compiled, assetKey === this.userFiltersPath);
|
|
|
|
if ( this.availableFilterLists.hasOwnProperty(assetKey) ) {
|
|
|
|
const entry = this.availableFilterLists[assetKey];
|
2017-12-29 19:31:37 +01:00
|
|
|
entry.entryCount = snfe.acceptedCount + sxfe.acceptedCount -
|
|
|
|
acceptedCount;
|
|
|
|
entry.entryUsedCount = entry.entryCount -
|
|
|
|
(snfe.discardedCount + sxfe.discardedCount - discardedCount);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2017-01-20 21:17:11 +01:00
|
|
|
loadedListKeys.push(assetKey);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const onFilterListsReady = function(lists) {
|
2019-06-30 18:54:05 +02:00
|
|
|
this.availableFilterLists = lists;
|
|
|
|
|
2022-02-13 15:24:57 +01:00
|
|
|
if ( vAPI.Net.canSuspend() ) {
|
2021-12-30 15:24:38 +01:00
|
|
|
vAPI.net.suspend();
|
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
redirectEngine.reset();
|
|
|
|
staticExtFilteringEngine.reset();
|
|
|
|
staticNetFilteringEngine.reset();
|
2019-05-15 20:49:12 +02:00
|
|
|
this.selfieManager.destroy();
|
2021-07-29 01:48:38 +02:00
|
|
|
staticFilteringReverseLookup.resetLists();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
// We need to build a complete list of assets to pull first: this is
|
|
|
|
// because it *may* happens that some load operations are synchronous:
|
2021-07-29 01:48:38 +02:00
|
|
|
// This happens for assets which do not exist, or assets with no
|
2015-02-24 00:31:29 +01:00
|
|
|
// content.
|
2018-12-06 01:18:20 +01:00
|
|
|
const toLoad = [];
|
|
|
|
for ( const assetKey in lists ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( lists.hasOwnProperty(assetKey) === false ) { continue; }
|
|
|
|
if ( lists[assetKey].off ) { continue; }
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
toLoad.push(
|
|
|
|
this.getCompiledFilterList(assetKey).then(details => {
|
|
|
|
applyCompiledFilters.call(
|
|
|
|
this,
|
|
|
|
details.assetKey,
|
|
|
|
details.content
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
|
|
|
|
return Promise.all(toLoad);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
return function() {
|
|
|
|
if ( loadingPromise instanceof Promise === false ) {
|
2020-02-23 18:18:45 +01:00
|
|
|
t0 = Date.now();
|
2019-09-15 13:58:28 +02:00
|
|
|
loadedListKeys.length = 0;
|
|
|
|
loadingPromise = Promise.all([
|
2019-10-29 15:26:34 +01:00
|
|
|
this.getAvailableLists().then(lists =>
|
|
|
|
onFilterListsReady.call(this, lists)
|
|
|
|
),
|
2019-09-15 13:58:28 +02:00
|
|
|
this.loadRedirectResources(),
|
|
|
|
]).then(( ) => {
|
|
|
|
onDone.call(this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return loadingPromise;
|
|
|
|
};
|
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.getCompiledFilterList = async function(assetKey) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const compiledPath = 'compiled/' + assetKey;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2020-12-08 16:00:47 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1365
|
|
|
|
// Verify that the list version matches that of the current compiled
|
|
|
|
// format.
|
2020-08-21 17:57:20 +02:00
|
|
|
if (
|
|
|
|
this.compiledFormatChanged === false &&
|
|
|
|
this.badLists.has(assetKey) === false
|
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
const compiledDetails = await io.get(compiledPath);
|
2021-08-16 18:15:30 +02:00
|
|
|
const compilerVersion = `${this.systemSettings.compiledMagic}\n`;
|
|
|
|
if ( compiledDetails.content.startsWith(compilerVersion) ) {
|
2019-10-27 16:49:05 +01:00
|
|
|
compiledDetails.assetKey = assetKey;
|
|
|
|
return compiledDetails;
|
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2017-05-20 21:32:03 +02:00
|
|
|
|
2020-08-22 14:43:16 +02:00
|
|
|
// Skip downloading really bad lists.
|
|
|
|
if ( this.badLists.get(assetKey) ) {
|
|
|
|
return { assetKey, content: '' };
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const rawDetails = await io.get(assetKey, { silent: true });
|
2019-09-15 13:58:28 +02:00
|
|
|
// Compiling an empty string results in an empty string.
|
|
|
|
if ( rawDetails.content === '' ) {
|
|
|
|
rawDetails.assetKey = assetKey;
|
|
|
|
return rawDetails;
|
|
|
|
}
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
this.extractFilterListMetadata(assetKey, rawDetails.content);
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2020-08-21 17:57:20 +02:00
|
|
|
// Skip compiling bad lists.
|
|
|
|
if ( this.badLists.has(assetKey) ) {
|
|
|
|
return { assetKey, content: '' };
|
|
|
|
}
|
|
|
|
|
2020-12-04 12:17:18 +01:00
|
|
|
const compiledContent =
|
|
|
|
this.compileFilters(rawDetails.content, { assetKey });
|
2021-07-29 01:48:38 +02:00
|
|
|
io.put(compiledPath, compiledContent);
|
2019-09-15 13:58:28 +02:00
|
|
|
|
2020-12-04 12:17:18 +01:00
|
|
|
return { assetKey, content: compiledContent };
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
2014-09-26 02:21:21 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
/******************************************************************************/
|
2014-09-26 02:21:21 +02:00
|
|
|
|
2018-02-23 12:42:17 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3406
|
|
|
|
// Lower minimum update period to 1 day.
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.extractFilterListMetadata = function(assetKey, raw) {
|
2019-05-15 20:49:12 +02:00
|
|
|
const listEntry = this.availableFilterLists[assetKey];
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( listEntry === undefined ) { return; }
|
|
|
|
// Metadata expected to be found at the top of content.
|
2019-05-15 20:49:12 +02:00
|
|
|
const head = raw.slice(0, 1024);
|
2017-01-18 19:17:47 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/313
|
|
|
|
// Always try to fetch the name if this is an external filter list.
|
2020-11-18 16:02:22 +01:00
|
|
|
if ( listEntry.group === 'custom' ) {
|
|
|
|
let matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Title[\t ]*:([^\n]+)/i);
|
|
|
|
const title = matches && matches[1].trim() || '';
|
|
|
|
if ( title !== '' && title !== listEntry.title ) {
|
2021-07-31 14:38:33 +02:00
|
|
|
listEntry.title = orphanizeString(title);
|
2021-07-29 01:48:38 +02:00
|
|
|
io.registerAssetSource(assetKey, { title });
|
2020-11-18 16:02:22 +01:00
|
|
|
}
|
2020-11-18 18:14:23 +01:00
|
|
|
matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Homepage[\t ]*:[\t ]*(https?:\/\/\S+)\s/i);
|
2020-11-18 16:02:22 +01:00
|
|
|
const supportURL = matches && matches[1] || '';
|
|
|
|
if ( supportURL !== '' && supportURL !== listEntry.supportURL ) {
|
2021-07-31 14:38:33 +02:00
|
|
|
listEntry.supportURL = orphanizeString(supportURL);
|
2021-07-29 01:48:38 +02:00
|
|
|
io.registerAssetSource(assetKey, { supportURL });
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Extract update frequency information
|
2019-05-15 20:49:12 +02:00
|
|
|
const matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Expires[\t ]*:[\t ]*(\d+)[\t ]*(h)?/i);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( matches !== null ) {
|
2020-11-18 16:02:22 +01:00
|
|
|
let updateAfter = parseInt(matches[1], 10);
|
|
|
|
if ( isNaN(updateAfter) === false ) {
|
2020-07-06 14:31:53 +02:00
|
|
|
if ( matches[2] !== undefined ) {
|
2020-11-18 16:02:22 +01:00
|
|
|
updateAfter = Math.ceil(updateAfter / 24);
|
2020-07-06 14:31:53 +02:00
|
|
|
}
|
2022-03-24 18:23:24 +01:00
|
|
|
updateAfter = Math.max(updateAfter, 0.5);
|
2020-11-18 16:02:22 +01:00
|
|
|
if ( updateAfter !== listEntry.updateAfter ) {
|
|
|
|
listEntry.updateAfter = updateAfter;
|
2021-07-29 01:48:38 +02:00
|
|
|
io.registerAssetSource(assetKey, { updateAfter });
|
2020-07-06 14:31:53 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-25 21:44:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-02-25 22:33:51 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.removeCompiledFilterList = function(assetKey) {
|
|
|
|
io.remove('compiled/' + assetKey);
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.removeFilterList = function(assetKey) {
|
2017-01-18 19:17:47 +01:00
|
|
|
this.removeCompiledFilterList(assetKey);
|
2021-07-29 01:48:38 +02:00
|
|
|
io.remove(assetKey);
|
2015-02-25 22:33:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.compileFilters = function(rawText, details = {}) {
|
2021-07-25 16:55:35 +02:00
|
|
|
const writer = new CompiledListWriter();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-12-15 16:46:17 +01:00
|
|
|
// Populate the writer with information potentially useful to the
|
|
|
|
// client compilers.
|
2020-11-13 15:23:25 +01:00
|
|
|
if ( details.assetKey ) {
|
2021-07-25 16:55:35 +02:00
|
|
|
writer.properties.set('name', details.assetKey);
|
2018-12-15 16:46:17 +01:00
|
|
|
}
|
2020-11-13 15:23:25 +01:00
|
|
|
const expertMode =
|
|
|
|
details.assetKey !== this.userFiltersPath ||
|
|
|
|
this.hiddenSettings.filterAuthorMode !== false;
|
2014-06-24 00:42:43 +02:00
|
|
|
// Useful references:
|
|
|
|
// https://adblockplus.org/en/filter-cheatsheet
|
|
|
|
// https://adblockplus.org/en/filters
|
2021-07-25 16:55:35 +02:00
|
|
|
const lineIter = new LineIterator(this.preparseDirectives.prune(rawText));
|
|
|
|
const parser = new StaticFilteringParser({ expertMode });
|
2021-08-04 21:14:48 +02:00
|
|
|
const compiler = staticNetFilteringEngine.createCompiler(parser);
|
2020-06-04 13:18:54 +02:00
|
|
|
|
2020-11-09 12:54:51 +01:00
|
|
|
parser.setMaxTokenLength(staticNetFilteringEngine.MAX_TOKEN_LENGTH);
|
2017-01-18 19:17:47 +01:00
|
|
|
|
2021-12-07 17:15:14 +01:00
|
|
|
compiler.start(writer);
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
while ( lineIter.eot() === false ) {
|
2020-06-04 13:18:54 +02:00
|
|
|
let line = lineIter.next();
|
2015-01-23 17:32:49 +01:00
|
|
|
|
2020-03-14 18:19:41 +01:00
|
|
|
while ( line.endsWith(' \\') ) {
|
2020-03-15 13:15:17 +01:00
|
|
|
if ( lineIter.peek(4) !== ' ' ) { break; }
|
2020-03-14 18:19:41 +01:00
|
|
|
line = line.slice(0, -2).trim() + lineIter.next().trim();
|
|
|
|
}
|
|
|
|
|
2020-06-04 13:18:54 +02:00
|
|
|
parser.analyze(line);
|
|
|
|
|
|
|
|
if ( parser.shouldIgnore() ) { continue; }
|
2015-01-23 17:32:49 +01:00
|
|
|
|
2020-06-04 13:18:54 +02:00
|
|
|
if ( parser.category === parser.CATStaticExtFilter ) {
|
|
|
|
staticExtFilteringEngine.compile(parser, writer);
|
|
|
|
continue;
|
2015-01-23 17:32:49 +01:00
|
|
|
}
|
2015-03-07 19:20:18 +01:00
|
|
|
|
2020-06-04 13:18:54 +02:00
|
|
|
if ( parser.category !== parser.CATStaticNetFilter ) { continue; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2020-06-04 13:18:54 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2599
|
|
|
|
// convert hostname to punycode if needed
|
2020-07-04 20:47:33 +02:00
|
|
|
if ( parser.patternHasUnicode() && parser.toASCII() === false ) {
|
2020-06-09 17:58:27 +02:00
|
|
|
continue;
|
2020-06-04 13:18:54 +02:00
|
|
|
}
|
2021-08-05 19:30:20 +02:00
|
|
|
if ( compiler.compile(writer) ) { continue; }
|
2021-08-04 21:14:48 +02:00
|
|
|
if ( compiler.error !== undefined ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
logger.writeOne({
|
2021-07-25 16:55:35 +02:00
|
|
|
realm: 'message',
|
|
|
|
type: 'error',
|
2021-08-04 21:14:48 +02:00
|
|
|
text: compiler.error
|
2021-07-25 16:55:35 +02:00
|
|
|
});
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:15:14 +01:00
|
|
|
compiler.finish(writer);
|
|
|
|
|
2020-12-08 16:00:47 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1365
|
|
|
|
// Embed version into compiled list itself: it is encoded in as the
|
|
|
|
// first digits followed by a whitespace.
|
|
|
|
const compiledContent
|
|
|
|
= `${this.systemSettings.compiledMagic}\n` + writer.toString();
|
|
|
|
|
|
|
|
return compiledContent;
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-02-17 15:28:20 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1395
|
2016-02-17 16:04:55 +01:00
|
|
|
// Added `firstparty` argument: to avoid discarding cosmetic filters when
|
2016-02-17 15:28:20 +01:00
|
|
|
// applying 1st-party filters.
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.applyCompiledFilters = function(rawText, firstparty) {
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( rawText === '' ) { return; }
|
2021-07-25 16:55:35 +02:00
|
|
|
const reader = new CompiledListReader(rawText);
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.fromCompiled(reader);
|
|
|
|
staticExtFilteringEngine.fromCompiledContent(reader, {
|
2017-12-28 19:49:02 +01:00
|
|
|
skipGenericCosmetic: this.userSettings.ignoreGenericCosmeticFilters,
|
|
|
|
skipCosmetic: !firstparty && !this.userSettings.parseAllABPHideFilters
|
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-04-05 13:29:15 +02:00
|
|
|
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/917
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.preparseDirectives = {
|
2020-07-03 14:43:40 +02:00
|
|
|
// This method returns an array of indices, corresponding to position in
|
|
|
|
// the content string which should alternatively be parsed and discarded.
|
|
|
|
split: function(content) {
|
|
|
|
const reIf = /^!#(if|endif)\b([^\n]*)(?:[\n\r]+|$)/gm;
|
2020-07-10 14:01:39 +02:00
|
|
|
const soup = vAPI.webextFlavor.soup;
|
2020-07-03 14:43:40 +02:00
|
|
|
const stack = [];
|
|
|
|
const shouldDiscard = ( ) => stack.some(v => v);
|
|
|
|
const parts = [ 0 ];
|
|
|
|
let discard = false;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const match = reIf.exec(content);
|
|
|
|
if ( match === null ) { break; }
|
|
|
|
|
|
|
|
switch ( match[1] ) {
|
|
|
|
case 'if':
|
|
|
|
let expr = match[2].trim();
|
|
|
|
const target = expr.charCodeAt(0) === 0x21 /* '!' */;
|
|
|
|
if ( target ) { expr = expr.slice(1); }
|
|
|
|
const token = this.tokens.get(expr);
|
|
|
|
const startDiscard =
|
2020-07-10 14:01:39 +02:00
|
|
|
token === 'false' && target === false ||
|
|
|
|
token !== undefined && soup.has(token) === target;
|
2020-07-03 14:43:40 +02:00
|
|
|
if ( discard === false && startDiscard ) {
|
|
|
|
parts.push(match.index);
|
|
|
|
discard = true;
|
|
|
|
}
|
|
|
|
stack.push(startDiscard);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'endif':
|
|
|
|
stack.pop();
|
|
|
|
const stopDiscard = shouldDiscard() === false;
|
|
|
|
if ( discard && stopDiscard ) {
|
|
|
|
parts.push(match.index + match[0].length);
|
|
|
|
discard = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-18 16:31:04 +02:00
|
|
|
|
2020-07-03 14:43:40 +02:00
|
|
|
default:
|
|
|
|
break;
|
2019-05-18 16:31:04 +02:00
|
|
|
}
|
2018-04-05 13:29:15 +02:00
|
|
|
}
|
2019-05-18 16:31:04 +02:00
|
|
|
|
2020-07-03 14:43:40 +02:00
|
|
|
parts.push(content.length);
|
|
|
|
return parts;
|
|
|
|
},
|
|
|
|
|
|
|
|
prune: function(content) {
|
|
|
|
const parts = this.split(content);
|
|
|
|
const out = [];
|
|
|
|
for ( let i = 0, n = parts.length - 1; i < n; i += 2 ) {
|
|
|
|
const beg = parts[i+0];
|
|
|
|
const end = parts[i+1];
|
|
|
|
out.push(content.slice(beg, end));
|
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
},
|
|
|
|
|
2020-07-09 14:09:51 +02:00
|
|
|
getHints: function() {
|
|
|
|
const out = [];
|
|
|
|
const vals = new Set();
|
|
|
|
for ( const [ key, val ] of this.tokens ) {
|
|
|
|
if ( vals.has(val) ) { continue; }
|
|
|
|
vals.add(val);
|
|
|
|
out.push(key);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
|
|
|
getTokens: function() {
|
2020-07-10 14:01:39 +02:00
|
|
|
const out = new Map();
|
|
|
|
const soup = vAPI.webextFlavor.soup;
|
|
|
|
for ( const [ key, val ] of this.tokens ) {
|
|
|
|
out.set(key, val !== 'false' && soup.has(val));
|
|
|
|
}
|
|
|
|
return Array.from(out);
|
2020-07-09 14:09:51 +02:00
|
|
|
},
|
|
|
|
|
2020-07-03 14:43:40 +02:00
|
|
|
tokens: new Map([
|
|
|
|
[ 'ext_ublock', 'ublock' ],
|
|
|
|
[ 'env_chromium', 'chromium' ],
|
|
|
|
[ 'env_edge', 'edge' ],
|
|
|
|
[ 'env_firefox', 'firefox' ],
|
|
|
|
[ 'env_legacy', 'legacy' ],
|
|
|
|
[ 'env_mobile', 'mobile' ],
|
|
|
|
[ 'env_safari', 'safari' ],
|
|
|
|
[ 'cap_html_filtering', 'html_filtering' ],
|
|
|
|
[ 'cap_user_stylesheet', 'user_stylesheet' ],
|
|
|
|
[ 'false', 'false' ],
|
2020-08-13 15:32:34 +02:00
|
|
|
// Hoping ABP-only list maintainers can at least make use of it to
|
2020-08-13 15:40:43 +02:00
|
|
|
// help non-ABP content blockers better deal with filters benefiting
|
2020-08-13 15:32:34 +02:00
|
|
|
// only ABP.
|
|
|
|
[ 'ext_abp', 'false' ],
|
2020-07-08 15:52:27 +02:00
|
|
|
// Compatibility with other blockers
|
|
|
|
// https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#adguard-specific
|
|
|
|
[ 'adguard', 'adguard' ],
|
2020-08-13 15:32:34 +02:00
|
|
|
[ 'adguard_app_android', 'false' ],
|
|
|
|
[ 'adguard_app_ios', 'false' ],
|
|
|
|
[ 'adguard_app_mac', 'false' ],
|
2020-07-10 14:01:39 +02:00
|
|
|
[ 'adguard_app_windows', 'false' ],
|
2020-08-13 15:32:34 +02:00
|
|
|
[ 'adguard_ext_android_cb', 'false' ],
|
2020-07-08 15:52:27 +02:00
|
|
|
[ 'adguard_ext_chromium', 'chromium' ],
|
|
|
|
[ 'adguard_ext_edge', 'edge' ],
|
|
|
|
[ 'adguard_ext_firefox', 'firefox' ],
|
|
|
|
[ 'adguard_ext_opera', 'chromium' ],
|
2020-08-13 15:32:34 +02:00
|
|
|
[ 'adguard_ext_safari', 'false' ],
|
2020-07-03 14:43:40 +02:00
|
|
|
]),
|
2018-04-05 13:29:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadRedirectResources = async function() {
|
2019-09-15 13:58:28 +02:00
|
|
|
try {
|
2021-08-02 15:23:48 +02:00
|
|
|
const success = await redirectEngine.resourcesFromSelfie(io);
|
2019-07-03 15:40:12 +02:00
|
|
|
if ( success === true ) { return true; }
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2021-08-02 15:23:48 +02:00
|
|
|
const fetcher = (path, options = undefined) => {
|
|
|
|
if ( path.startsWith('/web_accessible_resources/') ) {
|
|
|
|
path += `?secret=${vAPI.warSecret()}`;
|
|
|
|
return io.fetch(path, options);
|
|
|
|
}
|
|
|
|
return io.fetchText(path);
|
|
|
|
};
|
|
|
|
|
2019-07-03 15:40:12 +02:00
|
|
|
const fetchPromises = [
|
2021-08-02 15:23:48 +02:00
|
|
|
redirectEngine.loadBuiltinResources(fetcher)
|
2019-07-03 15:40:12 +02:00
|
|
|
];
|
2017-03-05 18:54:47 +01:00
|
|
|
|
2019-04-20 23:16:49 +02:00
|
|
|
const userResourcesLocation = this.hiddenSettings.userResourcesLocation;
|
|
|
|
if ( userResourcesLocation !== 'unset' ) {
|
|
|
|
for ( const url of userResourcesLocation.split(/\s+/) ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
fetchPromises.push(io.fetchText(url));
|
2019-04-20 23:16:49 +02:00
|
|
|
}
|
2017-03-05 18:54:47 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const results = await Promise.all(fetchPromises);
|
2019-07-03 15:40:12 +02:00
|
|
|
if ( Array.isArray(results) === false ) { return results; }
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2019-04-20 23:16:49 +02:00
|
|
|
let content = '';
|
2019-07-03 15:40:12 +02:00
|
|
|
for ( let i = 1; i < results.length; i++ ) {
|
|
|
|
const result = results[i];
|
2019-04-20 23:16:49 +02:00
|
|
|
if (
|
|
|
|
result instanceof Object === false ||
|
|
|
|
typeof result.content !== 'string' ||
|
|
|
|
result.content === ''
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
content += '\n\n' + result.content;
|
2018-02-15 23:25:38 +01:00
|
|
|
}
|
2019-04-20 23:16:49 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
redirectEngine.resourcesFromString(content);
|
2021-08-02 15:23:48 +02:00
|
|
|
redirectEngine.selfieFromResources(io);
|
2019-09-15 13:58:28 +02:00
|
|
|
} catch(ex) {
|
2021-07-29 01:48:38 +02:00
|
|
|
ubolog(ex);
|
2019-07-03 15:40:12 +02:00
|
|
|
return false;
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
|
|
|
return true;
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.loadPublicSuffixList = async function() {
|
2021-08-23 15:42:27 +02:00
|
|
|
const psl = publicSuffixList;
|
2021-07-29 22:54:51 +02:00
|
|
|
|
|
|
|
// WASM is nice but not critical
|
2021-08-08 17:41:05 +02:00
|
|
|
if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) {
|
2021-07-29 22:54:51 +02:00
|
|
|
const wasmModuleFetcher = function(path) {
|
|
|
|
return fetch( `${path}.wasm`, {
|
|
|
|
mode: 'same-origin'
|
|
|
|
}).then(
|
2021-08-23 15:42:27 +02:00
|
|
|
WebAssembly.compileStreaming
|
2021-07-29 22:54:51 +02:00
|
|
|
).catch(reason => {
|
|
|
|
ubolog(reason);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
let result = false;
|
|
|
|
try {
|
|
|
|
result = await psl.enableWASM(wasmModuleFetcher,
|
|
|
|
'./lib/publicsuffixlist/wasm/'
|
|
|
|
);
|
|
|
|
} catch(reason) {
|
|
|
|
ubolog(reason);
|
|
|
|
}
|
|
|
|
if ( result ) {
|
|
|
|
ubolog(`WASM PSL ready ${Date.now()-vAPI.T0} ms after launch`);
|
|
|
|
}
|
2019-02-10 18:19:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
try {
|
2021-07-29 01:48:38 +02:00
|
|
|
const result = await io.get(`compiled/${this.pslAssetKey}`);
|
2021-07-29 22:54:51 +02:00
|
|
|
if ( psl.fromSelfie(result.content, sparseBase64) ) { return; }
|
|
|
|
} catch (reason) {
|
|
|
|
ubolog(reason);
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const result = await io.get(this.pslAssetKey);
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( result.content !== '' ) {
|
|
|
|
this.compilePublicSuffixList(result.content);
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.compilePublicSuffixList = function(content) {
|
2021-08-23 15:42:27 +02:00
|
|
|
const psl = publicSuffixList;
|
2021-08-22 18:03:59 +02:00
|
|
|
psl.parse(content, punycode.toASCII);
|
2021-07-29 01:48:38 +02:00
|
|
|
io.put(`compiled/${this.pslAssetKey}`, psl.toSelfie(sparseBase64));
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
// This is to be sure the selfie is generated in a sane manner: the selfie will
|
2014-10-17 21:44:19 +02:00
|
|
|
// be generated if the user doesn't change his filter lists selection for
|
2014-09-08 23:46:58 +02:00
|
|
|
// some set time.
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.selfieManager = (( ) => {
|
2019-09-15 15:36:50 +02:00
|
|
|
let createTimer;
|
|
|
|
let destroyTimer;
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2018-06-01 13:54:31 +02:00
|
|
|
// As of 2018-05-31:
|
2019-02-14 19:33:55 +01:00
|
|
|
// JSON.stringify-ing ourselves results in a better baseline
|
|
|
|
// memory usage at selfie-load time. For some reasons.
|
|
|
|
|
2019-09-15 15:36:50 +02:00
|
|
|
const create = async function() {
|
|
|
|
await Promise.all([
|
2021-07-29 01:48:38 +02:00
|
|
|
io.put(
|
2019-02-14 19:33:55 +01:00
|
|
|
'selfie/main',
|
|
|
|
JSON.stringify({
|
|
|
|
magic: µb.systemSettings.selfieMagic,
|
|
|
|
availableFilterLists: µb.availableFilterLists,
|
|
|
|
})
|
|
|
|
),
|
2021-07-29 01:48:38 +02:00
|
|
|
redirectEngine.toSelfie('selfie/redirectEngine'),
|
|
|
|
staticExtFilteringEngine.toSelfie(
|
2019-09-22 01:48:02 +02:00
|
|
|
'selfie/staticExtFilteringEngine'
|
|
|
|
),
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.toSelfie(io,
|
2019-09-22 01:48:02 +02:00
|
|
|
'selfie/staticNetFilteringEngine'
|
|
|
|
),
|
2019-09-15 15:36:50 +02:00
|
|
|
]);
|
2021-07-29 01:48:38 +02:00
|
|
|
lz4Codec.relinquish();
|
2019-10-27 16:49:05 +01:00
|
|
|
µb.selfieIsInvalid = false;
|
2018-06-01 13:54:31 +02:00
|
|
|
};
|
2015-11-29 23:06:58 +01:00
|
|
|
|
2019-09-22 01:48:02 +02:00
|
|
|
const loadMain = async function() {
|
2021-07-29 01:48:38 +02:00
|
|
|
const details = await io.get('selfie/main');
|
2019-09-22 01:48:02 +02:00
|
|
|
if (
|
|
|
|
details instanceof Object === false ||
|
|
|
|
typeof details.content !== 'string' ||
|
|
|
|
details.content === ''
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let selfie;
|
|
|
|
try {
|
|
|
|
selfie = JSON.parse(details.content);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
selfie instanceof Object === false ||
|
|
|
|
selfie.magic !== µb.systemSettings.selfieMagic
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
µb.availableFilterLists = selfie.availableFilterLists;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const load = async function() {
|
2019-10-27 16:49:05 +01:00
|
|
|
if ( µb.selfieIsInvalid ) {
|
2019-09-22 01:48:02 +02:00
|
|
|
return false;
|
2019-09-22 01:24:47 +02:00
|
|
|
}
|
2019-09-22 01:48:02 +02:00
|
|
|
try {
|
|
|
|
const results = await Promise.all([
|
|
|
|
loadMain(),
|
2021-07-29 01:48:38 +02:00
|
|
|
redirectEngine.fromSelfie('selfie/redirectEngine'),
|
|
|
|
staticExtFilteringEngine.fromSelfie(
|
2019-09-22 01:48:02 +02:00
|
|
|
'selfie/staticExtFilteringEngine'
|
|
|
|
),
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.fromSelfie(io,
|
2019-09-22 01:48:02 +02:00
|
|
|
'selfie/staticNetFilteringEngine'
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
if ( results.every(v => v) ) {
|
2019-07-03 15:40:12 +02:00
|
|
|
return µb.loadRedirectResources();
|
|
|
|
}
|
2019-09-22 01:48:02 +02:00
|
|
|
}
|
|
|
|
catch (reason) {
|
2021-07-29 01:48:38 +02:00
|
|
|
ubolog(reason);
|
2019-09-22 01:48:02 +02:00
|
|
|
}
|
Expand bidi-trie usage in static network filtering engine
Related issues:
- https://github.com/uBlockOrigin/uBlock-issues/issues/761
- https://github.com/uBlockOrigin/uBlock-issues/issues/528
The previous bidi-trie code could only hold filters which
are plain pattern, i.e. no wildcard characters, and which
had no origin option (`domain=`), right and/or left anchor,
and no `csp=` option.
Example of filters that could be moved into a bidi-trie
data structure:
&ad_box_
/w/d/capu.php?z=$script,third-party
||liveonlinetv247.com/images/muvixx-150x50-watch-now-in-hd-play-btn.gif
Examples of filters that could NOT be moved to a bidi-trie:
-adap.$domain=~l-adap.org
/tsc.php?*&ses=
||ibsrv.net/*forumsponsor$domain=[...]
@@||imgspice.com/jquery.cookie.js|$script
||view.atdmt.com^*/iview/$third-party
||postimg.cc/image/$csp=[...]
Ideally the filters above should be able to be moved to a
bidi-trie since they are basically plain patterns, or at
least partially moved to a bidi-trie when there is only a
single wildcard (i.e. made of two plain patterns).
Also, there were two distinct bidi-tries in which
plain-pattern filters can be moved to: one for patterns
without hostname anchoring and another one for patterns
with hostname-anchoring. This was required because the
hostname-anchored patterns have an extra condition which
is outside the bidi-trie knowledge.
This commit expands the number of filters which can be
stored in the bidi-trie, and also remove the need to
use two distinct bidi-tries.
- Added ability to associate a pattern with an integer
in the bidi-trie [1].
- The bidi-trie match code passes this externally
provided integer when calling an externally
provided method used for testing extra conditions
that may be present for a plain pattern found to
be matching in the bidi-trie.
- Decomposed existing filters into smaller logical units:
- FilterPlainLeftAnchored =>
FilterPatternPlain +
FilterAnchorLeft
- FilterPlainRightAnchored =>
FilterPatternPlain +
FilterAnchorRight
- FilterExactMatch =>
FilterPatternPlain +
FilterAnchorLeft +
FilterAnchorRight
- FilterPlainHnAnchored =>
FilterPatternPlain +
FilterAnchorHn
- FilterWildcard1 =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
]
- FilterWildcard1HnAnchored =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
] +
FilterAnchorHn
- FilterGenericHnAnchored =>
FilterPatternGeneric +
FilterAnchorHn
- FilterGenericHnAndRightAnchored =>
FilterPatternGeneric +
FilterAnchorRight +
FilterAnchorHn
- FilterOriginMixedSet =>
FilterOriginMissSet +
FilterOriginHitSet
- Instances of FilterOrigin[...], FilterDataHolder
can also be added to a composite filter to
represent `domain=` and `csp=` options.
- Added a new filter class, FilterComposite, for
filters which are a combination of two or more
logical units. A FilterComposite instance is a
match when *all* filters composing it are a
match.
Since filters are now encoded into combination of
smaller units, it becomes possible to extract the
FilterPatternPlain component and store it in the
bidi-trie, and use the integer as a handle for the
remaining extra conditions, if any.
Since a single pattern in the bidi-trie may be a
component for different filters, the associated
integer points to a sequence of extra conditions,
and a match occurs as soon as one of the extra
conditions (which may itself be a sequence of
conditions) is fulfilled.
Decomposing filters which are currently single
instance into sequences of smaller logical filters
means increasing the storage and CPU overhead when
evaluating such filters. The CPU overhead is
compensated by the fact that more filters can now
moved into the bidi-trie, where the first match is
efficiently evaluated. The extra conditions have to
be evaluated if and only if there is a match in the
bidi-trie.
The storage overhead is compensated by the
bidi-trie's intrinsic nature of merging similar
patterns.
Furthermore, the storage overhead is reduced by no
longer using JavaScript array to store collection
of filters (which is what FilterComposite is):
the same technique used in [2] is imported to store
sequences of filters.
A sequence of filters is a sequence of integer pairs
where the first integer is an index to an actual
filter instance stored in a global array of filters
(`filterUnits`), while the second integer is an index
to the next pair in the sequence -- which means all
sequences of filters are encoded in one single array
of integers (`filterSequences` => Uint32Array). As
a result, a sequence of filters can be represented by
one single integer -- an index to the first pair --
regardless of the number of filters in the sequence.
This representation is further leveraged to replace
the use of JavaScript array in FilterBucket [3],
which used a JavaScript array to store collection
of filters. Doing so means there is no more need for
FilterPair [4], which purpose was to be a lightweight
representation when there was only two filters in a
collection.
As a result of the above changes, the map of `token`
(integer) => filter instance (object) used to
associate tokens to filters or collections of filters
is replaced with a more efficient map of `token`
(integer) to filter unit index (integer) to lookup a
filter object from the global `filterUnits` array.
Another consequence of using one single global
array to store all filter instances means we can reuse
existing instances when a logical filter instance is
parameter-less, which is the case for FilterAnchorLeft,
FilterAnchorRight, FilterAnchorHn, the index to these
single instances is reused where needed.
`urlTokenizer` now stores the character codes of the
scanned URL into a bidi-trie buffer, for reuse when
string matching methods are called.
New method: `tokenHistogram()`, used to generate
histograms of occurrences of token extracted from URLs
in built-in benchmark. The top results of the "miss"
histogram are used as "bad tokens", i.e. tokens to
avoid if possible when compiling filter lists.
All plain pattern strings are now stored in the
bidi-trie memory buffer, regardless of whether they
will be used in the trie proper or not.
Three methods have been added to the bidi-trie to test
stored string against the URL which is also stored in
then bidi-trie.
FilterParser is now instanciated on demand and
released when no longer used.
***
[1] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/strie.js#L120
[2] https://github.com/gorhill/uBlock/commit/e94024d350b066e4e04a772b0a3dbc69daab3fb7
[3] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1630
[4] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1566
2019-10-21 14:15:58 +02:00
|
|
|
destroy();
|
2019-09-22 01:48:02 +02:00
|
|
|
return false;
|
2018-04-06 22:02:35 +02:00
|
|
|
};
|
|
|
|
|
2019-02-14 19:33:55 +01:00
|
|
|
const destroy = function() {
|
2021-07-29 01:48:38 +02:00
|
|
|
io.remove(/^selfie\//);
|
2019-10-27 16:49:05 +01:00
|
|
|
µb.selfieIsInvalid = true;
|
2019-09-15 15:36:50 +02:00
|
|
|
createTimer = vAPI.setTimeout(( ) => {
|
|
|
|
createTimer = undefined;
|
2019-02-14 19:33:55 +01:00
|
|
|
create();
|
|
|
|
}, µb.hiddenSettings.selfieAfter * 60000);
|
2018-06-01 13:54:31 +02:00
|
|
|
};
|
2015-11-29 23:06:58 +01:00
|
|
|
|
2019-09-15 15:36:50 +02:00
|
|
|
const destroyAsync = function() {
|
|
|
|
if ( destroyTimer !== undefined ) { return; }
|
|
|
|
if ( createTimer !== undefined ) {
|
|
|
|
clearTimeout(createTimer);
|
|
|
|
createTimer = undefined;
|
|
|
|
}
|
|
|
|
destroyTimer = vAPI.setTimeout(
|
|
|
|
( ) => {
|
|
|
|
destroyTimer = undefined;
|
|
|
|
destroy();
|
|
|
|
},
|
|
|
|
1019
|
|
|
|
);
|
2019-10-27 16:49:05 +01:00
|
|
|
µb.selfieIsInvalid = true;
|
2019-09-15 15:36:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return { load, destroy: destroyAsync };
|
2015-11-29 23:06:58 +01:00
|
|
|
})();
|
2014-09-08 23:46:58 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-07-27 16:10:34 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/531
|
|
|
|
// Overwrite user settings with admin settings if present.
|
|
|
|
//
|
|
|
|
// Admin settings match layout of a uBlock backup. Not all data is
|
|
|
|
// necessarily present, i.e. administrators may removed entries which
|
|
|
|
// values are left to the user's choice.
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.restoreAdminSettings = async function() {
|
2021-01-06 17:39:24 +01:00
|
|
|
let toOverwrite = {};
|
2019-09-15 13:58:28 +02:00
|
|
|
let data;
|
|
|
|
try {
|
2021-01-05 18:16:50 +01:00
|
|
|
const store = await vAPI.adminStorage.get([
|
|
|
|
'adminSettings',
|
2021-01-06 17:39:24 +01:00
|
|
|
'toOverwrite',
|
2021-01-05 18:16:50 +01:00
|
|
|
]) || {};
|
2021-01-06 17:39:24 +01:00
|
|
|
if ( store.toOverwrite instanceof Object ) {
|
|
|
|
toOverwrite = store.toOverwrite;
|
2021-01-05 18:16:50 +01:00
|
|
|
}
|
|
|
|
const json = store.adminSettings;
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof json === 'string' && json !== '' ) {
|
2019-09-15 13:58:28 +02:00
|
|
|
data = JSON.parse(json);
|
2020-04-08 15:57:55 +02:00
|
|
|
} else if ( json instanceof Object ) {
|
|
|
|
data = json;
|
2015-07-27 16:10:34 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
} catch (ex) {
|
|
|
|
console.error(ex);
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2021-01-05 18:16:50 +01:00
|
|
|
if ( data instanceof Object === false ) { data = {}; }
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const bin = {};
|
|
|
|
let binNotEmpty = false;
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/666
|
|
|
|
// Allows an admin to set their own 'assets.json' file, with their
|
|
|
|
// own set of stock assets.
|
|
|
|
if (
|
|
|
|
typeof data.assetsBootstrapLocation === 'string' &&
|
|
|
|
data.assetsBootstrapLocation !== ''
|
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.assetsBootstrapLocation = data.assetsBootstrapLocation;
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( typeof data.userSettings === 'object' ) {
|
2021-01-08 15:18:26 +01:00
|
|
|
const µbus = this.userSettings;
|
|
|
|
const adminus = data.userSettings;
|
|
|
|
for ( const name in µbus ) {
|
|
|
|
if ( µbus.hasOwnProperty(name) === false ) { continue; }
|
|
|
|
if ( adminus.hasOwnProperty(name) === false ) { continue; }
|
|
|
|
bin[name] = adminus[name];
|
2015-10-21 17:53:03 +02:00
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// 'selectedFilterLists' is an array of filter list tokens. Each token
|
2021-01-08 15:18:26 +01:00
|
|
|
// is a reference to an asset in 'assets.json', or a URL for lists not
|
|
|
|
// present in 'assets.json'.
|
|
|
|
if (
|
|
|
|
Array.isArray(toOverwrite.filterLists) &&
|
|
|
|
toOverwrite.filterLists.length !== 0
|
|
|
|
) {
|
2021-01-31 16:30:12 +01:00
|
|
|
const importedLists = [];
|
2021-01-08 15:18:26 +01:00
|
|
|
for ( const list of toOverwrite.filterLists ) {
|
|
|
|
if ( /^[a-z-]+:\/\//.test(list) === false ) { continue; }
|
2021-01-31 16:30:12 +01:00
|
|
|
importedLists.push(list);
|
2021-01-08 15:18:26 +01:00
|
|
|
}
|
2021-01-31 16:30:12 +01:00
|
|
|
if ( importedLists.length !== 0 ) {
|
|
|
|
bin.importedLists = importedLists;
|
|
|
|
bin.externalLists = importedLists.join('\n');
|
2021-01-08 15:18:26 +01:00
|
|
|
}
|
|
|
|
bin.selectedFilterLists = toOverwrite.filterLists;
|
|
|
|
binNotEmpty = true;
|
|
|
|
} else if ( Array.isArray(data.selectedFilterLists) ) {
|
2019-09-15 13:58:28 +02:00
|
|
|
bin.selectedFilterLists = data.selectedFilterLists;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2021-01-08 15:18:26 +01:00
|
|
|
if (
|
|
|
|
Array.isArray(toOverwrite.trustedSiteDirectives) &&
|
|
|
|
toOverwrite.trustedSiteDirectives.length !== 0
|
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.netWhitelistDefault = toOverwrite.trustedSiteDirectives.slice();
|
2021-01-06 17:39:24 +01:00
|
|
|
bin.netWhitelist = toOverwrite.trustedSiteDirectives.slice();
|
2021-01-05 18:16:50 +01:00
|
|
|
binNotEmpty = true;
|
|
|
|
} else if ( Array.isArray(data.whitelist) ) {
|
2019-09-15 13:58:28 +02:00
|
|
|
bin.netWhitelist = data.whitelist;
|
|
|
|
binNotEmpty = true;
|
|
|
|
} else if ( typeof data.netWhitelist === 'string' ) {
|
|
|
|
bin.netWhitelist = data.netWhitelist.split('\n');
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( typeof data.dynamicFilteringString === 'string' ) {
|
|
|
|
bin.dynamicFilteringString = data.dynamicFilteringString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( typeof data.urlFilteringString === 'string' ) {
|
|
|
|
bin.urlFilteringString = data.urlFilteringString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( typeof data.hostnameSwitchesString === 'string' ) {
|
|
|
|
bin.hostnameSwitchesString = data.hostnameSwitchesString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( binNotEmpty ) {
|
|
|
|
vAPI.storage.set(bin);
|
|
|
|
}
|
2015-10-21 17:53:03 +02:00
|
|
|
|
2021-01-10 18:31:31 +01:00
|
|
|
if (
|
|
|
|
Array.isArray(toOverwrite.filters) &&
|
|
|
|
toOverwrite.filters.length !== 0
|
|
|
|
) {
|
|
|
|
this.saveUserFilters(toOverwrite.filters.join('\n'));
|
|
|
|
} else if ( typeof data.userFilters === 'string' ) {
|
2019-09-15 13:58:28 +02:00
|
|
|
this.saveUserFilters(data.userFilters);
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-09 18:53:05 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2344
|
2022-03-12 19:50:46 +01:00
|
|
|
// Support multiple locales per filter list.
|
2017-11-09 18:53:05 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3210
|
|
|
|
// Support ability to auto-enable a filter list based on user agent.
|
2021-11-06 17:49:27 +01:00
|
|
|
// https://github.com/gorhill/uBlock/pull/3860
|
|
|
|
// Get current language using extensions API (instead of `navigator.language`)
|
2017-11-09 18:53:05 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.listMatchesEnvironment = function(details) {
|
2017-11-09 18:53:05 +01:00
|
|
|
// Matches language?
|
|
|
|
if ( typeof details.lang === 'string' ) {
|
2019-04-15 00:20:57 +02:00
|
|
|
let re = this.listMatchesEnvironment.reLang;
|
2017-11-15 22:32:52 +01:00
|
|
|
if ( re === undefined ) {
|
2021-11-06 17:49:27 +01:00
|
|
|
const match = /^[a-z]+/.exec(browser.i18n.getUILanguage());
|
2019-04-15 00:20:57 +02:00
|
|
|
if ( match !== null ) {
|
|
|
|
re = new RegExp('\\b' + match[0] + '\\b');
|
|
|
|
this.listMatchesEnvironment.reLang = re;
|
|
|
|
}
|
2017-11-09 18:53:05 +01:00
|
|
|
}
|
2019-04-15 00:20:57 +02:00
|
|
|
if ( re !== undefined && re.test(details.lang) ) { return true; }
|
2017-11-09 18:53:05 +01:00
|
|
|
}
|
|
|
|
// Matches user agent?
|
|
|
|
if ( typeof details.ua === 'string' ) {
|
2019-04-15 00:20:57 +02:00
|
|
|
let re = new RegExp('\\b' + this.escapeRegex(details.ua) + '\\b', 'i');
|
2017-11-09 18:53:05 +01:00
|
|
|
if ( re.test(self.navigator.userAgent) ) { return true; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.scheduleAssetUpdater = (( ) => {
|
2019-05-15 20:49:12 +02:00
|
|
|
let timer, next = 0;
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
return function(updateDelay) {
|
|
|
|
if ( timer ) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = undefined;
|
2014-09-08 23:46:58 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( updateDelay === 0 ) {
|
|
|
|
next = 0;
|
2015-02-24 00:31:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-15 20:49:12 +02:00
|
|
|
const now = Date.now();
|
2017-01-18 19:17:47 +01:00
|
|
|
// Use the new schedule if and only if it is earlier than the previous
|
|
|
|
// one.
|
|
|
|
if ( next !== 0 ) {
|
|
|
|
updateDelay = Math.min(updateDelay, Math.max(next - now, 0));
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
next = now + updateDelay;
|
2019-05-15 20:49:12 +02:00
|
|
|
timer = vAPI.setTimeout(( ) => {
|
2017-01-18 19:17:47 +01:00
|
|
|
timer = undefined;
|
|
|
|
next = 0;
|
2021-07-29 01:48:38 +02:00
|
|
|
io.updateStart({
|
2019-05-15 20:49:12 +02:00
|
|
|
delay: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 ||
|
2020-04-08 15:57:55 +02:00
|
|
|
120000,
|
|
|
|
auto: true,
|
2017-01-18 19:17:47 +01:00
|
|
|
});
|
|
|
|
}, updateDelay);
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
})();
|
2015-08-18 19:15:58 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.assetObserver = function(topic, details) {
|
2017-01-18 19:17:47 +01:00
|
|
|
// Do not update filter list if not in use.
|
2020-08-22 14:43:16 +02:00
|
|
|
// Also, ignore really bad lists, i.e. those which should not even be
|
|
|
|
// fetched from a remote server.
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( topic === 'before-asset-updated' ) {
|
2017-05-08 20:00:41 +02:00
|
|
|
if ( details.type === 'filters' ) {
|
|
|
|
if (
|
|
|
|
this.availableFilterLists.hasOwnProperty(details.assetKey) === false ||
|
2020-08-22 14:43:16 +02:00
|
|
|
this.selectedFilterLists.indexOf(details.assetKey) === -1 ||
|
|
|
|
this.badLists.get(details.assetKey)
|
2017-05-08 20:00:41 +02:00
|
|
|
) {
|
2017-12-28 19:49:02 +01:00
|
|
|
return;
|
2017-05-08 20:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
return true;
|
2015-08-18 19:15:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Compile the list while we have the raw version in memory
|
|
|
|
if ( topic === 'after-asset-updated' ) {
|
2019-02-14 19:33:55 +01:00
|
|
|
// Skip selfie-related content.
|
|
|
|
if ( details.assetKey.startsWith('selfie/') ) { return; }
|
2019-05-15 20:49:12 +02:00
|
|
|
const cached = typeof details.content === 'string' &&
|
|
|
|
details.content !== '';
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( this.availableFilterLists.hasOwnProperty(details.assetKey) ) {
|
|
|
|
if ( cached ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( this.selectedFilterLists.indexOf(details.assetKey) !== -1 ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
this.extractFilterListMetadata(
|
|
|
|
details.assetKey,
|
|
|
|
details.content
|
|
|
|
);
|
2020-08-21 17:57:20 +02:00
|
|
|
if ( this.badLists.has(details.assetKey) === false ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
io.put(
|
2020-08-21 17:57:20 +02:00
|
|
|
'compiled/' + details.assetKey,
|
2020-11-13 15:23:25 +01:00
|
|
|
this.compileFilters(details.content, {
|
|
|
|
assetKey: details.assetKey
|
|
|
|
})
|
2020-08-21 17:57:20 +02:00
|
|
|
);
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.removeCompiledFilterList(details.assetKey);
|
2015-09-13 16:26:36 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
} else if ( details.assetKey === this.pslAssetKey ) {
|
|
|
|
if ( cached ) {
|
|
|
|
this.compilePublicSuffixList(details.content);
|
2015-08-18 19:15:58 +02:00
|
|
|
}
|
2020-08-21 17:57:20 +02:00
|
|
|
} else if ( details.assetKey === 'ublock-badlists' ) {
|
2020-08-22 14:43:16 +02:00
|
|
|
this.badLists = new Map();
|
2015-08-18 19:15:58 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
vAPI.messaging.broadcast({
|
|
|
|
what: 'assetUpdated',
|
|
|
|
key: details.assetKey,
|
|
|
|
cached: cached
|
2015-08-18 19:15:58 +02:00
|
|
|
});
|
2017-05-06 19:19:05 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2585
|
2019-02-14 19:33:55 +01:00
|
|
|
// Whenever an asset is overwritten, the current selfie is quite
|
|
|
|
// likely no longer valid.
|
2017-05-06 19:19:05 +02:00
|
|
|
this.selfieManager.destroy();
|
2017-01-18 19:17:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-08-18 19:15:58 +02:00
|
|
|
|
2017-01-22 22:05:16 +01:00
|
|
|
// Update failed.
|
|
|
|
if ( topic === 'asset-update-failed' ) {
|
|
|
|
vAPI.messaging.broadcast({
|
|
|
|
what: 'assetUpdated',
|
|
|
|
key: details.assetKey,
|
|
|
|
failed: true
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Reload all filter lists if needed.
|
|
|
|
if ( topic === 'after-assets-updated' ) {
|
|
|
|
if ( details.assetKeys.length !== 0 ) {
|
2019-07-06 18:36:28 +02:00
|
|
|
// https://github.com/gorhill/uBlock/pull/2314#issuecomment-278716960
|
2019-07-08 14:41:28 +02:00
|
|
|
if (
|
|
|
|
this.hiddenSettings.userResourcesLocation !== 'unset' ||
|
|
|
|
vAPI.webextFlavor.soup.has('devbuild')
|
|
|
|
) {
|
2021-08-02 15:23:48 +02:00
|
|
|
redirectEngine.invalidateResourcesSelfie(io);
|
2019-07-06 18:36:28 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
this.loadFilterLists();
|
|
|
|
}
|
|
|
|
if ( this.userSettings.autoUpdate ) {
|
2017-02-05 13:43:28 +01:00
|
|
|
this.scheduleAssetUpdater(this.hiddenSettings.autoUpdatePeriod * 3600000 || 25200000);
|
2017-01-18 19:17:47 +01:00
|
|
|
} else {
|
|
|
|
this.scheduleAssetUpdater(0);
|
|
|
|
}
|
2017-01-23 15:35:05 +01:00
|
|
|
vAPI.messaging.broadcast({
|
|
|
|
what: 'assetsUpdated',
|
|
|
|
assetKeys: details.assetKeys
|
|
|
|
});
|
2017-01-18 19:17:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-20 21:17:11 +01:00
|
|
|
|
|
|
|
// New asset source became available, if it's a filter list, should we
|
|
|
|
// auto-select it?
|
|
|
|
if ( topic === 'builtin-asset-source-added' ) {
|
|
|
|
if ( details.entry.content === 'filters' ) {
|
|
|
|
if (
|
|
|
|
details.entry.off !== true ||
|
2017-11-09 18:53:05 +01:00
|
|
|
this.listMatchesEnvironment(details.entry)
|
2017-01-20 21:17:11 +01:00
|
|
|
) {
|
|
|
|
this.saveSelectedFilterLists([ details.assetKey ], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-08-18 19:15:58 +02:00
|
|
|
};
|