/usr/share/chromium/extensions/ublock-origin/js/utils.js is in chromium-ublock-origin 1.13.8+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | /*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2017 Raymond Hill
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
*/
'use strict';
/******************************************************************************/
// 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.
//
// https://github.com/gorhill/uBlock/issues/2630
// Slice input URL into a list of safe-integer token values, instead of a list
// 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).
// Benchmark for string-based tokens vs. safe-integer token values:
// https://gorhill.github.io/obj-vs-set-vs-map/tokenize-to-str-vs-to-int.html
µ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;
},
tokenHashFromString: function(s) {
var l = s.length;
if ( l === 0 ) { return 0; }
if ( l === 1 ) {
if ( s === '*' ) { return 63; }
if ( s === '.' ) { return 62; }
}
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;
},
// https://github.com/chrisaljoudi/uBlock/issues/1118
// We limit to a maximum number of tokens.
_tokenize: function() {
var tokens = this._tokens,
url = this._urlOut,
l = url.length;
if ( l === 0 ) { tokens[0] = 0; return; }
if ( l > 2048 ) {
url = url.slice(0, 2048);
l = 2048;
}
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; }
if ( n === 8 ) { continue; }
th = th * 64 + v;
n += 1;
}
tokens[j++] = th;
tokens[j++] = ti;
}
},
_urlIn: '',
_urlOut: '',
_tokenized: false,
_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;
})()
};
/******************************************************************************/
µBlock.formatCount = function(count) {
if ( typeof count !== 'number' ) {
return '';
}
var s = count.toFixed(0);
if ( count >= 1000 ) {
if ( count < 10000 ) {
s = '>' + s.slice(0,1) + 'k';
} else if ( count < 100000 ) {
s = s.slice(0,2) + 'k';
} else if ( count < 1000000 ) {
s = s.slice(0,3) + 'k';
} else if ( count < 10000000 ) {
s = s.slice(0,1) + 'M';
} else {
s = s.slice(0,-6) + 'M';
}
}
return s;
};
// https://www.youtube.com/watch?v=DyvzfyqYm_s
/******************************************************************************/
µBlock.dateNowToSensibleString = function() {
var now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
return now.toISOString().replace(/\.\d+Z$/, '')
.replace(/:/g, '.')
.replace('T', '_');
};
/******************************************************************************/
µBlock.LineIterator = function(text, offset) {
this.text = text;
this.textLen = this.text.length;
this.offset = offset || 0;
};
µBlock.LineIterator.prototype.next = function(offset) {
if ( offset !== undefined ) {
this.offset += offset;
}
var lineEnd = this.text.indexOf('\n', this.offset);
if ( lineEnd === -1 ) {
lineEnd = this.text.indexOf('\r', this.offset);
if ( lineEnd === -1 ) {
lineEnd = this.textLen;
}
}
var line = this.text.slice(this.offset, lineEnd);
this.offset = lineEnd + 1;
return line;
};
µBlock.LineIterator.prototype.charCodeAt = function(offset) {
return this.text.charCodeAt(this.offset + offset);
};
µBlock.LineIterator.prototype.eot = function() {
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;
};
µBlock.FieldIterator.prototype.remainder = function() {
return this.text.slice(this.offset);
};
/******************************************************************************/
µBlock.CompiledLineWriter = function() {
this.output = [];
this.stringifier = JSON.stringify;
};
µBlock.CompiledLineWriter.fingerprint = function(args) {
return JSON.stringify(args);
};
µBlock.CompiledLineWriter.prototype = {
push: function(args) {
this.output[this.output.length] = this.stringifier(args);
},
toString: function() {
return this.output.join('\n');
}
};
µBlock.CompiledLineReader = function(raw) {
this.reset(raw);
this.parser = JSON.parse;
};
µBlock.CompiledLineReader.prototype = {
reset: function(raw) {
this.input = raw;
this.len = raw.length;
this.offset = 0;
this.s = '';
return this;
},
next: function() {
if ( this.offset === this.len ) {
this.s = '';
return false;
}
var pos = this.input.indexOf('\n', this.offset);
if ( pos !== -1 ) {
this.s = this.input.slice(this.offset, pos);
this.offset = pos + 1;
} else {
this.s = this.input.slice(this.offset);
this.offset = this.len;
}
return true;
},
fingerprint: function() {
return this.s;
},
args: function() {
return this.parser(this.s);
}
};
/******************************************************************************/
// 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.
// https://github.com/gorhill/uBlock/issues/2656
// Can't use chained calls if we want to support legacy Map().
µBlock.stringDeduplicater = {
strings: new Map(),
timer: undefined,
last: 0,
lookup: function(s) {
var t = this.strings.get(s);
if ( t === undefined ) {
this.strings.set(s, s);
t = this.strings.get(s);
if ( this.timer === undefined ) { this.cleanupAsync(); }
}
this.last = Date.now();
return t;
},
cleanupAsync: function() {
this.timer = vAPI.setTimeout(this.cleanup.bind(this), 10000);
},
cleanup: function() {
if ( (Date.now() - this.last) < 10000 ) {
this.timer = vAPI.setTimeout(this.cleanup.bind(this), 10000);
} else {
this.timer = undefined;
this.strings.clear();
}
}
};
/******************************************************************************/
µBlock.mapToArray = typeof Array.from === 'function'
? Array.from
: function(map) {
var out = [];
for ( var entry of map ) {
out.push(entry);
}
return out;
};
µBlock.mapFromArray = function(arr) {
return new Map(arr);
};
µBlock.setToArray = typeof Array.from === 'function'
? Array.from
: function(dict) {
var out = [];
for ( var value of dict ) {
out.push(value);
}
return out;
};
µBlock.setFromArray = function(arr) {
return new Set(arr);
};
/******************************************************************************/
µBlock.openNewTab = function(details) {
if ( details.url.startsWith('logger-ui.html') ) {
if ( details.shiftKey ) {
this.changeUserSettings(
'alwaysDetachLogger',
!this.userSettings.alwaysDetachLogger
);
}
details.popup = this.userSettings.alwaysDetachLogger;
}
vAPI.tabs.open(details);
};
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/2344
µBlock.matchCurrentLanguage = function(s) {
if ( typeof s !== 'string' ) { return false; }
if ( this.matchCurrentLanguage.reLang === undefined ) {
this.matchCurrentLanguage.reLang = new RegExp('\\b' + self.navigator.language.slice(0, 2) + '\\b');
}
return this.matchCurrentLanguage.reLang.test(s);
};
/******************************************************************************/
|