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-02-15 23:25:38 +01:00
|
|
|
Copyright (C) 2014-2018 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
|
|
|
|
*/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
/* global objectAssign, punycode, publicSuffixList */
|
2014-11-17 00:27:48 +01:00
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-07 05:36:09 +01:00
|
|
|
µBlock.getBytesInUse = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = this.noopFunc;
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
var getBytesInUseHandler = function(bytesInUse) {
|
|
|
|
µBlock.storageUsed = bytesInUse;
|
2015-03-07 05:36:09 +01:00
|
|
|
callback(bytesInUse);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2016-10-30 18:06:23 +01:00
|
|
|
// Not all platforms implement this method.
|
|
|
|
if ( vAPI.storage.getBytesInUse instanceof Function ) {
|
|
|
|
vAPI.storage.getBytesInUse(null, getBytesInUseHandler);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-10 08:17:12 +02:00
|
|
|
µBlock.keyvalSetOne = function(key, val, callback) {
|
|
|
|
var bin = {};
|
|
|
|
bin[key] = val;
|
|
|
|
vAPI.storage.set(bin, callback || this.noopFunc);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
µBlock.saveLocalSettings = (function() {
|
|
|
|
var saveAfter = 4 * 60 * 1000;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
var save = function() {
|
2016-10-08 16:15:31 +02:00
|
|
|
this.localSettingsLastSaved = Date.now();
|
2015-11-29 23:06:58 +01:00
|
|
|
vAPI.storage.set(this.localSettings);
|
2015-11-29 23:57:55 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
var onTimeout = function() {
|
|
|
|
var µb = µBlock;
|
2016-10-08 16:15:31 +02:00
|
|
|
if ( µb.localSettingsLastModified > µb.localSettingsLastSaved ) {
|
2015-11-29 23:57:55 +01:00
|
|
|
save.call(µb);
|
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);
|
|
|
|
|
|
|
|
return save;
|
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.saveUserSettings = function() {
|
2015-01-22 03:46:11 +01:00
|
|
|
vAPI.storage.set(this.userSettings);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-02-21 19:29:36 +01:00
|
|
|
µBlock.loadHiddenSettings = function() {
|
|
|
|
var onLoaded = function(bin) {
|
|
|
|
if ( bin instanceof Object === false ) { return; }
|
|
|
|
var µb = µBlock,
|
|
|
|
hs = bin.hiddenSettings;
|
|
|
|
// Remove following condition once 1.15.12+ is widespread.
|
|
|
|
if (
|
|
|
|
hs instanceof Object === false &&
|
|
|
|
typeof bin.hiddenSettingsString === 'string'
|
|
|
|
) {
|
|
|
|
vAPI.storage.remove('hiddenSettingsString');
|
|
|
|
hs = µBlock.hiddenSettingsFromString(bin.hiddenSettingsString);
|
|
|
|
}
|
|
|
|
if ( hs instanceof Object ) {
|
|
|
|
var hsDefault = µb.hiddenSettingsDefault;
|
|
|
|
for ( var key in hsDefault ) {
|
|
|
|
if (
|
|
|
|
hsDefault.hasOwnProperty(key) &&
|
|
|
|
hs.hasOwnProperty(key) &&
|
|
|
|
typeof hs[key] === typeof hsDefault[key]
|
|
|
|
) {
|
|
|
|
µb.hiddenSettings[key] = hs[key];
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 23:46:29 +02:00
|
|
|
// To remove once 1.15.26 is widespread. The reason is to ensure
|
|
|
|
// the change in the following commit is taken into account:
|
|
|
|
// https://github.com/gorhill/uBlock/commit/8071321e9104
|
|
|
|
if ( hs.manualUpdateAssetFetchPeriod === 2000 ) {
|
|
|
|
µb.hiddenSettings.manualUpdateAssetFetchPeriod =
|
|
|
|
µb.hiddenSettingsDefault.manualUpdateAssetFetchPeriod;
|
|
|
|
hs.manualUpdateAssetFetchPeriod = undefined;
|
|
|
|
µb.saveHiddenSettings();
|
|
|
|
}
|
2018-02-21 19:29:36 +01:00
|
|
|
}
|
|
|
|
if ( vAPI.localStorage.getItem('immediateHiddenSettings') === null ) {
|
|
|
|
µb.saveImmediateHiddenSettings();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.storage.get(
|
|
|
|
[ 'hiddenSettings', 'hiddenSettingsString'],
|
|
|
|
onLoaded
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-04-09 23:46:29 +02:00
|
|
|
// Note: Save only the settings which values differ from the default ones.
|
|
|
|
// This way the new default values in the future will properly apply for those
|
|
|
|
// which were not modified by the user.
|
|
|
|
|
2018-02-21 19:29:36 +01:00
|
|
|
µBlock.saveHiddenSettings = function(callback) {
|
2018-04-09 23:46:29 +02:00
|
|
|
var bin = { hiddenSettings: {} };
|
|
|
|
for ( var prop in this.hiddenSettings ) {
|
|
|
|
if (
|
|
|
|
this.hiddenSettings.hasOwnProperty(prop) &&
|
|
|
|
this.hiddenSettings[prop] !== this.hiddenSettingsDefault[prop]
|
|
|
|
) {
|
|
|
|
bin.hiddenSettings[prop] = this.hiddenSettings[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vAPI.storage.set(bin, callback);
|
2018-02-21 19:29:36 +01:00
|
|
|
this.saveImmediateHiddenSettings();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
µBlock.hiddenSettingsFromString = function(raw) {
|
|
|
|
var out = objectAssign({}, this.hiddenSettingsDefault),
|
|
|
|
lineIter = new this.LineIterator(raw),
|
|
|
|
line, matches, name, value;
|
|
|
|
while ( lineIter.eot() === false ) {
|
|
|
|
line = lineIter.next();
|
|
|
|
matches = /^\s*(\S+)\s+(.+)$/.exec(line);
|
|
|
|
if ( matches === null || matches.length !== 3 ) { continue; }
|
|
|
|
name = matches[1];
|
|
|
|
if ( out.hasOwnProperty(name) === false ) { continue; }
|
|
|
|
value = matches[2];
|
|
|
|
switch ( typeof out[name] ) {
|
|
|
|
case 'boolean':
|
|
|
|
if ( value === 'true' ) {
|
|
|
|
out[name] = true;
|
|
|
|
} else if ( value === 'false' ) {
|
|
|
|
out[name] = false;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-06 22:27:21 +01:00
|
|
|
case 'string':
|
|
|
|
out[name] = value;
|
|
|
|
break;
|
2017-01-18 19:17:47 +01:00
|
|
|
case 'number':
|
|
|
|
out[name] = parseInt(value, 10);
|
|
|
|
if ( isNaN(out[name]) ) {
|
|
|
|
out[name] = this.hiddenSettingsDefault[name];
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 16:20:47 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 19:29:36 +01:00
|
|
|
return out;
|
2016-11-03 16:20:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.stringFromHiddenSettings = function() {
|
|
|
|
var out = [],
|
2018-02-21 19:29:36 +01:00
|
|
|
keys = Object.keys(this.hiddenSettings).sort();
|
|
|
|
for ( var key of keys ) {
|
2016-11-03 16:20:47 +01:00
|
|
|
out.push(key + ' ' + this.hiddenSettings[key]);
|
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-02-21 19:29:36 +01:00
|
|
|
// These settings must be available immediately on startup, without delay
|
|
|
|
// through the vAPI.localStorage. Add/remove settings as needed.
|
|
|
|
|
|
|
|
µBlock.saveImmediateHiddenSettings = function() {
|
|
|
|
vAPI.localStorage.setItem(
|
|
|
|
'immediateHiddenSettings',
|
|
|
|
JSON.stringify({
|
|
|
|
suspendTabsUntilReady: this.hiddenSettings.suspendTabsUntilReady,
|
|
|
|
userResourcesLocation: this.hiddenSettings.userResourcesLocation
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do this here to have these hidden settings loaded ASAP.
|
|
|
|
µBlock.loadHiddenSettings();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
µBlock.savePermanentFirewallRules = function() {
|
2015-04-10 08:17:12 +02:00
|
|
|
this.keyvalSetOne('dynamicFilteringString', this.permanentFirewall.toString());
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
µBlock.savePermanentURLFilteringRules = function() {
|
|
|
|
this.keyvalSetOne('urlFilteringString', this.permanentURLFiltering.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
µBlock.saveHostnameSwitches = function() {
|
2015-04-10 08:17:12 +02:00
|
|
|
this.keyvalSetOne('hostnameSwitchesString', this.hnSwitches.toString());
|
2014-12-31 23:26:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-25 02:52:34 +02:00
|
|
|
µBlock.saveWhitelist = function() {
|
2015-08-11 21:29:14 +02:00
|
|
|
this.keyvalSetOne('netWhitelist', this.stringFromWhitelist(this.netWhitelist));
|
2014-08-25 02:52:34 +02:00
|
|
|
this.netWhitelistModifyTime = Date.now();
|
|
|
|
};
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
/*******************************************************************************
|
2014-08-25 02:52:34 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
TODO(seamless migration):
|
|
|
|
The code related to 'remoteBlacklist' can be removed when I am confident
|
|
|
|
all users have moved to a version of uBO which no longer depends on
|
|
|
|
the property 'remoteBlacklists, i.e. v1.11 and beyond.
|
2015-03-30 14:12:41 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
**/
|
2015-03-30 14:12:41 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.loadSelectedFilterLists = function(callback) {
|
|
|
|
var µb = this;
|
2017-12-14 22:42:54 +01:00
|
|
|
vAPI.storage.get('selectedFilterLists', function(bin) {
|
|
|
|
// Select default filter lists if first-time launch.
|
|
|
|
if ( !bin || Array.isArray(bin.selectedFilterLists) === false ) {
|
2017-01-26 16:17:38 +01:00
|
|
|
µb.assets.metadata(function(availableLists) {
|
2018-01-01 13:52:03 +01:00
|
|
|
µb.saveSelectedFilterLists(
|
|
|
|
µb.autoSelectRegionalFilterLists(availableLists)
|
|
|
|
);
|
2017-01-26 16:17:38 +01:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return;
|
2015-03-30 14:12:41 +02:00
|
|
|
}
|
2018-01-01 13:52:03 +01:00
|
|
|
// TODO: Removes once 1.1.15 is in widespread use.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3383
|
|
|
|
vAPI.storage.remove('remoteBlacklists');
|
2017-12-14 22:42:54 +01:00
|
|
|
µb.selectedFilterLists = bin.selectedFilterLists;
|
2017-01-26 16:17:38 +01:00
|
|
|
callback();
|
2017-01-18 19:17:47 +01:00
|
|
|
});
|
|
|
|
};
|
2015-03-30 14:12:41 +02:00
|
|
|
|
2017-02-03 14:12:26 +01:00
|
|
|
µBlock.saveSelectedFilterLists = function(newKeys, append, callback) {
|
|
|
|
if ( typeof append === 'function' ) {
|
|
|
|
callback = append;
|
|
|
|
append = false;
|
|
|
|
}
|
2017-01-26 16:17:38 +01:00
|
|
|
var oldKeys = this.selectedFilterLists.slice();
|
|
|
|
if ( append ) {
|
|
|
|
newKeys = newKeys.concat(oldKeys);
|
|
|
|
}
|
|
|
|
var newSet = new Set(newKeys);
|
|
|
|
// Purge unused filter lists from cache.
|
|
|
|
for ( var i = 0, n = oldKeys.length; i < n; i++ ) {
|
|
|
|
if ( newSet.has(oldKeys[i]) === false ) {
|
|
|
|
this.removeFilterList(oldKeys[i]);
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
2017-01-26 16:17:38 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
newKeys = this.arrayFrom(newSet);
|
2017-01-26 16:17:38 +01:00
|
|
|
var bin = {
|
2017-12-14 22:42:54 +01:00
|
|
|
selectedFilterLists: newKeys
|
2017-01-26 16:17:38 +01:00
|
|
|
};
|
|
|
|
this.selectedFilterLists = newKeys;
|
2017-02-03 14:12:26 +01:00
|
|
|
vAPI.storage.set(bin, callback);
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
|
|
|
|
2015-03-30 14:12:41 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-22 22:05:16 +01:00
|
|
|
µBlock.applyFilterListSelection = function(details, callback) {
|
|
|
|
var µb = this,
|
|
|
|
selectedListKeySet = new Set(this.selectedFilterLists),
|
|
|
|
externalLists = this.userSettings.externalLists,
|
|
|
|
i, n, assetKey;
|
|
|
|
|
|
|
|
// Filter lists to select
|
|
|
|
if ( Array.isArray(details.toSelect) ) {
|
|
|
|
if ( details.merge ) {
|
|
|
|
for ( i = 0, n = details.toSelect.length; i < n; i++ ) {
|
|
|
|
selectedListKeySet.add(details.toSelect[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedListKeySet = new Set(details.toSelect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imported filter lists to remove
|
|
|
|
if ( Array.isArray(details.toRemove) ) {
|
|
|
|
var removeURLFromHaystack = function(haystack, needle) {
|
|
|
|
return haystack.replace(
|
|
|
|
new RegExp(
|
|
|
|
'(^|\\n)' +
|
2017-11-10 12:56:38 +01:00
|
|
|
µb.escapeRegex(needle) +
|
2017-01-22 22:05:16 +01:00
|
|
|
'(\\n|$)', 'g'),
|
|
|
|
'\n'
|
|
|
|
).trim();
|
|
|
|
};
|
|
|
|
for ( i = 0, n = details.toRemove.length; i < n; i++ ) {
|
|
|
|
assetKey = details.toRemove[i];
|
|
|
|
selectedListKeySet.delete(assetKey);
|
|
|
|
externalLists = removeURLFromHaystack(externalLists, assetKey);
|
|
|
|
this.removeFilterList(assetKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter lists to import
|
|
|
|
if ( typeof details.toImport === 'string' ) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1181
|
|
|
|
// Try mapping the URL of an imported filter list to the assetKey of an
|
|
|
|
// existing stock list.
|
|
|
|
var assetKeyFromURL = function(url) {
|
|
|
|
var needle = url.replace(/^https?:/, '');
|
|
|
|
var assets = µb.availableFilterLists, asset;
|
|
|
|
for ( var assetKey in assets ) {
|
|
|
|
asset = assets[assetKey];
|
|
|
|
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; }
|
|
|
|
for ( i = 0, n = asset.contentURL.length; i < n; i++ ) {
|
|
|
|
if ( asset.contentURL[i].endsWith(needle) ) {
|
|
|
|
return assetKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
var importedSet = new Set(this.listKeysFromCustomFilterLists(externalLists)),
|
2017-05-19 16:12:55 +02:00
|
|
|
toImportSet = new Set(this.listKeysFromCustomFilterLists(details.toImport));
|
|
|
|
for ( var urlKey of toImportSet ) {
|
|
|
|
if ( importedSet.has(urlKey) ) { continue; }
|
|
|
|
assetKey = assetKeyFromURL(urlKey);
|
|
|
|
if ( assetKey === urlKey ) {
|
|
|
|
importedSet.add(urlKey);
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
selectedListKeySet.add(assetKey);
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
externalLists = this.arrayFrom(importedSet).sort().join('\n');
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
var result = this.arrayFrom(selectedListKeySet);
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( externalLists !== this.userSettings.externalLists ) {
|
|
|
|
this.userSettings.externalLists = externalLists;
|
|
|
|
vAPI.storage.set({ externalLists: externalLists });
|
|
|
|
}
|
|
|
|
this.saveSelectedFilterLists(result);
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback(result);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.listKeysFromCustomFilterLists = function(raw) {
|
|
|
|
var out = new Set(),
|
|
|
|
reIgnore = /^[!#]/,
|
|
|
|
reValid = /^[a-z-]+:\/\/\S+/,
|
|
|
|
lineIter = new this.LineIterator(raw),
|
|
|
|
location;
|
|
|
|
while ( lineIter.eot() === false ) {
|
|
|
|
location = lineIter.next().trim();
|
|
|
|
if ( reIgnore.test(location) || !reValid.test(location) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.add(location);
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
return this.arrayFrom(out);
|
2017-01-22 22:05:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
µBlock.saveUserFilters = function(content, callback) {
|
2015-12-07 14:59:22 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1022
|
|
|
|
// Be sure to end with an empty line.
|
|
|
|
content = content.trim();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( content !== '' ) { content += '\n'; }
|
2015-08-11 21:29:14 +02:00
|
|
|
this.assets.put(this.userFiltersPath, content, callback);
|
2017-01-18 19:17:47 +01:00
|
|
|
this.removeCompiledFilterList(this.userFiltersPath);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.loadUserFilters = function(callback) {
|
|
|
|
return this.assets.get(this.userFiltersPath, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-27 00:31:51 +02:00
|
|
|
µBlock.appendUserFilters = function(filters) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( filters.length === 0 ) { return; }
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
var µb = this;
|
|
|
|
|
2015-04-27 02:33:49 +02:00
|
|
|
var onSaved = function() {
|
2017-01-18 19:17:47 +01:00
|
|
|
var compiledFilters = µb.compileFilters(filters),
|
|
|
|
snfe = µb.staticNetFilteringEngine,
|
|
|
|
cfe = µb.cosmeticFilteringEngine,
|
|
|
|
acceptedCount = snfe.acceptedCount + cfe.acceptedCount,
|
|
|
|
discardedCount = snfe.discardedCount + cfe.discardedCount;
|
2016-02-17 15:28:20 +01:00
|
|
|
µb.applyCompiledFilters(compiledFilters, true);
|
2017-01-18 19:17:47 +01:00
|
|
|
var entry = µb.availableFilterLists[µb.userFiltersPath],
|
|
|
|
deltaEntryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount,
|
|
|
|
deltaEntryUsedCount = deltaEntryCount - (snfe.discardedCount + cfe.discardedCount - discardedCount);
|
2015-04-27 00:31:51 +02:00
|
|
|
entry.entryCount += deltaEntryCount;
|
|
|
|
entry.entryUsedCount += deltaEntryUsedCount;
|
2017-01-18 19:17:47 +01:00
|
|
|
vAPI.storage.set({ 'availableFilterLists': µb.availableFilterLists });
|
2015-02-24 00:31:29 +01:00
|
|
|
µb.staticNetFilteringEngine.freeze();
|
2015-11-29 17:04:42 +01:00
|
|
|
µb.redirectEngine.freeze();
|
2017-12-28 19:49:02 +01:00
|
|
|
µb.staticExtFilteringEngine.freeze();
|
2017-05-06 19:19:05 +02:00
|
|
|
µb.selfieManager.destroy();
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
var onLoaded = function(details) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( details.error ) { return; }
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/976
|
2015-03-10 04:00:52 +01:00
|
|
|
// 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.
|
2015-04-27 00:31:51 +02:00
|
|
|
µb.saveUserFilters(details.content.trim() + '\n\n' + filters.trim(), onSaved);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
this.loadUserFilters(onLoaded);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.autoSelectRegionalFilterLists = function(lists) {
|
2017-01-27 19:44:52 +01:00
|
|
|
var selectedListKeys = [ this.userFiltersPath ],
|
2017-01-18 19:17:47 +01:00
|
|
|
list;
|
|
|
|
for ( var key in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(key) === false ) { continue; }
|
|
|
|
list = lists[key];
|
|
|
|
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
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.getAvailableLists = function(callback) {
|
|
|
|
var µb = this,
|
|
|
|
oldAvailableLists = {},
|
|
|
|
newAvailableLists = {};
|
|
|
|
|
|
|
|
// User filter list.
|
|
|
|
newAvailableLists[this.userFiltersPath] = {
|
2018-04-09 21:45:25 +02:00
|
|
|
group: 'user',
|
2017-01-18 19:17:47 +01:00
|
|
|
title: vAPI.i18n('1pPageName')
|
|
|
|
};
|
|
|
|
|
|
|
|
// Custom filter lists.
|
|
|
|
var importedListKeys = this.listKeysFromCustomFilterLists(µb.userSettings.externalLists),
|
|
|
|
i = importedListKeys.length, listKey, entry;
|
|
|
|
while ( i-- ) {
|
|
|
|
listKey = importedListKeys[i];
|
|
|
|
entry = {
|
|
|
|
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',
|
|
|
|
title: ''
|
|
|
|
};
|
|
|
|
newAvailableLists[listKey] = entry;
|
|
|
|
this.assets.registerAssetSource(listKey, entry);
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:08:46 +02:00
|
|
|
// Convert a no longer existing stock list into an imported list.
|
|
|
|
var customListFromStockList = function(assetKey) {
|
|
|
|
var oldEntry = oldAvailableLists[assetKey];
|
|
|
|
if ( oldEntry === undefined || oldEntry.off === true ) { return; }
|
|
|
|
var listURL = oldEntry.contentURL;
|
|
|
|
if ( Array.isArray(listURL) ) {
|
|
|
|
listURL = listURL[0];
|
|
|
|
}
|
|
|
|
var newEntry = {
|
|
|
|
content: 'filters',
|
|
|
|
contentURL: listURL,
|
|
|
|
external: true,
|
|
|
|
group: 'custom',
|
|
|
|
submitter: 'user',
|
|
|
|
title: oldEntry.title || ''
|
|
|
|
};
|
|
|
|
newAvailableLists[listURL] = newEntry;
|
|
|
|
µb.assets.registerAssetSource(listURL, newEntry);
|
|
|
|
importedListKeys.push(listURL);
|
|
|
|
µb.userSettings.externalLists += '\n' + listURL;
|
|
|
|
µb.userSettings.externalLists = µb.userSettings.externalLists.trim();
|
|
|
|
vAPI.storage.set({ externalLists: µb.userSettings.externalLists });
|
|
|
|
µb.saveSelectedFilterLists([ listURL ], true);
|
|
|
|
};
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Final steps:
|
|
|
|
// - reuse existing list metadata if any;
|
|
|
|
// - unregister unreferenced imported filter lists if any.
|
|
|
|
var finalize = function() {
|
|
|
|
var assetKey, newEntry, oldEntry;
|
|
|
|
|
|
|
|
// Reuse existing metadata.
|
|
|
|
for ( assetKey in oldAvailableLists ) {
|
|
|
|
oldEntry = oldAvailableLists[assetKey];
|
|
|
|
newEntry = newAvailableLists[assetKey];
|
2017-07-03 15:08:46 +02:00
|
|
|
// List no longer exists. If a stock list, try to convert to
|
|
|
|
// imported list if it was selected.
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( newEntry === undefined ) {
|
|
|
|
µb.removeFilterList(assetKey);
|
2017-07-03 15:08:46 +02:00
|
|
|
if ( assetKey.indexOf('://') === -1 ) {
|
|
|
|
customListFromStockList(assetKey);
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
continue;
|
2015-08-18 17:44:24 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( oldEntry.entryCount !== undefined ) {
|
|
|
|
newEntry.entryCount = oldEntry.entryCount;
|
2014-08-25 02:52:34 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( oldEntry.entryUsedCount !== undefined ) {
|
|
|
|
newEntry.entryUsedCount = oldEntry.entryUsedCount;
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
2015-03-11 04:46:18 +01:00
|
|
|
// This may happen if the list name was pulled from the list
|
|
|
|
// content.
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/982
|
2015-03-11 04:46:18 +01:00
|
|
|
// There is no guarantee the title was successfully extracted from
|
|
|
|
// the list content.
|
2017-01-18 19:17:47 +01:00
|
|
|
if (
|
|
|
|
newEntry.title === '' &&
|
|
|
|
typeof oldEntry.title === 'string' &&
|
|
|
|
oldEntry.title !== ''
|
2015-03-11 04:46:18 +01:00
|
|
|
) {
|
2017-01-18 19:17:47 +01:00
|
|
|
newEntry.title = oldEntry.title;
|
2014-09-26 02:21:21 +02:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2016-01-03 19:58:25 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Remove unreferenced imported filter lists.
|
|
|
|
var dict = new Set(importedListKeys);
|
|
|
|
for ( assetKey in newAvailableLists ) {
|
|
|
|
newEntry = newAvailableLists[assetKey];
|
|
|
|
if ( newEntry.submitter !== 'user' ) { continue; }
|
|
|
|
if ( dict.has(assetKey) ) { continue; }
|
|
|
|
delete newAvailableLists[assetKey];
|
|
|
|
µb.assets.unregisterAssetSource(assetKey);
|
|
|
|
µb.removeFilterList(assetKey);
|
2016-01-03 19:58:25 +01:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2017-01-26 16:17:38 +01:00
|
|
|
// Built-in filter lists loaded.
|
2017-01-18 19:17:47 +01:00
|
|
|
var onBuiltinListsLoaded = function(entries) {
|
|
|
|
for ( var assetKey in entries ) {
|
|
|
|
if ( entries.hasOwnProperty(assetKey) === false ) { continue; }
|
|
|
|
entry = entries[assetKey];
|
|
|
|
if ( entry.content !== 'filters' ) { continue; }
|
|
|
|
newAvailableLists[assetKey] = objectAssign({}, entry);
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Load set of currently selected filter lists.
|
2017-01-26 16:17:38 +01:00
|
|
|
var listKeySet = new Set(µb.selectedFilterLists);
|
|
|
|
for ( listKey in newAvailableLists ) {
|
|
|
|
if ( newAvailableLists.hasOwnProperty(listKey) ) {
|
|
|
|
newAvailableLists[listKey].off = !listKeySet.has(listKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finalize();
|
|
|
|
callback(newAvailableLists);
|
2017-01-18 19:17:47 +01:00
|
|
|
};
|
2016-01-03 19:58:25 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Available lists previously computed.
|
|
|
|
var onOldAvailableListsLoaded = function(bin) {
|
|
|
|
oldAvailableLists = bin && bin.availableFilterLists || {};
|
|
|
|
µb.assets.metadata(onBuiltinListsLoaded);
|
|
|
|
};
|
2016-01-03 19:58:25 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Load previously saved available lists -- these contains data
|
|
|
|
// computed at run-time, we will reuse this data if possible.
|
|
|
|
vAPI.storage.get('availableFilterLists', onOldAvailableListsLoaded);
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-03 19:58:25 +01:00
|
|
|
// This is used to be re-entrancy resistant.
|
|
|
|
µBlock.loadingFilterLists = false;
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
µBlock.loadFilterLists = function(callback) {
|
2016-01-03 19:58:25 +01:00
|
|
|
// Callers are expected to check this first.
|
|
|
|
if ( this.loadingFilterLists ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.loadingFilterLists = true;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2017-01-20 21:17:11 +01:00
|
|
|
var µb = this,
|
|
|
|
filterlistsCount = 0,
|
|
|
|
loadedListKeys = [];
|
2014-07-25 22:12:20 +02:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = this.noopFunc;
|
|
|
|
}
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onDone = function() {
|
2014-12-28 16:07:43 +01:00
|
|
|
µb.staticNetFilteringEngine.freeze();
|
2017-12-28 19:49:02 +01:00
|
|
|
µb.staticExtFilteringEngine.freeze();
|
2015-11-24 01:18:25 +01:00
|
|
|
µb.redirectEngine.freeze();
|
2017-01-18 19:17:47 +01:00
|
|
|
vAPI.storage.set({ 'availableFilterLists': µb.availableFilterLists });
|
2015-02-24 19:48:03 +01:00
|
|
|
|
2017-01-20 21:17:11 +01:00
|
|
|
vAPI.messaging.broadcast({
|
|
|
|
what: 'staticFilteringDataChanged',
|
|
|
|
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
|
|
|
|
ignoreGenericCosmeticFilters: µb.userSettings.ignoreGenericCosmeticFilters,
|
|
|
|
listKeys: loadedListKeys
|
|
|
|
});
|
2016-09-24 20:36:08 +02:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
callback();
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2017-05-06 19:19:05 +02:00
|
|
|
µb.selfieManager.destroy();
|
2016-01-03 19:58:25 +01:00
|
|
|
µb.loadingFilterLists = false;
|
2014-07-25 22:12:20 +02:00
|
|
|
};
|
|
|
|
|
2017-01-20 21:17:11 +01:00
|
|
|
var applyCompiledFilters = function(assetKey, compiled) {
|
|
|
|
var snfe = µb.staticNetFilteringEngine,
|
2017-12-29 19:31:37 +01:00
|
|
|
sxfe = µb.staticExtFilteringEngine,
|
|
|
|
acceptedCount = snfe.acceptedCount + sxfe.acceptedCount,
|
|
|
|
discardedCount = snfe.discardedCount + sxfe.discardedCount;
|
2017-01-20 21:17:11 +01:00
|
|
|
µb.applyCompiledFilters(compiled, assetKey === µb.userFiltersPath);
|
|
|
|
if ( µb.availableFilterLists.hasOwnProperty(assetKey) ) {
|
|
|
|
var entry = µb.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
|
|
|
};
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onCompiledListLoaded = function(details) {
|
2017-01-18 19:17:47 +01:00
|
|
|
applyCompiledFilters(details.assetKey, details.content);
|
2015-02-24 00:31:29 +01:00
|
|
|
filterlistsCount -= 1;
|
|
|
|
if ( filterlistsCount === 0 ) {
|
|
|
|
onDone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var onFilterListsReady = function(lists) {
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.availableFilterLists = lists;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2015-11-24 01:18:25 +01:00
|
|
|
µb.redirectEngine.reset();
|
2017-12-28 19:49:02 +01:00
|
|
|
µb.staticExtFilteringEngine.reset();
|
2015-02-24 00:31:29 +01:00
|
|
|
µb.staticNetFilteringEngine.reset();
|
2015-11-29 23:06:58 +01:00
|
|
|
µb.selfieManager.destroy();
|
2015-06-11 18:12:23 +02:00
|
|
|
µb.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:
|
|
|
|
// This happens for assets which do not exist, ot assets with no
|
|
|
|
// content.
|
|
|
|
var toLoad = [];
|
2017-01-18 19:17:47 +01:00
|
|
|
for ( var assetKey in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(assetKey) === false ) { continue; }
|
|
|
|
if ( lists[assetKey].off ) { continue; }
|
|
|
|
toLoad.push(assetKey);
|
2015-02-06 07:20:04 +01:00
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
filterlistsCount = toLoad.length;
|
|
|
|
if ( filterlistsCount === 0 ) {
|
2016-01-03 19:58:25 +01:00
|
|
|
return onDone();
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var i = toLoad.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
µb.getCompiledFilterList(toLoad[i], onCompiledListLoaded);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
this.getAvailableLists(onFilterListsReady);
|
2015-12-16 03:34:36 +01:00
|
|
|
this.loadRedirectResources();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.getCompiledFilterList = function(assetKey, callback) {
|
|
|
|
var µb = this,
|
2017-05-20 21:32:03 +02:00
|
|
|
compiledPath = 'compiled/' + assetKey,
|
|
|
|
rawContent;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-05-20 21:32:03 +02:00
|
|
|
var onCompiledListLoaded2 = function(details) {
|
|
|
|
if ( details.content === '' ) {
|
|
|
|
details.content = µb.compileFilters(rawContent);
|
|
|
|
µb.assets.put(compiledPath, details.content);
|
|
|
|
}
|
|
|
|
rawContent = undefined;
|
2017-01-18 19:17:47 +01:00
|
|
|
details.assetKey = assetKey;
|
2017-05-20 21:32:03 +02:00
|
|
|
callback(details);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onRawListLoaded = function(details) {
|
2015-06-08 14:09:08 +02:00
|
|
|
if ( details.content === '' ) {
|
2017-05-20 21:32:03 +02:00
|
|
|
details.assetKey = assetKey;
|
2015-06-08 14:09:08 +02:00
|
|
|
callback(details);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.extractFilterListMetadata(assetKey, details.content);
|
2017-05-20 21:32:03 +02:00
|
|
|
// Fectching the raw content may cause the compiled content to be
|
|
|
|
// generated somewhere else in uBO, hence we try one last time to
|
|
|
|
// fetch the compiled content in case it has become available.
|
|
|
|
rawContent = details.content;
|
|
|
|
µb.assets.get(compiledPath, onCompiledListLoaded2);
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2017-05-20 21:32:03 +02:00
|
|
|
var onCompiledListLoaded1 = function(details) {
|
2015-02-24 00:31:29 +01:00
|
|
|
if ( details.content === '' ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.assets.get(assetKey, onRawListLoaded);
|
2015-02-24 00:31:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
details.assetKey = assetKey;
|
2015-02-24 00:31:29 +01:00
|
|
|
callback(details);
|
|
|
|
};
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2017-05-20 21:32:03 +02:00
|
|
|
this.assets.get(compiledPath, onCompiledListLoaded1);
|
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.
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.extractFilterListMetadata = function(assetKey, raw) {
|
|
|
|
var listEntry = this.availableFilterLists[assetKey];
|
|
|
|
if ( listEntry === undefined ) { return; }
|
|
|
|
// Metadata expected to be found at the top of content.
|
|
|
|
var head = raw.slice(0, 1024),
|
|
|
|
matches, v;
|
|
|
|
// https://github.com/gorhill/uBlock/issues/313
|
|
|
|
// Always try to fetch the name if this is an external filter list.
|
|
|
|
if ( listEntry.title === '' || listEntry.group === 'custom' ) {
|
2018-04-06 22:15:10 +02:00
|
|
|
matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Title:([^\n]+)/i);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( matches !== null ) {
|
2017-05-26 18:46:36 +02:00
|
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=2869
|
|
|
|
// JSON.stringify/JSON.parse is to work around String.slice()
|
|
|
|
// potentially causing the whole raw filter list to be held in
|
|
|
|
// memory just because we cut out the title as a substring.
|
|
|
|
listEntry.title = JSON.parse(JSON.stringify(matches[1].trim()));
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Extract update frequency information
|
2018-04-06 21:59:01 +02:00
|
|
|
matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Expires:[\t ]*(\d+)[\t ]*day/i);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( matches !== null ) {
|
2018-02-23 12:42:17 +01:00
|
|
|
v = Math.max(parseInt(matches[1], 10), 1);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( v !== listEntry.updateAfter ) {
|
|
|
|
this.assets.registerAssetSource(assetKey, { updateAfter: v });
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 21:44:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-02-25 22:33:51 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.removeCompiledFilterList = function(assetKey) {
|
|
|
|
this.assets.remove('compiled/' + assetKey);
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.removeFilterList = function(assetKey) {
|
|
|
|
this.removeCompiledFilterList(assetKey);
|
|
|
|
this.assets.remove(assetKey);
|
2015-02-25 22:33:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.compileFilters = function(rawText) {
|
2017-12-28 19:49:02 +01:00
|
|
|
var writer = new this.CompiledLineWriter();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
// Useful references:
|
|
|
|
// https://adblockplus.org/en/filter-cheatsheet
|
|
|
|
// https://adblockplus.org/en/filters
|
2017-01-18 19:17:47 +01:00
|
|
|
var staticNetFilteringEngine = this.staticNetFilteringEngine,
|
2017-12-28 19:49:02 +01:00
|
|
|
staticExtFilteringEngine = this.staticExtFilteringEngine,
|
2017-01-18 19:17:47 +01:00
|
|
|
reIsWhitespaceChar = /\s/,
|
|
|
|
reMaybeLocalIp = /^[\d:f]/,
|
2018-04-11 17:28:10 +02:00
|
|
|
reIsLocalhostRedirect = /\s+(?:0\.0\.0\.0|broadcasthost|ip6-all(?:nodes|routers)|local|localhost|localhost\.localdomain)\b/,
|
2017-01-18 19:17:47 +01:00
|
|
|
reLocalIp = /^(?:0\.0\.0\.0|127\.0\.0\.1|::1|fe80::1%lo0)/,
|
2017-12-28 19:49:02 +01:00
|
|
|
line, c, pos,
|
2018-04-05 13:29:15 +02:00
|
|
|
lineIter = new this.LineIterator(this.processDirectives(rawText));
|
2017-01-18 19:17:47 +01:00
|
|
|
|
|
|
|
while ( lineIter.eot() === false ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
// rhill 2014-04-18: The trim is important here, as without it there
|
|
|
|
// could be a lingering `\r` which would cause problems in the
|
|
|
|
// following parsing code.
|
2018-04-05 13:29:15 +02:00
|
|
|
line = lineIter.next().trim();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( line.length === 0 ) { continue; }
|
2015-01-23 17:32:49 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
// Strip comments
|
|
|
|
c = line.charAt(0);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( c === '!' || c === '[' ) { continue; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-09-25 21:44:18 +02:00
|
|
|
// Parse or skip cosmetic filters
|
2015-01-23 17:32:49 +01:00
|
|
|
// All cosmetic filters are caught here
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( staticExtFilteringEngine.compile(line, writer) ) { continue; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-01-23 17:32:49 +01:00
|
|
|
// Whatever else is next can be assumed to not be a cosmetic filter
|
|
|
|
|
|
|
|
// Most comments start in first column
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( c === '#' ) { continue; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-01-23 17:32:49 +01:00
|
|
|
// Catch comments somewhere on the line
|
|
|
|
// Remove:
|
|
|
|
// ... #blah blah blah
|
|
|
|
// ... # blah blah blah
|
|
|
|
// Don't remove:
|
|
|
|
// ...#blah blah blah
|
|
|
|
// because some ABP filters uses the `#` character (URL fragment)
|
|
|
|
pos = line.indexOf('#');
|
|
|
|
if ( pos !== -1 && reIsWhitespaceChar.test(line.charAt(pos - 1)) ) {
|
|
|
|
line = line.slice(0, pos).trim();
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/15
|
|
|
|
// Ensure localhost et al. don't end up in the ubiquitous blacklist.
|
2015-01-23 17:32:49 +01:00
|
|
|
// With hosts files, we need to remove local IP redirection
|
|
|
|
if ( reMaybeLocalIp.test(c) ) {
|
|
|
|
// Ignore hosts file redirect configuration
|
|
|
|
// 127.0.0.1 localhost
|
|
|
|
// 255.255.255.255 broadcasthost
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( reIsLocalhostRedirect.test(line) ) { continue; }
|
2015-01-23 17:32:49 +01:00
|
|
|
line = line.replace(reLocalIp, '').trim();
|
|
|
|
}
|
2015-03-07 19:20:18 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( line.length === 0 ) { continue; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
staticNetFilteringEngine.compile(line, writer);
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
return writer.toString();
|
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.
|
|
|
|
|
|
|
|
µBlock.applyCompiledFilters = function(rawText, firstparty) {
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( rawText === '' ) { return; }
|
2017-12-28 19:49:02 +01:00
|
|
|
var reader = new this.CompiledLineReader(rawText);
|
2017-05-25 23:46:59 +02:00
|
|
|
this.staticNetFilteringEngine.fromCompiledContent(reader);
|
2017-12-28 19:49:02 +01:00
|
|
|
this.staticExtFilteringEngine.fromCompiledContent(reader, {
|
|
|
|
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
|
|
|
|
|
|
|
|
µBlock.processDirectives = function(content) {
|
|
|
|
var reIf = /^!#(if|endif)\b([^\n]*)/gm,
|
|
|
|
parts = [],
|
|
|
|
beg = 0, depth = 0, discard = false;
|
|
|
|
while ( beg < content.length ) {
|
|
|
|
var match = reIf.exec(content);
|
|
|
|
if ( match === null ) { break; }
|
|
|
|
if ( match[1] === 'if' ) {
|
|
|
|
var expr = match[2].trim();
|
|
|
|
var target = expr.startsWith('!');
|
|
|
|
if ( target ) { expr = expr.slice(1); }
|
|
|
|
var token = this.processDirectives.tokens.get(expr);
|
|
|
|
if (
|
|
|
|
depth === 0 &&
|
|
|
|
discard === false &&
|
|
|
|
token !== undefined &&
|
|
|
|
vAPI.webextFlavor.soup.has(token) === target
|
|
|
|
) {
|
|
|
|
parts.push(content.slice(beg, match.index));
|
|
|
|
discard = true;
|
|
|
|
}
|
|
|
|
depth += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
depth -= 1;
|
|
|
|
if ( depth < 0 ) { break; }
|
|
|
|
if ( depth === 0 && discard ) {
|
|
|
|
beg = match.index + match[0].length + 1;
|
|
|
|
discard = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( depth === 0 && parts.length !== 0 ) {
|
|
|
|
parts.push(content.slice(beg));
|
|
|
|
content = parts.join('\n');
|
|
|
|
}
|
|
|
|
return content.trim();
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.processDirectives.tokens = new Map([
|
2018-04-11 12:34:13 +02:00
|
|
|
[ 'ext_ublock', 'ublock' ],
|
|
|
|
[ 'env_chromium', 'chromium' ],
|
|
|
|
[ 'env_edge', 'edge' ],
|
|
|
|
[ 'env_firefox', 'firefox' ],
|
|
|
|
[ 'env_mobile', 'mobile' ],
|
|
|
|
[ 'env_safari', 'safari' ],
|
|
|
|
[ 'cap_html_filtering', 'html_filtering' ],
|
|
|
|
[ 'cap_user_stylesheet', 'user_stylesheet' ]
|
2018-04-05 13:29:15 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-02-15 23:25:38 +01:00
|
|
|
µBlock.loadRedirectResources = function(updatedContent) {
|
2017-03-05 18:54:47 +01:00
|
|
|
var µb = this,
|
|
|
|
content = '';
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2017-03-05 18:54:47 +01:00
|
|
|
var onDone = function() {
|
|
|
|
µb.redirectEngine.resourcesFromString(content);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onUserResourcesLoaded = function(details) {
|
|
|
|
if ( details.content !== '' ) {
|
|
|
|
content += '\n\n' + details.content;
|
|
|
|
}
|
|
|
|
onDone();
|
|
|
|
};
|
|
|
|
|
2015-11-24 05:34:03 +01:00
|
|
|
var onResourcesLoaded = function(details) {
|
2015-11-23 13:52:50 +01:00
|
|
|
if ( details.content !== '' ) {
|
2017-03-05 18:54:47 +01:00
|
|
|
content = details.content;
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
2017-03-05 18:54:47 +01:00
|
|
|
if ( µb.hiddenSettings.userResourcesLocation === 'unset' ) {
|
|
|
|
return onDone();
|
|
|
|
}
|
|
|
|
µb.assets.fetchText(µb.hiddenSettings.userResourcesLocation, onUserResourcesLoaded);
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
2018-02-15 23:25:38 +01:00
|
|
|
if ( typeof updatedContent === 'string' && updatedContent.length !== 0 ) {
|
|
|
|
return onResourcesLoaded({ content: updatedContent });
|
|
|
|
}
|
|
|
|
|
|
|
|
var onSelfieReady = function(success) {
|
|
|
|
if ( success !== true ) {
|
|
|
|
µb.assets.get('ublock-resources', onResourcesLoaded);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
µb.redirectEngine.resourcesFromSelfie(onSelfieReady);
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-25 02:52:34 +02:00
|
|
|
µBlock.loadPublicSuffixList = function(callback) {
|
2017-01-18 19:17:47 +01:00
|
|
|
var µb = this,
|
|
|
|
assetKey = µb.pslAssetKey,
|
|
|
|
compiledAssetKey = 'compiled/' + assetKey;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = this.noopFunc;
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
var onRawListLoaded = function(details) {
|
|
|
|
if ( details.content !== '' ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.compilePublicSuffixList(details.content);
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
var onCompiledListLoaded = function(details) {
|
2018-04-05 21:22:19 +02:00
|
|
|
var selfie;
|
|
|
|
try {
|
|
|
|
selfie = JSON.parse(details.content);
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
selfie === undefined ||
|
|
|
|
publicSuffixList.fromSelfie(selfie) === false
|
|
|
|
) {
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.assets.get(assetKey, onRawListLoaded);
|
2015-02-24 00:31:29 +01:00
|
|
|
return;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-09-08 23:46:58 +02:00
|
|
|
callback();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
this.assets.get(compiledAssetKey, onCompiledListLoaded);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.compilePublicSuffixList = function(content) {
|
|
|
|
publicSuffixList.parse(content, punycode.toASCII);
|
|
|
|
this.assets.put(
|
|
|
|
'compiled/' + this.pslAssetKey,
|
|
|
|
JSON.stringify(publicSuffixList.toSelfie())
|
|
|
|
);
|
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
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
µBlock.selfieManager = (function() {
|
|
|
|
var timer = null;
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
var create = function() {
|
|
|
|
timer = null;
|
|
|
|
var selfie = {
|
2017-05-06 19:19:05 +02:00
|
|
|
magic: this.systemSettings.selfieMagic,
|
|
|
|
availableFilterLists: this.availableFilterLists,
|
|
|
|
staticNetFilteringEngine: this.staticNetFilteringEngine.toSelfie(),
|
|
|
|
redirectEngine: this.redirectEngine.toSelfie(),
|
2017-12-28 19:49:02 +01:00
|
|
|
staticExtFilteringEngine: this.staticExtFilteringEngine.toSelfie()
|
2015-11-29 23:06:58 +01:00
|
|
|
};
|
2016-10-28 14:40:38 +02:00
|
|
|
vAPI.cacheStorage.set({ selfie: selfie });
|
2017-05-06 19:19:05 +02:00
|
|
|
}.bind(µBlock);
|
2015-11-29 23:06:58 +01:00
|
|
|
|
2018-04-06 22:02:35 +02:00
|
|
|
var load = function(callback) {
|
|
|
|
vAPI.cacheStorage.get('selfie', function(bin) {
|
|
|
|
var µb = µBlock;
|
|
|
|
if (
|
|
|
|
bin instanceof Object === false ||
|
|
|
|
bin.selfie instanceof Object === false ||
|
|
|
|
bin.selfie.magic !== µb.systemSettings.selfieMagic ||
|
|
|
|
bin.selfie.redirectEngine === undefined
|
|
|
|
) {
|
|
|
|
return callback(false);
|
|
|
|
}
|
|
|
|
µb.availableFilterLists = bin.selfie.availableFilterLists;
|
|
|
|
µb.staticNetFilteringEngine.fromSelfie(bin.selfie.staticNetFilteringEngine);
|
|
|
|
µb.redirectEngine.fromSelfie(bin.selfie.redirectEngine);
|
|
|
|
µb.staticExtFilteringEngine.fromSelfie(bin.selfie.staticExtFilteringEngine);
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-29 23:06:58 +01:00
|
|
|
var destroy = function() {
|
|
|
|
if ( timer !== null ) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
2016-10-28 14:40:38 +02:00
|
|
|
vAPI.cacheStorage.remove('selfie');
|
2017-05-06 19:19:05 +02:00
|
|
|
timer = vAPI.setTimeout(create, this.selfieAfter);
|
|
|
|
}.bind(µBlock);
|
2015-11-29 23:06:58 +01:00
|
|
|
|
|
|
|
return {
|
2018-04-06 22:02:35 +02:00
|
|
|
load: load,
|
2015-11-29 23:06:58 +01:00
|
|
|
destroy: destroy
|
|
|
|
};
|
|
|
|
})();
|
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.
|
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
µBlock.restoreAdminSettings = function(callback) {
|
2016-10-15 15:32:33 +02:00
|
|
|
// Support for vAPI.adminStorage is optional (webext).
|
|
|
|
if ( vAPI.adminStorage instanceof Object === false ) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
var onRead = function(json) {
|
|
|
|
var µb = µBlock;
|
|
|
|
var data;
|
|
|
|
if ( typeof json === 'string' && json !== '' ) {
|
|
|
|
try {
|
|
|
|
data = JSON.parse(json);
|
|
|
|
} catch (ex) {
|
|
|
|
console.error(ex);
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
}
|
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data !== 'object' || data === null ) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
var bin = {};
|
|
|
|
var binNotEmpty = false;
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Allows an admin to set their own 'assets.json' file, with their own
|
|
|
|
// set of stock assets.
|
|
|
|
if ( typeof data.assetsBootstrapLocation === 'string' ) {
|
|
|
|
bin.assetsBootstrapLocation = data.assetsBootstrapLocation;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.userSettings === 'object' ) {
|
|
|
|
for ( var name in µb.userSettings ) {
|
|
|
|
if ( µb.userSettings.hasOwnProperty(name) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( data.userSettings.hasOwnProperty(name) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bin[name] = data.userSettings[name];
|
|
|
|
binNotEmpty = true;
|
2015-07-27 16:10:34 +02:00
|
|
|
}
|
2015-10-21 17:53:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// 'selectedFilterLists' is an array of filter list tokens. Each token
|
|
|
|
// is a reference to an asset in 'assets.json'.
|
|
|
|
if ( Array.isArray(data.selectedFilterLists) ) {
|
|
|
|
bin.selectedFilterLists = data.selectedFilterLists;
|
|
|
|
binNotEmpty = true;
|
2015-07-27 16:10:34 +02:00
|
|
|
}
|
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.netWhitelist === 'string' ) {
|
|
|
|
bin.netWhitelist = data.netWhitelist;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.dynamicFilteringString === 'string' ) {
|
|
|
|
bin.dynamicFilteringString = data.dynamicFilteringString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.urlFilteringString === 'string' ) {
|
|
|
|
bin.urlFilteringString = data.urlFilteringString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.hostnameSwitchesString === 'string' ) {
|
|
|
|
bin.hostnameSwitchesString = data.hostnameSwitchesString;
|
|
|
|
binNotEmpty = true;
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( binNotEmpty ) {
|
|
|
|
vAPI.storage.set(bin);
|
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
if ( typeof data.userFilters === 'string' ) {
|
2016-02-17 15:28:20 +01:00
|
|
|
µb.assets.put(µb.userFiltersPath, data.userFilters);
|
2015-10-21 17:53:03 +02:00
|
|
|
}
|
2015-07-27 16:10:34 +02:00
|
|
|
|
2015-10-21 17:53:03 +02:00
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.adminStorage.getItem('adminSettings', onRead);
|
2015-07-27 16:10:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-09 18:53:05 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2344
|
|
|
|
// Support mutliple locales per filter list.
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3210
|
|
|
|
// Support ability to auto-enable a filter list based on user agent.
|
|
|
|
|
|
|
|
µBlock.listMatchesEnvironment = function(details) {
|
|
|
|
var re;
|
|
|
|
// Matches language?
|
|
|
|
if ( typeof details.lang === 'string' ) {
|
2017-11-15 22:32:52 +01:00
|
|
|
re = this.listMatchesEnvironment.reLang;
|
|
|
|
if ( re === undefined ) {
|
2017-11-09 18:53:05 +01:00
|
|
|
re = new RegExp('\\b' + self.navigator.language.slice(0, 2) + '\\b');
|
|
|
|
this.listMatchesEnvironment.reLang = re;
|
|
|
|
}
|
|
|
|
if ( re.test(details.lang) ) { return true; }
|
|
|
|
}
|
|
|
|
// Matches user agent?
|
|
|
|
if ( typeof details.ua === 'string' ) {
|
|
|
|
re = new RegExp('\\b' + this.escapeRegex(details.ua) + '\\b', 'i');
|
|
|
|
if ( re.test(self.navigator.userAgent) ) { return true; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.scheduleAssetUpdater = (function() {
|
|
|
|
var timer, next = 0;
|
|
|
|
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;
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
var now = Date.now();
|
|
|
|
// 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;
|
|
|
|
timer = vAPI.setTimeout(function() {
|
|
|
|
timer = undefined;
|
|
|
|
next = 0;
|
|
|
|
var µb = µBlock;
|
|
|
|
µb.assets.updateStart({
|
|
|
|
delay: µb.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 || 120000
|
|
|
|
});
|
|
|
|
}, updateDelay);
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
})();
|
2015-08-18 19:15:58 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
µBlock.assetObserver = function(topic, details) {
|
|
|
|
// Do not update filter list if not in use.
|
|
|
|
if ( topic === 'before-asset-updated' ) {
|
2017-05-08 20:00:41 +02:00
|
|
|
if ( details.type === 'filters' ) {
|
|
|
|
if (
|
|
|
|
this.availableFilterLists.hasOwnProperty(details.assetKey) === false ||
|
|
|
|
this.selectedFilterLists.indexOf(details.assetKey) === -1
|
|
|
|
) {
|
2017-12-28 19:49:02 +01:00
|
|
|
return;
|
2017-05-08 20:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2594
|
|
|
|
if ( details.assetKey === 'ublock-resources' ) {
|
|
|
|
if (
|
|
|
|
this.hiddenSettings.ignoreRedirectFilters === true &&
|
|
|
|
this.hiddenSettings.ignoreScriptInjectFilters === true
|
|
|
|
) {
|
2017-12-28 19:49:02 +01:00
|
|
|
return;
|
2017-05-08 20:00:41 +02:00
|
|
|
}
|
2015-08-18 19:15:58 +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' ) {
|
|
|
|
var cached = typeof details.content === 'string' && details.content !== '';
|
|
|
|
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
|
|
|
|
);
|
|
|
|
this.assets.put(
|
|
|
|
'compiled/' + details.assetKey,
|
|
|
|
this.compileFilters(details.content)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
} else if ( details.assetKey === 'ublock-resources' ) {
|
2018-02-15 23:25:38 +01:00
|
|
|
this.redirectEngine.invalidateResourcesSelfie();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( cached ) {
|
2018-02-15 23:25:38 +01:00
|
|
|
this.loadRedirectResources(details.content);
|
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
|
|
|
|
// Whenever an asset is overwritten, the current selfie is quite
|
|
|
|
// likely no longer valid.
|
|
|
|
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 ) {
|
|
|
|
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
|
|
|
};
|