2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-03-07 19:20:18 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2015-02-13 18:10:10 +01:00
|
|
|
Copyright (C) 2014-2015 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
|
|
|
|
*/
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
/* global YaMD5, µBlock, vAPI, 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
|
|
|
};
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.storage.getBytesInUse(null, getBytesInUseHandler);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-11 16:05:13 +01:00
|
|
|
µBlock.saveLocalSettings = function(force) {
|
|
|
|
if ( force ) {
|
|
|
|
this.localSettingsModifyTime = Date.now();
|
2014-10-07 14:59:35 +02:00
|
|
|
}
|
2015-03-05 01:38:19 +01:00
|
|
|
if ( this.localSettingsModifyTime <= this.localSettingsSaveTime ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.localSettingsSaveTime = Date.now();
|
2015-03-11 16:05:13 +01:00
|
|
|
vAPI.storage.set(this.localSettings);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Save local settings regularly. Not critical.
|
|
|
|
|
|
|
|
µBlock.asyncJobs.add(
|
|
|
|
'autoSaveLocalSettings',
|
|
|
|
null,
|
|
|
|
µBlock.saveLocalSettings.bind(µBlock),
|
2015-02-24 19:48:03 +01:00
|
|
|
4 * 60 * 1000,
|
2014-06-24 00:42:43 +02:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.saveUserSettings = function() {
|
2015-01-22 03:46:11 +01:00
|
|
|
vAPI.storage.set(this.userSettings);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 06:26:45 +01:00
|
|
|
µBlock.savePermanentFirewallRules = function() {
|
|
|
|
this.userSettings.dynamicFilteringString = this.permanentFirewall.toString();
|
2014-12-31 23:26:17 +01:00
|
|
|
this.XAL.keyvalSetOne('dynamicFilteringString', this.userSettings.dynamicFilteringString);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-25 02:52:34 +02:00
|
|
|
µBlock.saveWhitelist = function() {
|
|
|
|
var bin = {
|
|
|
|
'netWhitelist': this.stringFromWhitelist(this.netWhitelist)
|
|
|
|
};
|
2015-01-22 03:46:11 +01:00
|
|
|
vAPI.storage.set(bin);
|
2014-08-25 02:52:34 +02:00
|
|
|
this.netWhitelistModifyTime = Date.now();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
µBlock.saveUserFilters = function(content, callback) {
|
|
|
|
return this.assets.put(this.userFiltersPath, content, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.loadUserFilters = function(callback) {
|
|
|
|
return this.assets.get(this.userFiltersPath, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-10 04:00:52 +01:00
|
|
|
µBlock.appendUserFilters = function(filter) {
|
|
|
|
if ( filter.length === 0 ) {
|
2015-02-24 00:31:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
var µb = this;
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onCompiledListLoaded = function(details) {
|
|
|
|
var snfe = µb.staticNetFilteringEngine;
|
|
|
|
var cfe = µb.cosmeticFilteringEngine;
|
|
|
|
var acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
|
|
|
|
var duplicateCount = snfe.duplicateCount + cfe.duplicateCount;
|
|
|
|
µb.applyCompiledFilters(details.content);
|
|
|
|
var entry = µb.remoteBlacklists[µb.userFiltersPath];
|
|
|
|
entry.entryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount;
|
|
|
|
entry.entryUsedCount = entry.entryCount - snfe.duplicateCount - cfe.duplicateCount + duplicateCount;
|
|
|
|
µb.staticNetFilteringEngine.freeze();
|
|
|
|
µb.cosmeticFilteringEngine.freeze();
|
|
|
|
};
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
var onSaved = function(details) {
|
|
|
|
if ( details.error ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
µb.getCompiledFilterList(µb.userFiltersPath, onCompiledListLoaded);
|
2014-07-13 02:32:44 +02:00
|
|
|
};
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
var onLoaded = function(details) {
|
|
|
|
if ( details.error ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-10 04:00:52 +01:00
|
|
|
// https://github.com/gorhill/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.
|
|
|
|
µb.saveUserFilters(details.content.trim() + '\n\n' + filter.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
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-25 22:12:20 +02:00
|
|
|
µBlock.getAvailableLists = function(callback) {
|
|
|
|
var availableLists = {};
|
2015-02-25 22:33:51 +01:00
|
|
|
var relocationMap = {};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-25 22:51:04 +01:00
|
|
|
var fixLocation = function(location) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/418
|
|
|
|
// We now support built-in external filter lists
|
|
|
|
if ( /^https?:/.test(location) === false ) {
|
|
|
|
location = 'assets/thirdparties/' + location;
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
};
|
|
|
|
|
2014-07-25 22:12:20 +02:00
|
|
|
// selected lists
|
|
|
|
var onSelectedListsLoaded = function(store) {
|
2014-08-25 02:52:34 +02:00
|
|
|
var µb = µBlock;
|
2014-07-25 22:12:20 +02:00
|
|
|
var lists = store.remoteBlacklists;
|
|
|
|
var locations = Object.keys(lists);
|
2014-11-17 00:27:48 +01:00
|
|
|
var location, availableEntry, storedEntry;
|
2014-07-25 22:12:20 +02:00
|
|
|
|
2014-11-17 00:27:48 +01:00
|
|
|
while ( location = locations.pop() ) {
|
2015-02-25 22:33:51 +01:00
|
|
|
storedEntry = lists[location];
|
|
|
|
// New location?
|
|
|
|
if ( relocationMap.hasOwnProperty(location) ) {
|
|
|
|
µb.purgeFilterList(location);
|
|
|
|
location = relocationMap[location];
|
|
|
|
}
|
2014-11-17 00:27:48 +01:00
|
|
|
availableEntry = availableLists[location];
|
2014-08-25 02:52:34 +02:00
|
|
|
if ( availableEntry === undefined ) {
|
2015-02-25 22:33:51 +01:00
|
|
|
µb.purgeFilterList(location);
|
2014-07-25 22:12:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-25 02:52:34 +02:00
|
|
|
availableEntry.off = storedEntry.off || false;
|
2014-11-17 00:27:48 +01:00
|
|
|
µb.assets.setHomeURL(location, availableEntry.homeURL);
|
2014-08-25 02:52:34 +02:00
|
|
|
if ( storedEntry.entryCount !== undefined ) {
|
|
|
|
availableEntry.entryCount = storedEntry.entryCount;
|
|
|
|
}
|
|
|
|
if ( storedEntry.entryUsedCount !== undefined ) {
|
|
|
|
availableEntry.entryUsedCount = storedEntry.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.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/982
|
|
|
|
// There is no guarantee the title was successfully extracted from
|
|
|
|
// the list content.
|
|
|
|
if ( availableEntry.title === '' &&
|
|
|
|
typeof storedEntry.title === 'string' &&
|
|
|
|
storedEntry.title !== ''
|
|
|
|
) {
|
2014-09-26 02:21:21 +02:00
|
|
|
availableEntry.title = storedEntry.title;
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-07-25 22:12:20 +02:00
|
|
|
callback(availableLists);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2014-07-25 22:12:20 +02:00
|
|
|
// built-in lists
|
|
|
|
var onBuiltinListsLoaded = function(details) {
|
|
|
|
var location, locations;
|
|
|
|
try {
|
|
|
|
locations = JSON.parse(details.content);
|
|
|
|
} catch (e) {
|
|
|
|
locations = {};
|
|
|
|
}
|
2014-12-17 14:02:37 +01:00
|
|
|
var entry;
|
2014-07-25 22:12:20 +02:00
|
|
|
for ( location in locations ) {
|
|
|
|
if ( locations.hasOwnProperty(location) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-17 14:02:37 +01:00
|
|
|
entry = locations[location];
|
2015-02-25 22:51:04 +01:00
|
|
|
location = fixLocation(location);
|
2015-02-25 22:33:51 +01:00
|
|
|
// Migrate obsolete location to new location, if any
|
|
|
|
if ( typeof entry.oldLocation === 'string' ) {
|
2015-02-25 22:51:04 +01:00
|
|
|
entry.oldLocation = fixLocation(entry.oldLocation);
|
2015-02-25 22:33:51 +01:00
|
|
|
relocationMap[entry.oldLocation] = location;
|
|
|
|
}
|
2014-12-17 14:02:37 +01:00
|
|
|
availableLists[location] = entry;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-07-25 22:12:20 +02:00
|
|
|
|
|
|
|
// Now get user's selection of lists
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.storage.get(
|
2014-07-25 22:12:20 +02:00
|
|
|
{ 'remoteBlacklists': availableLists },
|
2014-10-17 21:44:19 +02:00
|
|
|
onSelectedListsLoaded
|
2014-06-24 00:42:43 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-07-25 22:12:20 +02:00
|
|
|
// permanent lists
|
|
|
|
var location;
|
|
|
|
var lists = this.permanentLists;
|
|
|
|
for ( location in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(location) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
availableLists[location] = lists[location];
|
|
|
|
}
|
|
|
|
|
|
|
|
// custom lists
|
|
|
|
var c;
|
|
|
|
var locations = this.userSettings.externalLists.split('\n');
|
|
|
|
for ( var i = 0; i < locations.length; i++ ) {
|
|
|
|
location = locations[i].trim();
|
|
|
|
c = location.charAt(0);
|
|
|
|
if ( location === '' || c === '!' || c === '#' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Coarse validation
|
|
|
|
if ( /[^0-9A-Za-z!*'();:@&=+$,\/?%#\[\]_.~-]/.test(location) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
availableLists[location] = {
|
|
|
|
title: '',
|
|
|
|
group: 'custom',
|
|
|
|
external: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// get built-in block lists.
|
|
|
|
this.assets.get('assets/ublock/filter-lists.json', onBuiltinListsLoaded);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.createShortUniqueId = function(path) {
|
|
|
|
var md5 = YaMD5.hashStr(path);
|
|
|
|
return md5.slice(0, 4) + md5.slice(-4);
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.createShortUniqueId.idLength = 8;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
µBlock.loadFilterLists = function(callback) {
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
|
|
//quickProfiler.start('µBlock.loadFilterLists()');
|
|
|
|
|
2014-07-25 22:12:20 +02:00
|
|
|
var µb = this;
|
2015-02-24 00:31:29 +01:00
|
|
|
var filterlistsCount = 0;
|
2014-07-25 22:12:20 +02:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = this.noopFunc;
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:52:20 +01:00
|
|
|
// Never fetch from remote servers when we load filter lists: this has to
|
|
|
|
// be as fast as possible.
|
|
|
|
µb.assets.remoteFetchBarrier += 1;
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onDone = function() {
|
2015-03-11 19:52:20 +01:00
|
|
|
// Remove barrier to remote fetching
|
|
|
|
µb.assets.remoteFetchBarrier -= 1;
|
|
|
|
|
2014-12-28 16:07:43 +01:00
|
|
|
µb.staticNetFilteringEngine.freeze();
|
2014-09-08 23:46:58 +02:00
|
|
|
µb.cosmeticFilteringEngine.freeze();
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.storage.set({ 'remoteBlacklists': µb.remoteBlacklists });
|
2015-02-24 19:48:03 +01:00
|
|
|
|
|
|
|
//quickProfiler.stop(0);
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
vAPI.messaging.broadcast({ what: 'allFilterListsReloaded' });
|
2014-09-08 23:46:58 +02:00
|
|
|
callback();
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
|
|
µb.toSelfieAsync();
|
2014-07-25 22:12:20 +02:00
|
|
|
};
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var applyCompiledFilters = function(path, compiled) {
|
|
|
|
var snfe = µb.staticNetFilteringEngine;
|
|
|
|
var cfe = µb.cosmeticFilteringEngine;
|
|
|
|
var acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
|
|
|
|
var duplicateCount = snfe.duplicateCount + cfe.duplicateCount;
|
|
|
|
µb.applyCompiledFilters(compiled);
|
|
|
|
if ( µb.remoteBlacklists.hasOwnProperty(path) ) {
|
|
|
|
var entry = µb.remoteBlacklists[path];
|
|
|
|
entry.entryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount;
|
2015-03-02 17:01:21 +01:00
|
|
|
entry.entryUsedCount = entry.entryCount - (snfe.duplicateCount + cfe.duplicateCount - duplicateCount);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onCompiledListLoaded = function(details) {
|
|
|
|
applyCompiledFilters(details.path, details.content);
|
|
|
|
filterlistsCount -= 1;
|
|
|
|
if ( filterlistsCount === 0 ) {
|
|
|
|
onDone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var onFilterListsReady = function(lists) {
|
2014-07-25 22:12:20 +02:00
|
|
|
µb.remoteBlacklists = lists;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
µb.cosmeticFilteringEngine.reset();
|
2015-02-24 00:31:29 +01:00
|
|
|
µb.staticNetFilteringEngine.reset();
|
2014-09-08 23:46:58 +02:00
|
|
|
µb.destroySelfie();
|
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 = [];
|
|
|
|
for ( var path in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(path) === false ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
if ( lists[path].off ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
toLoad.push(path);
|
2015-02-06 07:20:04 +01:00
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
filterlistsCount = toLoad.length;
|
|
|
|
if ( filterlistsCount === 0 ) {
|
|
|
|
onDone();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.getCompiledFilterListPath = function(path) {
|
|
|
|
return 'cache://compiled-filter-list:' + this.createShortUniqueId(path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.getCompiledFilterList = function(path, callback) {
|
|
|
|
var compiledPath = this.getCompiledFilterListPath(path);
|
|
|
|
var µb = this;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onRawListLoaded = function(details) {
|
|
|
|
if ( details.content !== '' ) {
|
2015-02-25 00:29:58 +01:00
|
|
|
var listMeta = µb.remoteBlacklists[path];
|
|
|
|
if ( listMeta && listMeta.title === '' ) {
|
|
|
|
var matches = details.content.slice(0, 1024).match(/(?:^|\n)!\s*Title:([^\n]+)/i);
|
|
|
|
if ( matches !== null ) {
|
|
|
|
listMeta.title = matches[1].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
//console.debug('µBlock.getCompiledFilterList/onRawListLoaded: compiling "%s"', path);
|
|
|
|
details.content = µb.compileFilters(details.content);
|
|
|
|
µb.assets.put(compiledPath, details.content);
|
|
|
|
}
|
|
|
|
callback(details);
|
|
|
|
};
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
var onCompiledListLoaded = function(details) {
|
|
|
|
if ( details.content === '' ) {
|
|
|
|
//console.debug('µBlock.getCompiledFilterList/onCompiledListLoaded: no compiled version for "%s"', path);
|
|
|
|
µb.assets.get(path, onRawListLoaded);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//console.debug('µBlock.getCompiledFilterList/onCompiledListLoaded: using compiled version for "%s"', path);
|
|
|
|
details.path = path;
|
|
|
|
callback(details);
|
|
|
|
};
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
this.assets.get(compiledPath, onCompiledListLoaded);
|
|
|
|
};
|
2014-09-26 02:21:21 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
/******************************************************************************/
|
2014-09-26 02:21:21 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.purgeCompiledFilterList = function(path) {
|
|
|
|
this.assets.purge(this.getCompiledFilterListPath(path));
|
2014-09-25 21:44:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-02-25 22:33:51 +01:00
|
|
|
|
|
|
|
µBlock.purgeFilterList = function(path) {
|
|
|
|
this.purgeCompiledFilterList(path);
|
|
|
|
this.assets.purge(path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-09-25 21:44:18 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.compileFilters = function(rawText) {
|
2014-09-25 21:44:18 +02:00
|
|
|
var rawEnd = rawText.length;
|
2015-02-24 00:31:29 +01:00
|
|
|
var compiledFilters = [];
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
// Useful references:
|
|
|
|
// https://adblockplus.org/en/filter-cheatsheet
|
|
|
|
// https://adblockplus.org/en/filters
|
2014-12-28 16:07:43 +01:00
|
|
|
var staticNetFilteringEngine = this.staticNetFilteringEngine;
|
2014-09-08 23:46:58 +02:00
|
|
|
var cosmeticFilteringEngine = this.cosmeticFilteringEngine;
|
2015-01-23 17:32:49 +01:00
|
|
|
var reIsWhitespaceChar = /\s/;
|
|
|
|
var reMaybeLocalIp = /^[\d:f]/;
|
|
|
|
var reIsLocalhostRedirect = /\s+(?:broadcasthost|local|localhost|localhost\.localdomain)(?=\s|$)/;
|
|
|
|
var reLocalIp = /^(?:0\.0\.0\.0|127\.0\.0\.1|::1|fe80::1%lo0)/;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
var lineBeg = 0, lineEnd, currentLineBeg;
|
2015-01-23 17:32:49 +01:00
|
|
|
var line, lineRaw, c, pos;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
while ( lineBeg < rawEnd ) {
|
|
|
|
lineEnd = rawText.indexOf('\n', lineBeg);
|
2014-09-25 21:44:18 +02:00
|
|
|
if ( lineEnd === -1 ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
lineEnd = rawText.indexOf('\r', lineBeg);
|
2014-09-25 21:44:18 +02:00
|
|
|
if ( lineEnd === -1 ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
lineEnd = rawEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-01-23 17:32:49 +01:00
|
|
|
line = lineRaw = rawText.slice(lineBeg, lineEnd).trim();
|
2014-06-24 00:42:43 +02:00
|
|
|
currentLineBeg = lineBeg;
|
|
|
|
lineBeg = lineEnd + 1;
|
|
|
|
|
2015-01-23 17:32:49 +01:00
|
|
|
if ( line.length === 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
// Strip comments
|
|
|
|
c = line.charAt(0);
|
|
|
|
if ( c === '!' || c === '[' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
2015-02-24 00:31:29 +01:00
|
|
|
if ( cosmeticFilteringEngine.compile(line, compiledFilters) ) {
|
2014-09-25 21:44:18 +02:00
|
|
|
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
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( c === '#' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if ( reIsLocalhostRedirect.test(line) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
line = line.replace(reLocalIp, '').trim();
|
|
|
|
}
|
2015-03-07 19:20:18 +01:00
|
|
|
|
2015-01-23 17:32:49 +01:00
|
|
|
if ( line.length === 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
//staticNetFilteringEngine.add(line);
|
|
|
|
staticNetFilteringEngine.compile(line, compiledFilters);
|
|
|
|
}
|
|
|
|
|
|
|
|
return compiledFilters.join('\n');
|
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.applyCompiledFilters = function(rawText) {
|
|
|
|
var skipCosmetic = !this.userSettings.parseAllABPHideFilters;
|
|
|
|
var staticNetFilteringEngine = this.staticNetFilteringEngine;
|
|
|
|
var cosmeticFilteringEngine = this.cosmeticFilteringEngine;
|
|
|
|
var lineBeg = 0;
|
|
|
|
var rawEnd = rawText.length;
|
|
|
|
while ( lineBeg < rawEnd ) {
|
|
|
|
lineBeg = cosmeticFilteringEngine.fromCompiledContent(rawText, lineBeg, skipCosmetic);
|
|
|
|
lineBeg = staticNetFilteringEngine.fromCompiledContent(rawText, lineBeg);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
// `switches` contains the filter lists for which the switch must be revisited.
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
µBlock.selectFilterLists = function(switches) {
|
|
|
|
switches = switches || {};
|
|
|
|
|
|
|
|
// Only the lists referenced by the switches are touched.
|
|
|
|
var filterLists = this.remoteBlacklists;
|
|
|
|
var entry, state, location;
|
|
|
|
var i = switches.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
entry = switches[i];
|
|
|
|
state = entry.off === true;
|
|
|
|
location = entry.location;
|
|
|
|
if ( filterLists.hasOwnProperty(location) === false ) {
|
|
|
|
if ( state !== true ) {
|
|
|
|
filterLists[location] = { off: state };
|
|
|
|
}
|
|
|
|
continue;
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
2015-03-11 04:46:18 +01:00
|
|
|
if ( filterLists[location].off === state ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
filterLists[location].off = state;
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
vAPI.storage.set({ 'remoteBlacklists': filterLists });
|
|
|
|
};
|
2015-03-09 17:57:52 +01:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
/******************************************************************************/
|
2015-03-09 17:57:52 +01:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
// Plain reload of all filters.
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
µBlock.reloadAllFilters = function() {
|
|
|
|
var µb = this;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
// We are just reloading the filter lists: we do not want assets to update.
|
|
|
|
this.assets.autoUpdate = false;
|
|
|
|
|
|
|
|
var onFiltersReady = function() {
|
|
|
|
µb.assets.autoUpdate = µb.userSettings.autoUpdate;
|
2015-02-24 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
this.loadFilterLists(onFiltersReady);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-25 02:52:34 +02:00
|
|
|
µBlock.loadPublicSuffixList = function(callback) {
|
2015-02-24 00:31:29 +01:00
|
|
|
var µb = this;
|
|
|
|
var path = µb.pslPath;
|
|
|
|
var compiledPath = 'cache://compiled-publicsuffixlist';
|
|
|
|
|
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 !== '' ) {
|
|
|
|
//console.debug('µBlock.loadPublicSuffixList/onRawListLoaded: compiling "%s"', path);
|
2014-06-24 00:42:43 +02:00
|
|
|
publicSuffixList.parse(details.content, punycode.toASCII);
|
2015-02-24 00:31:29 +01:00
|
|
|
µb.assets.put(compiledPath, JSON.stringify(publicSuffixList.toSelfie()));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
var onCompiledListLoaded = function(details) {
|
|
|
|
if ( details.content === '' ) {
|
|
|
|
//console.debug('µBlock.loadPublicSuffixList/onCompiledListLoaded: no compiled version for "%s"', path);
|
|
|
|
µb.assets.get(path, onRawListLoaded);
|
|
|
|
return;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
//console.debug('µBlock.loadPublicSuffixList/onCompiledListLoaded: using compiled version for "%s"', path);
|
|
|
|
publicSuffixList.fromSelfie(JSON.parse(details.content));
|
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
|
|
|
|
|
|
|
this.assets.get(compiledPath, onCompiledListLoaded);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
µBlock.toSelfie = function() {
|
|
|
|
var selfie = {
|
2015-02-24 00:31:29 +01:00
|
|
|
magic: this.systemSettings.selfieMagic,
|
2014-09-08 23:46:58 +02:00
|
|
|
publicSuffixList: publicSuffixList.toSelfie(),
|
|
|
|
filterLists: this.remoteBlacklists,
|
2014-12-28 16:07:43 +01:00
|
|
|
staticNetFilteringEngine: this.staticNetFilteringEngine.toSelfie(),
|
2014-09-11 22:00:50 +02:00
|
|
|
cosmeticFilteringEngine: this.cosmeticFilteringEngine.toSelfie()
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.storage.set({ selfie: selfie });
|
2015-02-15 13:16:31 +01:00
|
|
|
//console.debug('storage.js > µBlock.toSelfie()');
|
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
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
µBlock.toSelfieAsync = function(after) {
|
|
|
|
if ( typeof after !== 'number' ) {
|
|
|
|
after = this.selfieAfter;
|
|
|
|
}
|
|
|
|
this.asyncJobs.add(
|
|
|
|
'toSelfie',
|
|
|
|
null,
|
|
|
|
this.toSelfie.bind(this),
|
|
|
|
after,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.destroySelfie = function() {
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.storage.remove('selfie');
|
2015-02-13 18:10:10 +01:00
|
|
|
this.asyncJobs.remove('toSelfie');
|
2015-02-24 00:31:29 +01:00
|
|
|
//console.debug('µBlock.destroySelfie()');
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-11 16:05:13 +01:00
|
|
|
µBlock.updateStartHandler = function(callback) {
|
2014-08-25 02:52:34 +02:00
|
|
|
var µb = this;
|
2015-02-13 18:10:10 +01:00
|
|
|
var onListsReady = function(lists) {
|
2015-03-11 16:05:13 +01:00
|
|
|
var assets = {};
|
2015-02-13 18:10:10 +01:00
|
|
|
for ( var location in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(location) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( lists[location].off ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-11 16:05:13 +01:00
|
|
|
assets[location] = true;
|
2015-02-13 18:10:10 +01:00
|
|
|
}
|
2015-03-11 16:05:13 +01:00
|
|
|
assets[µb.pslPath] = true;
|
|
|
|
assets['assets/ublock/mirror-candidates.txt'] = true;
|
|
|
|
callback(assets);
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
2015-03-11 16:05:13 +01:00
|
|
|
|
2015-02-13 18:10:10 +01:00
|
|
|
this.getAvailableLists(onListsReady);
|
|
|
|
};
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2015-02-13 18:10:10 +01:00
|
|
|
/******************************************************************************/
|
2015-01-24 18:06:22 +01:00
|
|
|
|
2015-02-24 00:31:29 +01:00
|
|
|
µBlock.assetUpdatedHandler = function(details) {
|
|
|
|
var path = details.path || '';
|
|
|
|
if ( this.remoteBlacklists.hasOwnProperty(path) === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var entry = this.remoteBlacklists[path];
|
|
|
|
if ( entry.off ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Compile the list while we have the raw version in memory
|
|
|
|
//console.debug('µBlock.getCompiledFilterList/onRawListLoaded: compiling "%s"', path);
|
|
|
|
this.assets.put(
|
|
|
|
this.getCompiledFilterListPath(path),
|
|
|
|
this.compileFilters(details.content)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-13 18:10:10 +01:00
|
|
|
µBlock.updateCompleteHandler = function(details) {
|
|
|
|
var µb = this;
|
|
|
|
var updatedCount = details.updatedCount;
|
2015-01-22 03:46:11 +01:00
|
|
|
|
2015-03-11 19:52:20 +01:00
|
|
|
// Assets are supposed to have been all updated, prevent fetching from
|
2015-02-13 18:10:10 +01:00
|
|
|
// remote servers.
|
2015-03-11 19:52:20 +01:00
|
|
|
µb.assets.remoteFetchBarrier += 1;
|
2015-01-22 03:46:11 +01:00
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
var onFiltersReady = function() {
|
2015-03-11 19:52:20 +01:00
|
|
|
µb.assets.remoteFetchBarrier -= 1;
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
2014-08-25 02:52:34 +02:00
|
|
|
var onPSLReady = function() {
|
2015-02-13 18:10:10 +01:00
|
|
|
if ( updatedCount !== 0 ) {
|
2015-02-15 13:16:31 +01:00
|
|
|
//console.debug('storage.js > µBlock.updateCompleteHandler: reloading filter lists');
|
2015-02-13 18:10:10 +01:00
|
|
|
µb.loadFilterLists(onFiltersReady);
|
|
|
|
} else {
|
2014-12-15 20:37:09 +01:00
|
|
|
onFiltersReady();
|
2014-09-08 23:46:58 +02:00
|
|
|
}
|
2014-08-25 02:52:34 +02:00
|
|
|
};
|
|
|
|
|
2015-02-13 18:10:10 +01:00
|
|
|
if ( details.hasOwnProperty('assets/ublock/mirror-candidates.txt') ) {
|
|
|
|
/* TODO */
|
|
|
|
}
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2015-02-13 18:10:10 +01:00
|
|
|
if ( details.hasOwnProperty(this.pslPath) ) {
|
2015-02-15 13:16:31 +01:00
|
|
|
//console.debug('storage.js > µBlock.updateCompleteHandler: reloading PSL');
|
2015-02-13 18:10:10 +01:00
|
|
|
this.loadPublicSuffixList(onPSLReady);
|
|
|
|
updatedCount -= 1;
|
|
|
|
} else {
|
|
|
|
onPSLReady();
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.assetCacheRemovedHandler = (function() {
|
|
|
|
var barrier = false;
|
|
|
|
|
|
|
|
var handler = function(paths) {
|
|
|
|
if ( barrier ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
barrier = true;
|
|
|
|
var i = paths.length;
|
|
|
|
var path;
|
|
|
|
while ( i-- ) {
|
|
|
|
path = paths[i];
|
|
|
|
if ( this.remoteBlacklists.hasOwnProperty(path) ) {
|
|
|
|
//console.debug('µBlock.assetCacheRemovedHandler: decompiling "%s"', path);
|
|
|
|
this.purgeCompiledFilterList(path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( path === this.pslPath ) {
|
|
|
|
//console.debug('µBlock.assetCacheRemovedHandler: decompiling "%s"', path);
|
|
|
|
this.assets.purge('cache://compiled-publicsuffixlist');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.destroySelfie();
|
|
|
|
barrier = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
})();
|