2015-03-27 18:00:55 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-11-12 19:42:36 +01:00
|
|
|
uBlock Origin - a Chromium browser extension to black/white list requests.
|
2018-09-01 00:47:02 +02:00
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2015-03-27 18:00:55 +01: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
|
|
|
|
*/
|
|
|
|
|
2016-11-12 19:42:36 +01:00
|
|
|
/* global punycode */
|
2015-03-27 18:00:55 +01:00
|
|
|
/* jshint bitwise: false */
|
|
|
|
|
2016-11-12 19:42:36 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.HnSwitches = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var HnSwitches = function() {
|
|
|
|
this.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var switchBitOffsets = {
|
2017-10-19 15:35:28 +02:00
|
|
|
'no-strict-blocking': 0,
|
|
|
|
'no-popups': 2,
|
|
|
|
'no-cosmetic-filtering': 4,
|
|
|
|
'no-remote-fonts': 6,
|
|
|
|
'no-large-media': 8,
|
2018-09-01 00:47:02 +02:00
|
|
|
'no-csp-reports': 10,
|
|
|
|
'no-scripting': 12,
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var switchStateToNameMap = {
|
2015-04-11 14:54:22 +02:00
|
|
|
'1': 'true',
|
|
|
|
'2': 'false'
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var nameToSwitchStateMap = {
|
|
|
|
'true': 1,
|
2015-04-09 18:20:24 +02:00
|
|
|
'false': 2,
|
|
|
|
'on': 1,
|
|
|
|
'off': 2
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// For performance purpose, as simple tests as possible
|
2018-03-11 15:59:39 +01:00
|
|
|
var reNotASCII = /[^\x20-\x7F]/;
|
2015-03-27 18:00:55 +01:00
|
|
|
|
|
|
|
// http://tools.ietf.org/html/rfc5952
|
|
|
|
// 4.3: "MUST be represented in lowercase"
|
|
|
|
// Also: http://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
HnSwitches.prototype.reset = function() {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.switches = new Map();
|
2015-06-10 15:23:48 +02:00
|
|
|
this.n = '';
|
2015-04-25 06:28:30 +02:00
|
|
|
this.z = '';
|
2015-06-10 15:23:48 +02:00
|
|
|
this.r = 0;
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2018-06-03 19:27:42 +02:00
|
|
|
this.decomposedSource = [];
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-09-03 20:06:49 +02:00
|
|
|
HnSwitches.prototype.assign = function(from) {
|
|
|
|
// Remove rules not in other
|
|
|
|
for ( let hn of this.switches.keys() ) {
|
|
|
|
if ( from.switches.has(hn) === false ) {
|
|
|
|
this.switches.delete(hn);
|
|
|
|
this.changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add/change rules in other
|
|
|
|
for ( let [hn, bits] of from.switches ) {
|
|
|
|
if ( this.switches.get(hn) !== bits ) {
|
|
|
|
this.switches.set(hn, bits);
|
|
|
|
this.changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
HnSwitches.prototype.copyRules = function(from, srcHostname) {
|
|
|
|
let thisBits = this.switches.get(srcHostname);
|
|
|
|
let fromBits = from.switches.get(srcHostname);
|
|
|
|
if ( fromBits !== thisBits ) {
|
|
|
|
if ( fromBits !== undefined ) {
|
|
|
|
this.switches.set(srcHostname, fromBits);
|
|
|
|
} else {
|
|
|
|
this.switches.delete(srcHostname);
|
|
|
|
}
|
|
|
|
this.changed = true;
|
|
|
|
}
|
|
|
|
return this.changed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
HnSwitches.prototype.hasSameRules = function(other, srcHostname) {
|
|
|
|
return this.switches.get(srcHostname) === other.switches.get(srcHostname);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
// If value is undefined, the switch is removed
|
|
|
|
|
|
|
|
HnSwitches.prototype.toggle = function(switchName, hostname, newVal) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let bitOffset = switchBitOffsets[switchName];
|
|
|
|
if ( bitOffset === undefined ) { return false; }
|
|
|
|
if ( newVal === this.evaluate(switchName, hostname) ) { return false; }
|
|
|
|
let bits = this.switches.get(hostname) || 0;
|
2015-03-27 18:00:55 +01:00
|
|
|
bits &= ~(3 << bitOffset);
|
|
|
|
bits |= newVal << bitOffset;
|
|
|
|
if ( bits === 0 ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.switches.delete(hostname);
|
2015-03-27 18:00:55 +01:00
|
|
|
} else {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.switches.set(hostname, bits);
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-03-27 18:00:55 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-07 19:24:15 +02:00
|
|
|
HnSwitches.prototype.toggleOneZ = function(switchName, hostname, newState) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let bitOffset = switchBitOffsets[switchName];
|
|
|
|
if ( bitOffset === undefined ) { return false; }
|
|
|
|
let state = this.evaluateZ(switchName, hostname);
|
|
|
|
if ( newState === state ) { return false; }
|
2015-03-27 18:00:55 +01:00
|
|
|
if ( newState === undefined ) {
|
|
|
|
newState = !state;
|
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
let bits = this.switches.get(hostname) || 0;
|
2015-03-27 18:00:55 +01:00
|
|
|
bits &= ~(3 << bitOffset);
|
|
|
|
if ( bits === 0 ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.switches.delete(hostname);
|
2015-03-27 18:00:55 +01:00
|
|
|
} else {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.switches.set(hostname, bits);
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
|
|
|
state = this.evaluateZ(switchName, hostname);
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( state !== newState ) {
|
|
|
|
this.switches.set(hostname, bits | ((newState ? 1 : 2) << bitOffset));
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-03-27 18:00:55 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-07 19:24:15 +02:00
|
|
|
HnSwitches.prototype.toggleBranchZ = function(switchName, targetHostname, newState) {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.toggleOneZ(switchName, targetHostname, newState);
|
2015-04-07 19:24:15 +02:00
|
|
|
|
2015-04-09 18:20:24 +02:00
|
|
|
// Turn off all descendant switches, they will inherit the state of the
|
|
|
|
// branch's origin.
|
2018-09-03 20:06:49 +02:00
|
|
|
let targetLen = targetHostname.length;
|
|
|
|
for ( let hostname of this.switches.keys() ) {
|
|
|
|
if ( hostname === targetHostname ) { continue; }
|
|
|
|
if ( hostname.length <= targetLen ) { continue; }
|
|
|
|
if ( hostname.endsWith(targetHostname) === false ) { continue; }
|
2015-04-07 19:24:15 +02:00
|
|
|
if ( hostname.charAt(hostname.length - targetLen - 1) !== '.' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.toggle(switchName, hostname, 0);
|
2015-04-07 19:24:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
return this.changed;
|
2015-04-07 19:24:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-07 19:27:46 +02:00
|
|
|
HnSwitches.prototype.toggleZ = function(switchName, hostname, deep, newState) {
|
|
|
|
if ( deep === true ) {
|
2015-04-08 12:21:31 +02:00
|
|
|
return this.toggleBranchZ(switchName, hostname, newState);
|
2015-04-07 19:27:46 +02:00
|
|
|
}
|
2015-04-08 12:21:31 +02:00
|
|
|
return this.toggleOneZ(switchName, hostname, newState);
|
2015-04-07 19:27:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
// 0 = inherit from broader scope, up to default state
|
|
|
|
// 1 = non-default state
|
|
|
|
// 2 = forced default state (to override a broader non-default state)
|
|
|
|
|
|
|
|
HnSwitches.prototype.evaluate = function(switchName, hostname) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let bits = this.switches.get(hostname);
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( bits === undefined ) {
|
2015-03-27 18:00:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
let bitOffset = switchBitOffsets[switchName];
|
2015-03-27 18:00:55 +01:00
|
|
|
if ( bitOffset === undefined ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-12 19:42:36 +01:00
|
|
|
return (bits >>> bitOffset) & 3;
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
HnSwitches.prototype.evaluateZ = function(switchName, hostname) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let bitOffset = switchBitOffsets[switchName];
|
2015-03-27 18:00:55 +01:00
|
|
|
if ( bitOffset === undefined ) {
|
2015-06-10 15:23:48 +02:00
|
|
|
this.r = 0;
|
2015-03-27 18:00:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-10 15:23:48 +02:00
|
|
|
this.n = switchName;
|
2018-06-03 19:27:42 +02:00
|
|
|
µBlock.decomposeHostname(hostname, this.decomposedSource);
|
|
|
|
for ( let shn of this.decomposedSource ) {
|
|
|
|
let bits = this.switches.get(shn);
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( bits !== undefined ) {
|
2016-11-12 19:42:36 +01:00
|
|
|
bits = bits >>> bitOffset & 3;
|
2015-03-27 18:00:55 +01:00
|
|
|
if ( bits !== 0 ) {
|
2018-06-03 19:27:42 +02:00
|
|
|
this.z = shn;
|
2015-06-10 15:23:48 +02:00
|
|
|
this.r = bits;
|
2015-03-27 18:00:55 +01:00
|
|
|
return bits === 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 15:23:48 +02:00
|
|
|
this.r = 0;
|
2015-03-27 18:00:55 +01:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
HnSwitches.prototype.toLogData = function() {
|
|
|
|
return {
|
|
|
|
source: 'switch',
|
|
|
|
result: this.r,
|
|
|
|
raw: this.n + ': ' + this.z + ' true'
|
|
|
|
};
|
2015-06-10 15:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
HnSwitches.prototype.toArray = function() {
|
2018-09-03 20:06:49 +02:00
|
|
|
let out = [],
|
2017-05-04 01:43:09 +02:00
|
|
|
toUnicode = punycode.toUnicode;
|
2018-09-03 20:06:49 +02:00
|
|
|
for ( let hostname of this.switches.keys() ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
for ( var switchName in switchBitOffsets ) {
|
2015-03-27 18:00:55 +01:00
|
|
|
if ( switchBitOffsets.hasOwnProperty(switchName) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
let val = this.evaluate(switchName, hostname);
|
2017-05-04 01:43:09 +02:00
|
|
|
if ( val === 0 ) { continue; }
|
|
|
|
if ( hostname.indexOf('xn--') !== -1 ) {
|
|
|
|
hostname = toUnicode(hostname);
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
|
|
|
out.push(switchName + ': ' + hostname + ' ' + switchStateToNameMap[val]);
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
HnSwitches.prototype.toString = function() {
|
|
|
|
return this.toArray().join('\n');
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-03-31 16:21:11 +02:00
|
|
|
HnSwitches.prototype.fromString = function(text, append) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let lineIter = new µBlock.LineIterator(text);
|
2018-03-31 16:21:11 +02:00
|
|
|
if ( append !== true ) { this.reset(); }
|
2017-05-04 01:43:09 +02:00
|
|
|
while ( lineIter.eot() === false ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.addFromRuleParts(lineIter.next().trim().split(/\s+/));
|
|
|
|
}
|
|
|
|
};
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
/******************************************************************************/
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
HnSwitches.prototype.validateRuleParts = function(parts) {
|
|
|
|
if ( parts.length < 3 ) { return; }
|
|
|
|
if ( parts[0].endsWith(':') === false ) { return; }
|
|
|
|
if ( nameToSwitchStateMap.hasOwnProperty(parts[2]) === false ) { return; }
|
|
|
|
// Performance: avoid punycoding if hostname is made only of ASCII chars.
|
|
|
|
if ( reNotASCII.test(parts[1]) ) { parts[1] = punycode.toASCII(parts[1]); }
|
|
|
|
return parts;
|
|
|
|
};
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
/******************************************************************************/
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
HnSwitches.prototype.addFromRuleParts = function(parts) {
|
|
|
|
if ( this.validateRuleParts(parts) !== undefined ) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let switchName = parts[0].slice(0, -1);
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( switchBitOffsets.hasOwnProperty(switchName) ) {
|
|
|
|
this.toggle(switchName, parts[1], nameToSwitchStateMap[parts[2]]);
|
|
|
|
return true;
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
HnSwitches.prototype.removeFromRuleParts = function(parts) {
|
|
|
|
if ( this.validateRuleParts(parts) !== undefined ) {
|
|
|
|
this.toggle(parts[0].slice(0, -1), parts[1], 0);
|
|
|
|
return true;
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
return false;
|
2015-03-27 18:00:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return HnSwitches;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-09-03 20:06:49 +02:00
|
|
|
µBlock.sessionSwitches = new µBlock.HnSwitches();
|
|
|
|
µBlock.permanentSwitches = new µBlock.HnSwitches();
|
2015-03-27 18:00:55 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|