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
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import µb from './background.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.formatCount = function(count) {
|
2014-08-20 02:41:52 +02:00
|
|
|
if ( typeof count !== 'number' ) {
|
|
|
|
return '';
|
|
|
|
}
|
2019-09-11 14:08:30 +02:00
|
|
|
let s = count.toFixed(0);
|
2014-08-20 02:41:52 +02:00
|
|
|
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
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.dateNowToSensibleString = function() {
|
2019-09-11 14:08:30 +02:00
|
|
|
const now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
|
2016-10-13 19:25:57 +02:00
|
|
|
return now.toISOString().replace(/\.\d+Z$/, '')
|
|
|
|
.replace(/:/g, '.')
|
|
|
|
.replace('T', '_');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.openNewTab = function(details) {
|
2016-09-16 23:41:17 +02:00
|
|
|
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
|
|
|
}
|
2019-09-06 17:41:07 +02:00
|
|
|
if ( this.userSettings.alwaysDetachLogger ) {
|
|
|
|
details.popup = this.hiddenSettings.loggerPopupType;
|
2018-12-14 17:01:21 +01:00
|
|
|
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
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.MRUCache = class {
|
2019-09-11 14:08:30 +02:00
|
|
|
constructor(size) {
|
|
|
|
this.size = size;
|
|
|
|
this.array = [];
|
|
|
|
this.map = new Map();
|
|
|
|
this.resetTime = Date.now();
|
|
|
|
}
|
|
|
|
add(key, value) {
|
|
|
|
const found = this.map.has(key);
|
2017-10-21 19:43:46 +02:00
|
|
|
this.map.set(key, value);
|
|
|
|
if ( !found ) {
|
|
|
|
if ( this.array.length === this.size ) {
|
|
|
|
this.map.delete(this.array.pop());
|
|
|
|
}
|
|
|
|
this.array.unshift(key);
|
|
|
|
}
|
2019-09-11 14:08:30 +02:00
|
|
|
}
|
|
|
|
remove(key) {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( this.map.has(key) ) {
|
|
|
|
this.array.splice(this.array.indexOf(key), 1);
|
|
|
|
}
|
2019-09-11 14:08:30 +02:00
|
|
|
}
|
|
|
|
lookup(key) {
|
|
|
|
const value = this.map.get(key);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( value !== undefined && this.array[0] !== key ) {
|
2019-09-11 14:08:30 +02:00
|
|
|
let i = this.array.indexOf(key);
|
2017-12-22 15:37:26 +01:00
|
|
|
do {
|
|
|
|
this.array[i] = this.array[i-1];
|
|
|
|
} while ( --i );
|
|
|
|
this.array[0] = key;
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
return value;
|
2019-09-11 14:08:30 +02:00
|
|
|
}
|
|
|
|
reset() {
|
2017-10-21 19:43:46 +02:00
|
|
|
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
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.escapeRegex = function(s) {
|
2017-11-09 18:53:05 +01:00
|
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2018-06-03 19:27:42 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.fireDOMEvent = function(name) {
|
2019-08-10 16:57:24 +02:00
|
|
|
if (
|
|
|
|
window instanceof Object &&
|
|
|
|
window.dispatchEvent instanceof Function &&
|
|
|
|
window.CustomEvent instanceof Function
|
|
|
|
) {
|
|
|
|
window.dispatchEvent(new CustomEvent(name));
|
|
|
|
}
|
|
|
|
};
|
2021-01-16 16:35:56 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-01-31 16:30:12 +01:00
|
|
|
// TODO: properly compare arrays
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.getModifiedSettings = function(edit, orig = {}) {
|
2021-01-16 16:35:56 +01:00
|
|
|
const out = {};
|
|
|
|
for ( const prop in edit ) {
|
|
|
|
if ( orig.hasOwnProperty(prop) && edit[prop] !== orig[prop] ) {
|
|
|
|
out[prop] = edit[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.settingValueFromString = function(orig, name, s) {
|
2021-01-16 16:35:56 +01:00
|
|
|
if ( typeof name !== 'string' || typeof s !== 'string' ) { return; }
|
|
|
|
if ( orig.hasOwnProperty(name) === false ) { return; }
|
|
|
|
let r;
|
|
|
|
switch ( typeof orig[name] ) {
|
|
|
|
case 'boolean':
|
|
|
|
if ( s === 'true' ) {
|
|
|
|
r = true;
|
|
|
|
} else if ( s === 'false' ) {
|
|
|
|
r = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
r = s.trim();
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
if ( s.startsWith('0b') ) {
|
|
|
|
r = parseInt(s.slice(2), 2);
|
|
|
|
} else if ( s.startsWith('0x') ) {
|
|
|
|
r = parseInt(s.slice(2), 16);
|
|
|
|
} else {
|
|
|
|
r = parseInt(s, 10);
|
|
|
|
}
|
|
|
|
if ( isNaN(r) ) { r = undefined; }
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|