2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-12-29 17:34:41 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-14 17:01:21 +01:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-12-29 17:34:41 +01:00
|
|
|
// A standalone URL tokenizer will allow us to use URL tokens in more than
|
|
|
|
// just static filtering engine. This opens the door to optimize other
|
|
|
|
// filtering engine parts aside static filtering. This also allows:
|
|
|
|
// - Tokenize only on demand.
|
|
|
|
// - To potentially avoid tokenizing when same URL is fed to tokenizer.
|
|
|
|
// - Benchmarking shows this to be a common occurrence.
|
2017-05-19 14:45:19 +02:00
|
|
|
//
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2630
|
2017-05-20 02:22:26 +02:00
|
|
|
// Slice input URL into a list of safe-integer token values, instead of a list
|
2017-05-19 14:45:19 +02:00
|
|
|
// of substrings. The assumption is that with dealing only with numeric
|
|
|
|
// values, less underlying memory allocations, and also as a consequence
|
|
|
|
// less work for the garbage collector down the road.
|
|
|
|
// Another assumption is that using a numeric-based key value for Map() is
|
|
|
|
// more efficient than string-based key value (but that is something I would
|
|
|
|
// have to benchmark).
|
2017-05-20 02:22:26 +02:00
|
|
|
// Benchmark for string-based tokens vs. safe-integer token values:
|
2017-05-19 14:45:19 +02:00
|
|
|
// https://gorhill.github.io/obj-vs-set-vs-map/tokenize-to-str-vs-to-int.html
|
2015-12-29 17:34:41 +01:00
|
|
|
|
|
|
|
µBlock.urlTokenizer = {
|
|
|
|
setURL: function(url) {
|
|
|
|
if ( url !== this._urlIn ) {
|
|
|
|
this._urlIn = url;
|
|
|
|
this._urlOut = url.toLowerCase();
|
|
|
|
this._tokenized = false;
|
|
|
|
}
|
|
|
|
return this._urlOut;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Tokenize on demand.
|
|
|
|
getTokens: function() {
|
|
|
|
if ( this._tokenized === false ) {
|
|
|
|
this._tokenize();
|
|
|
|
this._tokenized = true;
|
|
|
|
}
|
|
|
|
return this._tokens;
|
|
|
|
},
|
|
|
|
|
2017-05-19 14:45:19 +02:00
|
|
|
tokenHashFromString: function(s) {
|
|
|
|
var l = s.length;
|
|
|
|
if ( l === 0 ) { return 0; }
|
|
|
|
if ( l === 1 ) {
|
|
|
|
if ( s === '*' ) { return 63; }
|
|
|
|
if ( s === '.' ) { return 62; }
|
2015-12-29 17:34:41 +01:00
|
|
|
}
|
2017-05-19 14:45:19 +02:00
|
|
|
var vtc = this._validTokenChars,
|
|
|
|
th = vtc[s.charCodeAt(0)];
|
|
|
|
for ( var i = 1; i !== 8 && i !== l; i++ ) {
|
|
|
|
th = th * 64 + vtc[s.charCodeAt(i)];
|
|
|
|
}
|
|
|
|
return th;
|
2015-12-29 17:34:41 +01:00
|
|
|
},
|
|
|
|
|
2017-05-26 14:31:19 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1118
|
|
|
|
// We limit to a maximum number of tokens.
|
|
|
|
|
2015-12-29 17:34:41 +01:00
|
|
|
_tokenize: function() {
|
|
|
|
var tokens = this._tokens,
|
2017-05-19 14:45:19 +02:00
|
|
|
url = this._urlOut,
|
|
|
|
l = url.length;
|
|
|
|
if ( l === 0 ) { tokens[0] = 0; return; }
|
|
|
|
if ( l > 2048 ) {
|
|
|
|
url = url.slice(0, 2048);
|
|
|
|
l = 2048;
|
2015-12-29 17:34:41 +01:00
|
|
|
}
|
2017-05-19 14:45:19 +02:00
|
|
|
var i = 0, j = 0, v, n, ti, th,
|
|
|
|
vtc = this._validTokenChars;
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
if ( i === l ) { tokens[j] = 0; return; }
|
|
|
|
v = vtc[url.charCodeAt(i++)];
|
|
|
|
if ( v !== 0 ) { break; }
|
|
|
|
}
|
|
|
|
th = v; ti = i - 1; n = 1;
|
|
|
|
for (;;) {
|
|
|
|
if ( i === l ) { break; }
|
|
|
|
v = vtc[url.charCodeAt(i++)];
|
|
|
|
if ( v === 0 ) { break; }
|
2017-05-20 22:32:42 +02:00
|
|
|
if ( n === 8 ) { continue; }
|
|
|
|
th = th * 64 + v;
|
|
|
|
n += 1;
|
2017-05-19 14:45:19 +02:00
|
|
|
}
|
|
|
|
tokens[j++] = th;
|
|
|
|
tokens[j++] = ti;
|
2016-10-11 17:53:28 +02:00
|
|
|
}
|
2015-12-29 17:34:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_urlIn: '',
|
|
|
|
_urlOut: '',
|
|
|
|
_tokenized: false,
|
2017-05-19 14:45:19 +02:00
|
|
|
_tokens: [ 0 ],
|
|
|
|
_validTokenChars: (function() {
|
|
|
|
var vtc = new Uint8Array(128),
|
|
|
|
chars = '0123456789%abcdefghijklmnopqrstuvwxyz',
|
|
|
|
i = chars.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
vtc[chars.charCodeAt(i)] = i + 1;
|
|
|
|
}
|
|
|
|
return vtc;
|
|
|
|
})()
|
2015-12-29 17:34:41 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-12-29 17:34:41 +01:00
|
|
|
µBlock.formatCount = function(count) {
|
2014-08-20 02:41:52 +02:00
|
|
|
if ( typeof count !== 'number' ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
var s = count.toFixed(0);
|
|
|
|
if ( count >= 1000 ) {
|
|
|
|
if ( count < 10000 ) {
|
2014-12-24 14:11:22 +01:00
|
|
|
s = '>' + s.slice(0,1) + 'k';
|
2014-08-20 02:41:52 +02:00
|
|
|
} else if ( count < 100000 ) {
|
2014-12-24 14:11:22 +01:00
|
|
|
s = s.slice(0,2) + 'k';
|
2014-08-20 02:41:52 +02:00
|
|
|
} else if ( count < 1000000 ) {
|
2014-12-24 14:11:22 +01:00
|
|
|
s = s.slice(0,3) + 'k';
|
2014-08-20 02:41:52 +02:00
|
|
|
} else if ( count < 10000000 ) {
|
|
|
|
s = s.slice(0,1) + 'M';
|
|
|
|
} else {
|
|
|
|
s = s.slice(0,-6) + 'M';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2014-08-20 15:24:16 +02:00
|
|
|
// https://www.youtube.com/watch?v=DyvzfyqYm_s
|
2014-08-20 02:41:52 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-08-13 22:42:58 +02:00
|
|
|
|
2016-10-13 19:25:57 +02:00
|
|
|
µBlock.dateNowToSensibleString = function() {
|
|
|
|
var now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
|
|
|
|
return now.toISOString().replace(/\.\d+Z$/, '')
|
|
|
|
.replace(/:/g, '.')
|
|
|
|
.replace('T', '_');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
µBlock.LineIterator = function(text, offset) {
|
|
|
|
this.text = text;
|
2016-09-12 16:22:25 +02:00
|
|
|
this.textLen = this.text.length;
|
2016-08-13 22:42:58 +02:00
|
|
|
this.offset = offset || 0;
|
|
|
|
};
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
µBlock.LineIterator.prototype.next = function(offset) {
|
|
|
|
if ( offset !== undefined ) {
|
|
|
|
this.offset += offset;
|
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
var lineEnd = this.text.indexOf('\n', this.offset);
|
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = this.text.indexOf('\r', this.offset);
|
|
|
|
if ( lineEnd === -1 ) {
|
2016-09-12 16:22:25 +02:00
|
|
|
lineEnd = this.textLen;
|
2016-08-13 22:42:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var line = this.text.slice(this.offset, lineEnd);
|
|
|
|
this.offset = lineEnd + 1;
|
|
|
|
return line;
|
|
|
|
};
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
µBlock.LineIterator.prototype.charCodeAt = function(offset) {
|
|
|
|
return this.text.charCodeAt(this.offset + offset);
|
2017-03-11 19:55:47 +01:00
|
|
|
};
|
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
µBlock.LineIterator.prototype.eot = function() {
|
2016-09-12 16:22:25 +02:00
|
|
|
return this.offset >= this.textLen;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// The field iterator is less CPU-intensive than when using native
|
|
|
|
// String.split().
|
|
|
|
|
|
|
|
µBlock.FieldIterator = function(sep) {
|
|
|
|
this.text = '';
|
|
|
|
this.sep = sep;
|
|
|
|
this.sepLen = sep.length;
|
|
|
|
this.offset = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.FieldIterator.prototype.first = function(text) {
|
|
|
|
this.text = text;
|
|
|
|
this.offset = 0;
|
|
|
|
return this.next();
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.FieldIterator.prototype.next = function() {
|
|
|
|
var end = this.text.indexOf(this.sep, this.offset);
|
|
|
|
if ( end === -1 ) {
|
|
|
|
end = this.text.length;
|
|
|
|
}
|
|
|
|
var field = this.text.slice(this.offset, end);
|
|
|
|
this.offset = end + this.sepLen;
|
|
|
|
return field;
|
|
|
|
};
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
µBlock.FieldIterator.prototype.remainder = function() {
|
|
|
|
return this.text.slice(this.offset);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-10-23 19:01:08 +02:00
|
|
|
µBlock.CompiledLineIO = {
|
|
|
|
serialize: JSON.stringify,
|
|
|
|
unserialize: JSON.parse,
|
|
|
|
blockStartPrefix: '#block-start-', // ensure no special regex characters
|
|
|
|
blockEndPrefix: '#block-end-', // ensure no special regex characters
|
|
|
|
|
|
|
|
Writer: function() {
|
|
|
|
this.io = µBlock.CompiledLineIO;
|
|
|
|
this.blockId = undefined;
|
|
|
|
this.block = undefined;
|
|
|
|
this.stringifier = this.io.serialize;
|
2018-12-15 16:46:17 +01:00
|
|
|
this.blocks = new Map();
|
|
|
|
this.properties = new Map();
|
2018-10-23 19:01:08 +02:00
|
|
|
},
|
2017-05-12 16:35:11 +02:00
|
|
|
|
2018-10-23 19:01:08 +02:00
|
|
|
Reader: function(raw, blockId) {
|
|
|
|
this.io = µBlock.CompiledLineIO;
|
|
|
|
this.block = '';
|
|
|
|
this.len = 0;
|
|
|
|
this.offset = 0;
|
|
|
|
this.line = '';
|
|
|
|
this.parser = this.io.unserialize;
|
|
|
|
this.blocks = new Map();
|
2018-12-15 16:46:17 +01:00
|
|
|
this.properties = new Map();
|
2018-10-23 19:01:08 +02:00
|
|
|
let reBlockStart = new RegExp(
|
|
|
|
'^' + this.io.blockStartPrefix + '(\\d+)\\n',
|
|
|
|
'gm'
|
|
|
|
);
|
|
|
|
let match = reBlockStart.exec(raw);
|
|
|
|
while ( match !== null ) {
|
|
|
|
let beg = match.index + match[0].length;
|
|
|
|
let end = raw.indexOf(this.io.blockEndPrefix + match[1], beg);
|
|
|
|
this.blocks.set(parseInt(match[1], 10), raw.slice(beg, end));
|
|
|
|
reBlockStart.lastIndex = end;
|
|
|
|
match = reBlockStart.exec(raw);
|
|
|
|
}
|
|
|
|
if ( blockId !== undefined ) {
|
|
|
|
this.select(blockId);
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
};
|
|
|
|
|
2018-10-23 19:01:08 +02:00
|
|
|
µBlock.CompiledLineIO.Writer.prototype = {
|
2017-05-25 23:46:59 +02:00
|
|
|
push: function(args) {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.block[this.block.length] = this.stringifier(args);
|
|
|
|
},
|
|
|
|
select: function(blockId) {
|
|
|
|
if ( blockId === this.blockId ) { return; }
|
|
|
|
this.blockId = blockId;
|
|
|
|
this.block = this.blocks.get(blockId);
|
|
|
|
if ( this.block === undefined ) {
|
|
|
|
this.blocks.set(blockId, (this.block = []));
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
},
|
|
|
|
toString: function() {
|
2018-10-23 19:01:08 +02:00
|
|
|
let result = [];
|
|
|
|
for ( let [ id, lines ] of this.blocks ) {
|
|
|
|
if ( lines.length === 0 ) { continue; }
|
2017-12-28 19:49:02 +01:00
|
|
|
result.push(
|
2018-10-23 19:01:08 +02:00
|
|
|
this.io.blockStartPrefix + id,
|
|
|
|
lines.join('\n'),
|
|
|
|
this.io.blockEndPrefix + id
|
2017-12-28 19:49:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return result.join('\n');
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-23 19:01:08 +02:00
|
|
|
µBlock.CompiledLineIO.Reader.prototype = {
|
2017-05-25 23:46:59 +02:00
|
|
|
next: function() {
|
|
|
|
if ( this.offset === this.len ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.line = '';
|
2017-05-25 23:46:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 19:01:08 +02:00
|
|
|
let pos = this.block.indexOf('\n', this.offset);
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( pos !== -1 ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.line = this.block.slice(this.offset, pos);
|
2017-05-25 23:46:59 +02:00
|
|
|
this.offset = pos + 1;
|
|
|
|
} else {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.line = this.block.slice(this.offset);
|
2017-05-25 23:46:59 +02:00
|
|
|
this.offset = this.len;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2017-12-28 19:49:02 +01:00
|
|
|
select: function(blockId) {
|
|
|
|
this.block = this.blocks.get(blockId) || '';
|
|
|
|
this.len = this.block.length;
|
|
|
|
this.offset = 0;
|
|
|
|
return this;
|
|
|
|
},
|
2017-05-25 23:46:59 +02:00
|
|
|
fingerprint: function() {
|
2017-12-28 19:49:02 +01:00
|
|
|
return this.line;
|
2017-05-25 23:46:59 +02:00
|
|
|
},
|
|
|
|
args: function() {
|
2017-12-28 19:49:02 +01:00
|
|
|
return this.parser(this.line);
|
2017-05-25 23:46:59 +02:00
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
};
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-27 02:00:21 +02:00
|
|
|
// I want this helper to be self-maintained, callers must not worry about
|
|
|
|
// this helper cleaning after itself by asking them to reset it when it is no
|
|
|
|
// longer needed. A timer will be used for self-garbage-collect.
|
|
|
|
// Cleaning up 10s after last hit sounds reasonable.
|
|
|
|
|
|
|
|
µBlock.stringDeduplicater = {
|
|
|
|
strings: new Map(),
|
|
|
|
timer: undefined,
|
|
|
|
last: 0,
|
|
|
|
|
|
|
|
lookup: function(s) {
|
2018-06-01 17:49:48 +02:00
|
|
|
let t = this.strings.get(s);
|
2017-05-27 02:00:21 +02:00
|
|
|
if ( t === undefined ) {
|
2018-06-01 17:49:48 +02:00
|
|
|
t = this.strings.set(s, s).get(s);
|
|
|
|
if ( this.timer === undefined ) {
|
|
|
|
this.timer = vAPI.setTimeout(() => { this.cleanup(); }, 10000);
|
|
|
|
}
|
2017-05-27 02:00:21 +02:00
|
|
|
}
|
|
|
|
this.last = Date.now();
|
|
|
|
return t;
|
|
|
|
},
|
|
|
|
|
|
|
|
cleanup: function() {
|
|
|
|
if ( (Date.now() - this.last) < 10000 ) {
|
2018-06-01 17:49:48 +02:00
|
|
|
this.timer = vAPI.setTimeout(() => { this.cleanup(); }, 10000);
|
2017-05-27 02:00:21 +02:00
|
|
|
} else {
|
|
|
|
this.timer = undefined;
|
|
|
|
this.strings.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-09-16 23:41:17 +02:00
|
|
|
µBlock.openNewTab = function(details) {
|
|
|
|
if ( details.url.startsWith('logger-ui.html') ) {
|
|
|
|
if ( details.shiftKey ) {
|
2016-09-17 01:12:16 +02:00
|
|
|
this.changeUserSettings(
|
|
|
|
'alwaysDetachLogger',
|
|
|
|
!this.userSettings.alwaysDetachLogger
|
|
|
|
);
|
2016-09-16 23:41:17 +02:00
|
|
|
}
|
|
|
|
details.popup = this.userSettings.alwaysDetachLogger;
|
2018-12-14 17:01:21 +01:00
|
|
|
if ( details.popup ) {
|
|
|
|
const url = new URL(vAPI.getURL(details.url));
|
|
|
|
url.searchParams.set('popup', '1');
|
|
|
|
details.url = url.href;
|
|
|
|
let popupLoggerBox;
|
|
|
|
try {
|
|
|
|
popupLoggerBox = JSON.parse(
|
|
|
|
vAPI.localStorage.getItem('popupLoggerBox')
|
|
|
|
);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
if ( popupLoggerBox !== undefined ) {
|
|
|
|
details.box = popupLoggerBox;
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 23:41:17 +02:00
|
|
|
}
|
|
|
|
vAPI.tabs.open(details);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-01-27 19:44:52 +01:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
µBlock.MRUCache = function(size) {
|
|
|
|
this.size = size;
|
|
|
|
this.array = [];
|
|
|
|
this.map = new Map();
|
2017-12-21 23:05:25 +01:00
|
|
|
this.resetTime = Date.now();
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.MRUCache.prototype = {
|
|
|
|
add: function(key, value) {
|
|
|
|
var found = this.map.has(key);
|
|
|
|
this.map.set(key, value);
|
|
|
|
if ( !found ) {
|
|
|
|
if ( this.array.length === this.size ) {
|
|
|
|
this.map.delete(this.array.pop());
|
|
|
|
}
|
|
|
|
this.array.unshift(key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remove: function(key) {
|
|
|
|
if ( this.map.has(key) ) {
|
|
|
|
this.array.splice(this.array.indexOf(key), 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lookup: function(key) {
|
|
|
|
var value = this.map.get(key);
|
|
|
|
if ( value !== undefined && this.array[0] !== key ) {
|
2017-12-22 15:37:26 +01:00
|
|
|
var i = this.array.indexOf(key);
|
|
|
|
do {
|
|
|
|
this.array[i] = this.array[i-1];
|
|
|
|
} while ( --i );
|
|
|
|
this.array[0] = key;
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
reset: function() {
|
|
|
|
this.array = [];
|
|
|
|
this.map.clear();
|
2017-12-21 23:05:25 +01:00
|
|
|
this.resetTime = Date.now();
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-11-09 18:53:05 +01:00
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
|
|
|
|
|
|
|
|
µBlock.escapeRegex = function(s) {
|
|
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2018-06-03 19:27:42 +02:00
|
|
|
|
|
|
|
µBlock.decomposeHostname = (function() {
|
|
|
|
// For performance purpose, as simple tests as possible
|
|
|
|
let reHostnameVeryCoarse = /[g-z_-]/;
|
|
|
|
let reIPv4VeryCoarse = /\.\d+$/;
|
|
|
|
|
|
|
|
let toBroaderHostname = function(hostname) {
|
|
|
|
let pos = hostname.indexOf('.');
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
return hostname.slice(pos + 1);
|
|
|
|
}
|
|
|
|
return hostname !== '*' && hostname !== '' ? '*' : '';
|
|
|
|
};
|
|
|
|
|
2018-08-09 17:31:25 +02:00
|
|
|
let toBroaderIPv4Address = function(ipaddress) {
|
|
|
|
if ( ipaddress === '*' || ipaddress === '' ) { return ''; }
|
|
|
|
let pos = ipaddress.lastIndexOf('.');
|
|
|
|
if ( pos === -1 ) { return '*'; }
|
|
|
|
return ipaddress.slice(0, pos);
|
|
|
|
};
|
|
|
|
|
|
|
|
let toBroaderIPv6Address = function(ipaddress) {
|
2018-06-03 19:27:42 +02:00
|
|
|
return ipaddress !== '*' && ipaddress !== '' ? '*' : '';
|
|
|
|
};
|
|
|
|
|
|
|
|
return function decomposeHostname(hostname, decomposed) {
|
|
|
|
if ( decomposed.length === 0 || decomposed[0] !== hostname ) {
|
2018-08-09 17:31:25 +02:00
|
|
|
let broaden;
|
|
|
|
if ( reHostnameVeryCoarse.test(hostname) === false ) {
|
2018-09-03 22:15:51 +02:00
|
|
|
if ( reIPv4VeryCoarse.test(hostname) ) {
|
2018-08-09 17:31:25 +02:00
|
|
|
broaden = toBroaderIPv4Address;
|
|
|
|
} else if ( hostname.startsWith('[') ) {
|
|
|
|
broaden = toBroaderIPv6Address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( broaden === undefined ) {
|
|
|
|
broaden = toBroaderHostname;
|
|
|
|
}
|
2018-06-03 19:27:42 +02:00
|
|
|
decomposed[0] = hostname;
|
|
|
|
let i = 1;
|
|
|
|
for (;;) {
|
|
|
|
hostname = broaden(hostname);
|
|
|
|
if ( hostname === '' ) { break; }
|
|
|
|
decomposed[i++] = hostname;
|
|
|
|
}
|
|
|
|
decomposed.length = i;
|
|
|
|
}
|
|
|
|
return decomposed;
|
|
|
|
};
|
|
|
|
})();
|
2018-10-23 19:01:08 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// TODO: evaluate using TextEncoder/TextDecoder
|
|
|
|
|
|
|
|
µBlock.orphanizeString = function(s) {
|
|
|
|
return JSON.parse(JSON.stringify(s));
|
2018-12-14 17:01:21 +01:00
|
|
|
};
|