2015-11-23 13:52:50 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-08-11 16:39:43 +02:00
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2015-11-23 13:52:50 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-10 15:01:05 +02:00
|
|
|
µBlock.redirectEngine = (function(){
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2015-11-26 23:56:30 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-10-31 23:34:54 +01:00
|
|
|
const warResolve = (function() {
|
2018-02-16 15:07:20 +01:00
|
|
|
var warPairs = [];
|
2018-02-15 23:25:38 +01:00
|
|
|
|
2018-02-16 15:07:20 +01:00
|
|
|
var onPairsReady = function() {
|
2018-02-15 23:25:38 +01:00
|
|
|
var reng = µBlock.redirectEngine;
|
2018-02-16 15:07:20 +01:00
|
|
|
for ( var i = 0; i < warPairs.length; i += 2 ) {
|
|
|
|
var resource = reng.resources.get(warPairs[i+0]);
|
2018-02-15 23:25:38 +01:00
|
|
|
if ( resource === undefined ) { continue; }
|
2018-02-16 15:07:20 +01:00
|
|
|
resource.warURL = vAPI.getURL(
|
|
|
|
'/web_accessible_resources/' + warPairs[i+1]
|
2018-02-15 23:25:38 +01:00
|
|
|
);
|
|
|
|
}
|
2018-02-16 15:07:20 +01:00
|
|
|
reng.selfieFromResources();
|
2018-02-15 23:25:38 +01:00
|
|
|
};
|
|
|
|
|
2018-02-16 15:07:20 +01:00
|
|
|
return function() {
|
|
|
|
if ( vAPI.warSecret === undefined || warPairs.length !== 0 ) {
|
|
|
|
return onPairsReady();
|
2018-02-15 23:25:38 +01:00
|
|
|
}
|
2018-02-16 15:07:20 +01:00
|
|
|
|
|
|
|
var onPairsLoaded = function(details) {
|
|
|
|
var marker = '>>>>>';
|
|
|
|
var pos = details.content.indexOf(marker);
|
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
var pairs = details.content.slice(pos + marker.length)
|
|
|
|
.trim()
|
|
|
|
.split('\n');
|
|
|
|
if ( (pairs.length & 1) !== 0 ) { return; }
|
|
|
|
for ( var i = 0; i < pairs.length; i++ ) {
|
|
|
|
pairs[i] = pairs[i].trim();
|
|
|
|
}
|
|
|
|
warPairs = pairs;
|
|
|
|
onPairsReady();
|
|
|
|
};
|
|
|
|
|
|
|
|
µBlock.assets.fetchText(
|
|
|
|
'/web_accessible_resources/imported.txt?secret=' + vAPI.warSecret,
|
|
|
|
onPairsLoaded
|
|
|
|
);
|
2018-02-15 23:25:38 +01:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2018-04-24 11:48:28 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3639
|
|
|
|
// https://github.com/EFForg/https-everywhere/issues/14961
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=111700
|
|
|
|
// Do not redirect to a WAR if the platform suffers from spurious redirect
|
|
|
|
// conflicts, and the request to redirect is not `https:`.
|
|
|
|
// This special handling code can removed once the Chromium issue is fixed.
|
2018-10-31 23:34:54 +01:00
|
|
|
const suffersSpuriousRedirectConflicts = vAPI.webextFlavor.soup.has('chromium');
|
2018-04-24 11:48:28 +02:00
|
|
|
|
2018-02-15 23:25:38 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-10-31 23:34:54 +01:00
|
|
|
const RedirectEntry = function() {
|
2015-11-29 17:04:42 +01:00
|
|
|
this.mime = '';
|
|
|
|
this.data = '';
|
2018-02-15 23:25:38 +01:00
|
|
|
this.warURL = undefined;
|
2015-11-26 23:56:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-02-16 19:37:20 +01:00
|
|
|
// Prevent redirection to web accessible resources when the request is
|
|
|
|
// of type 'xmlhttprequest', because XMLHttpRequest.responseURL would
|
|
|
|
// cause leakage of extension id. See:
|
|
|
|
// - https://stackoverflow.com/a/8056313
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=998076
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
RedirectEntry.prototype.toURL = function(fctxt) {
|
2018-02-18 12:08:48 +01:00
|
|
|
if (
|
|
|
|
this.warURL !== undefined &&
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt instanceof Object &&
|
|
|
|
fctxt.type !== 'xmlhttprequest' &&
|
2018-04-24 11:48:28 +02:00
|
|
|
(
|
|
|
|
suffersSpuriousRedirectConflicts === false ||
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.url.startsWith('https:')
|
2018-04-24 11:48:28 +02:00
|
|
|
)
|
2018-02-18 12:08:48 +01:00
|
|
|
) {
|
|
|
|
return this.warURL + '?secret=' + vAPI.warSecret;
|
2018-02-15 23:25:38 +01:00
|
|
|
}
|
2015-12-23 17:02:36 +01:00
|
|
|
if ( this.data.startsWith('data:') === false ) {
|
|
|
|
if ( this.mime.indexOf(';') === -1 ) {
|
|
|
|
this.data = 'data:' + this.mime + ';base64,' + btoa(this.data);
|
|
|
|
} else {
|
|
|
|
this.data = 'data:' + this.mime + ',' + this.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.data;
|
|
|
|
};
|
2015-11-26 23:56:30 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-12-23 17:02:36 +01:00
|
|
|
RedirectEntry.prototype.toContent = function() {
|
|
|
|
if ( this.data.startsWith('data:') ) {
|
|
|
|
var pos = this.data.indexOf(',');
|
|
|
|
var base64 = this.data.endsWith(';base64', pos);
|
|
|
|
this.data = this.data.slice(pos + 1);
|
|
|
|
if ( base64 ) {
|
|
|
|
this.data = atob(this.data);
|
|
|
|
}
|
2015-11-26 23:56:30 +01:00
|
|
|
}
|
2015-12-23 17:02:36 +01:00
|
|
|
return this.data;
|
2015-11-29 17:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEntry.fromFields = function(mime, lines) {
|
|
|
|
var r = new RedirectEntry();
|
|
|
|
r.mime = mime;
|
2015-12-23 17:02:36 +01:00
|
|
|
r.data = lines.join(mime.indexOf(';') !== -1 ? '' : '\n');
|
2015-11-29 17:04:42 +01:00
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEntry.fromSelfie = function(selfie) {
|
|
|
|
var r = new RedirectEntry();
|
|
|
|
r.mime = selfie.mime;
|
|
|
|
r.data = selfie.data;
|
2018-02-15 23:25:38 +01:00
|
|
|
r.warURL = selfie.warURL;
|
2015-11-29 17:04:42 +01:00
|
|
|
return r;
|
2015-11-26 23:56:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-11-23 13:52:50 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-10-31 23:34:54 +01:00
|
|
|
const RedirectEngine = function() {
|
2016-10-10 15:01:05 +02:00
|
|
|
this.resources = new Map();
|
2015-11-23 13:52:50 +01:00
|
|
|
this.reset();
|
2016-01-07 23:30:56 +01:00
|
|
|
this.resourceNameRegister = '';
|
2016-10-10 15:01:05 +02:00
|
|
|
this._desAll = []; // re-use better than re-allocate
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEngine.prototype.reset = function() {
|
2016-10-10 15:01:05 +02:00
|
|
|
this.rules = new Map();
|
|
|
|
this.ruleTypes = new Set();
|
|
|
|
this.ruleSources = new Set();
|
|
|
|
this.ruleDestinations = new Set();
|
2017-12-21 23:05:25 +01:00
|
|
|
this.modifyTime = Date.now();
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-11-24 01:18:25 +01:00
|
|
|
RedirectEngine.prototype.freeze = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-10 15:01:05 +02:00
|
|
|
RedirectEngine.prototype.toBroaderHostname = function(hostname) {
|
|
|
|
var pos = hostname.indexOf('.');
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
return hostname.slice(pos + 1);
|
|
|
|
}
|
|
|
|
return hostname !== '*' ? '*' : '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
RedirectEngine.prototype.lookup = function(fctxt) {
|
|
|
|
const type = fctxt.type;
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( this.ruleTypes.has(type) === false ) { return; }
|
2018-12-13 18:30:54 +01:00
|
|
|
const desAll = this._desAll;
|
|
|
|
const reqURL = fctxt.url;
|
|
|
|
let src = fctxt.getDocHostname();
|
|
|
|
let des = fctxt.getHostname();
|
|
|
|
let n = 0;
|
2016-10-10 15:01:05 +02:00
|
|
|
for (;;) {
|
|
|
|
if ( this.ruleDestinations.has(des) ) {
|
|
|
|
desAll[n] = des; n += 1;
|
|
|
|
}
|
|
|
|
des = this.toBroaderHostname(des);
|
|
|
|
if ( des === '' ) { break; }
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( n === 0 ) { return; }
|
2015-11-23 13:52:50 +01:00
|
|
|
for (;;) {
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( this.ruleSources.has(src) ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( let i = 0; i < n; i++ ) {
|
|
|
|
const entries = this.rules.get(src + ' ' + desAll[i] + ' ' + type);
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( entries && this.lookupToken(entries, reqURL) ) {
|
|
|
|
return this.resourceNameRegister;
|
2015-11-23 15:49:50 +01:00
|
|
|
}
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
src = this.toBroaderHostname(src);
|
|
|
|
if ( src === '' ) { break; }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RedirectEngine.prototype.lookupToken = function(entries, reqURL) {
|
2018-10-31 23:34:54 +01:00
|
|
|
let j = entries.length;
|
2016-10-10 15:01:05 +02:00
|
|
|
while ( j-- ) {
|
2018-10-31 23:34:54 +01:00
|
|
|
let entry = entries[j];
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( entry.pat instanceof RegExp === false ) {
|
|
|
|
entry.pat = new RegExp(entry.pat, 'i');
|
|
|
|
}
|
|
|
|
if ( entry.pat.test(reqURL) ) {
|
|
|
|
this.resourceNameRegister = entry.tok;
|
|
|
|
return true;
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
RedirectEngine.prototype.toURL = function(fctxt) {
|
|
|
|
let token = this.lookup(fctxt);
|
2018-02-16 19:37:20 +01:00
|
|
|
if ( token === undefined ) { return; }
|
2018-10-31 23:34:54 +01:00
|
|
|
let entry = this.resources.get(token);
|
2015-11-26 23:56:30 +01:00
|
|
|
if ( entry !== undefined ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
return entry.toURL(fctxt);
|
2015-11-26 23:56:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEngine.prototype.matches = function(context) {
|
|
|
|
var token = this.lookup(context);
|
2016-10-10 15:01:05 +02:00
|
|
|
return token !== undefined && this.resources.has(token);
|
2015-11-26 23:56:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-11-24 01:18:25 +01:00
|
|
|
RedirectEngine.prototype.addRule = function(src, des, type, pattern, redirect) {
|
2016-10-10 15:01:05 +02:00
|
|
|
this.ruleSources.add(src);
|
|
|
|
this.ruleDestinations.add(des);
|
|
|
|
this.ruleTypes.add(type);
|
|
|
|
var key = src + ' ' + des + ' ' + type,
|
|
|
|
entries = this.rules.get(key);
|
|
|
|
if ( entries === undefined ) {
|
|
|
|
this.rules.set(key, [ { tok: redirect, pat: pattern } ]);
|
2017-12-21 23:05:25 +01:00
|
|
|
this.modifyTime = Date.now();
|
2016-10-10 15:01:05 +02:00
|
|
|
return;
|
2015-11-24 01:18:25 +01:00
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
var entry;
|
|
|
|
for ( var i = 0, n = entries.length; i < n; i++ ) {
|
|
|
|
entry = entries[i];
|
|
|
|
if ( redirect === entry.tok ) { break; }
|
2016-01-22 00:30:06 +01:00
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( i === n ) {
|
|
|
|
entries.push({ tok: redirect, pat: pattern });
|
2016-01-22 00:30:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
var p = entry.pat;
|
2016-01-22 00:30:06 +01:00
|
|
|
if ( p instanceof RegExp ) {
|
|
|
|
p = p.source;
|
2015-11-24 01:18:25 +01:00
|
|
|
}
|
2016-01-22 00:30:06 +01:00
|
|
|
// Duplicate?
|
|
|
|
var pos = p.indexOf(pattern);
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
if ( pos === 0 || p.charAt(pos - 1) === '|' ) {
|
|
|
|
pos += pattern.length;
|
2016-10-10 15:01:05 +02:00
|
|
|
if ( pos === p.length || p.charAt(pos) === '|' ) { return; }
|
2016-01-22 00:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
entry.pat = p + '|' + pattern;
|
2015-11-24 01:18:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEngine.prototype.fromCompiledRule = function(line) {
|
|
|
|
var fields = line.split('\t');
|
|
|
|
if ( fields.length !== 5 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.addRule(fields[0], fields[1], fields[2], fields[3], fields[4]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
|
2018-12-17 13:46:04 +01:00
|
|
|
const matches = this.reFilterParser.exec(line);
|
|
|
|
if ( matches === null || matches.length !== 4 ) { return; }
|
|
|
|
|
|
|
|
let des = matches[1] || '';
|
|
|
|
const pattern = (des + matches[2]).replace(/[.+?{}()|[\]\/\\]/g, '\\$&')
|
|
|
|
.replace(/\^/g, '[^\\w.%-]')
|
|
|
|
.replace(/\*/g, '.*?');
|
|
|
|
let type,
|
2016-10-26 14:59:15 +02:00
|
|
|
redirect = '',
|
2018-12-17 13:46:04 +01:00
|
|
|
srcs = [];
|
|
|
|
for ( const option of matches[3].split(',') ) {
|
2015-12-15 16:40:40 +01:00
|
|
|
if ( option.startsWith('redirect=') ) {
|
2015-11-24 01:18:25 +01:00
|
|
|
redirect = option.slice(9);
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-15 16:40:40 +01:00
|
|
|
if ( option.startsWith('domain=') ) {
|
2015-11-24 01:18:25 +01:00
|
|
|
srcs = option.slice(7).split('|');
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-17 13:46:04 +01:00
|
|
|
if ( option === 'first-party' || option === '1p' ) {
|
|
|
|
srcs.push(µBlock.URI.domainFromHostnameNoCache(des) || des);
|
2015-12-16 03:34:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-25 16:05:23 +01:00
|
|
|
// One and only one type must be specified.
|
2018-12-17 13:46:04 +01:00
|
|
|
if ( this.supportedTypes.has(option) ) {
|
|
|
|
if ( type !== undefined ) { return; }
|
|
|
|
type = this.supportedTypes.get(option);
|
2015-11-24 01:18:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 05:34:03 +01:00
|
|
|
// Need a resource token.
|
2018-12-17 13:46:04 +01:00
|
|
|
if ( redirect === '' ) { return; }
|
2015-11-24 05:34:03 +01:00
|
|
|
|
2015-11-25 16:05:23 +01:00
|
|
|
// Need one single type -- not negated.
|
2018-12-17 13:46:04 +01:00
|
|
|
if ( type === undefined ) { return; }
|
2015-11-24 01:18:25 +01:00
|
|
|
|
|
|
|
if ( des === '' ) {
|
|
|
|
des = '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( srcs.length === 0 ) {
|
|
|
|
srcs.push('*');
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:46:04 +01:00
|
|
|
const out = [];
|
|
|
|
for ( const src of srcs ) {
|
|
|
|
if ( src === '' ) { continue; }
|
|
|
|
if ( src.startsWith('~') ) { continue; }
|
2015-11-25 16:05:23 +01:00
|
|
|
out.push(src + '\t' + des + '\t' + type + '\t' + pattern + '\t' + redirect);
|
2015-11-24 01:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-04-27 16:08:32 +02:00
|
|
|
RedirectEngine.prototype.reFilterParser = /^(?:\|\|([^\/:?#^*]+)|\*)([^$]+)\$([^$]+)$/;
|
2015-11-24 01:18:25 +01:00
|
|
|
|
2018-12-17 13:46:04 +01:00
|
|
|
RedirectEngine.prototype.supportedTypes = new Map([
|
|
|
|
[ 'css', 'stylesheet' ],
|
|
|
|
[ 'font', 'font' ],
|
|
|
|
[ 'image', 'image' ],
|
|
|
|
[ 'media', 'media' ],
|
|
|
|
[ 'object', 'object' ],
|
|
|
|
[ 'script', 'script' ],
|
|
|
|
[ 'stylesheet', 'stylesheet' ],
|
|
|
|
[ 'frame', 'sub_frame' ],
|
|
|
|
[ 'subdocument', 'sub_frame' ],
|
|
|
|
[ 'xhr', 'xmlhttprequest' ],
|
|
|
|
[ 'xmlhttprequest', 'xmlhttprequest' ],
|
|
|
|
]);
|
2015-11-24 01:18:25 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-11-29 17:04:42 +01:00
|
|
|
RedirectEngine.prototype.toSelfie = function() {
|
2016-10-10 15:01:05 +02:00
|
|
|
// Because rules may contains RegExp instances, we need to manually
|
|
|
|
// convert it to a serializable format. The serialized format must be
|
|
|
|
// suitable to be used as an argument to the Map() constructor.
|
|
|
|
var rules = [],
|
2017-05-19 16:12:55 +02:00
|
|
|
rule, entries, i, entry;
|
|
|
|
for ( var item of this.rules ) {
|
|
|
|
rule = [ item[0], [] ];
|
|
|
|
entries = item[1];
|
2016-10-10 15:01:05 +02:00
|
|
|
i = entries.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
entry = entries[i];
|
|
|
|
rule[1].push({
|
|
|
|
tok: entry.tok,
|
|
|
|
pat: entry.pat instanceof RegExp ? entry.pat.source : entry.pat
|
|
|
|
});
|
2015-11-29 23:06:58 +01:00
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
rules.push(rule);
|
2015-11-29 23:06:58 +01:00
|
|
|
}
|
2016-10-10 15:01:05 +02:00
|
|
|
return {
|
|
|
|
rules: rules,
|
2018-06-01 13:54:31 +02:00
|
|
|
ruleTypes: Array.from(this.ruleTypes),
|
|
|
|
ruleSources: Array.from(this.ruleSources),
|
|
|
|
ruleDestinations: Array.from(this.ruleDestinations)
|
2016-10-10 15:01:05 +02:00
|
|
|
};
|
2015-11-29 17:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
RedirectEngine.prototype.fromSelfie = function(selfie) {
|
2017-10-21 19:43:46 +02:00
|
|
|
this.rules = new Map(selfie.rules);
|
|
|
|
this.ruleTypes = new Set(selfie.ruleTypes);
|
|
|
|
this.ruleSources = new Set(selfie.ruleSources);
|
|
|
|
this.ruleDestinations = new Set(selfie.ruleDestinations);
|
2017-12-21 23:05:25 +01:00
|
|
|
this.modifyTime = Date.now();
|
2015-11-29 17:04:42 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-12-23 17:02:36 +01:00
|
|
|
RedirectEngine.prototype.resourceURIFromName = function(name, mime) {
|
2016-10-10 15:01:05 +02:00
|
|
|
var entry = this.resources.get(name);
|
2015-12-22 22:32:09 +01:00
|
|
|
if ( entry && (mime === undefined || entry.mime.startsWith(mime)) ) {
|
|
|
|
return entry.toURL();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-12-23 17:02:36 +01:00
|
|
|
RedirectEngine.prototype.resourceContentFromName = function(name, mime) {
|
2016-10-10 15:01:05 +02:00
|
|
|
var entry;
|
|
|
|
for (;;) {
|
|
|
|
entry = this.resources.get(name);
|
|
|
|
if ( entry === undefined ) { return; }
|
|
|
|
if ( entry.mime.startsWith('alias/') === false ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
name = entry.mime.slice(6);
|
|
|
|
}
|
|
|
|
if ( mime === undefined || entry.mime.startsWith(mime) ) {
|
2015-12-23 17:02:36 +01:00
|
|
|
return entry.toContent();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-11-24 01:18:25 +01:00
|
|
|
// TODO: combine same key-redirect pairs into a single regex.
|
|
|
|
|
2018-10-31 23:34:54 +01:00
|
|
|
// https://github.com/uBlockOrigin/uAssets/commit/deefe875551197d655f79cb540e62dfc17c95f42
|
|
|
|
// Consider 'none' a reserved keyword, to be used to disable redirection.
|
|
|
|
|
2015-11-24 05:34:03 +01:00
|
|
|
RedirectEngine.prototype.resourcesFromString = function(text) {
|
2018-10-31 23:34:54 +01:00
|
|
|
let fields, encoded,
|
2017-01-18 19:17:47 +01:00
|
|
|
reNonEmptyLine = /\S/,
|
|
|
|
lineIter = new µBlock.LineIterator(text);
|
2015-11-24 19:21:14 +01:00
|
|
|
|
2016-10-10 15:01:05 +02:00
|
|
|
this.resources = new Map();
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
while ( lineIter.eot() === false ) {
|
2018-10-31 23:34:54 +01:00
|
|
|
let line = lineIter.next();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( line.startsWith('#') ) { continue; }
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2015-11-24 19:21:14 +01:00
|
|
|
if ( fields === undefined ) {
|
2018-10-31 23:34:54 +01:00
|
|
|
let head = line.trim().split(/\s+/);
|
|
|
|
if ( head.length !== 2 ) { continue; }
|
|
|
|
if ( head[0] === 'none' ) { continue; }
|
|
|
|
encoded = head[1].indexOf(';') !== -1;
|
|
|
|
fields = head;
|
2015-11-23 13:52:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:38:05 +01:00
|
|
|
if ( reNonEmptyLine.test(line) ) {
|
|
|
|
fields.push(encoded ? line.trim() : line);
|
2015-11-23 13:52:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-24 05:34:03 +01:00
|
|
|
|
2015-11-24 19:21:14 +01:00
|
|
|
// No more data, add the resource.
|
2018-10-31 23:34:54 +01:00
|
|
|
this.resources.set(
|
|
|
|
fields[0],
|
|
|
|
RedirectEntry.fromFields(fields[1], fields.slice(2))
|
|
|
|
);
|
2015-11-24 19:21:14 +01:00
|
|
|
|
|
|
|
fields = undefined;
|
|
|
|
}
|
2015-11-24 05:34:03 +01:00
|
|
|
|
2015-11-24 19:21:14 +01:00
|
|
|
// Process pending resource data.
|
|
|
|
if ( fields !== undefined ) {
|
2018-10-31 23:34:54 +01:00
|
|
|
this.resources.set(
|
|
|
|
fields[0],
|
|
|
|
RedirectEntry.fromFields(fields[1], fields.slice(2))
|
|
|
|
);
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
2018-02-16 15:07:20 +01:00
|
|
|
|
|
|
|
warResolve();
|
2018-03-19 15:25:03 +01:00
|
|
|
|
|
|
|
this.modifyTime = Date.now();
|
2015-11-23 13:52:50 +01:00
|
|
|
};
|
|
|
|
|
2018-02-15 23:25:38 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-08-22 14:13:10 +02:00
|
|
|
let resourcesSelfieVersion = 3;
|
2018-02-15 23:25:38 +01:00
|
|
|
|
|
|
|
RedirectEngine.prototype.selfieFromResources = function() {
|
2018-08-22 14:13:10 +02:00
|
|
|
let selfie = {
|
|
|
|
version: resourcesSelfieVersion,
|
|
|
|
resources: Array.from(this.resources)
|
|
|
|
};
|
|
|
|
µBlock.cacheStorage.set({ resourcesSelfie: JSON.stringify(selfie) });
|
2018-02-15 23:25:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
RedirectEngine.prototype.resourcesFromSelfie = function(callback) {
|
2018-08-22 14:13:10 +02:00
|
|
|
µBlock.cacheStorage.get('resourcesSelfie', bin => {
|
|
|
|
let selfie = bin && bin.resourcesSelfie;
|
|
|
|
if ( typeof selfie === 'string' ) {
|
|
|
|
try {
|
|
|
|
selfie = JSON.parse(selfie);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
2018-02-15 23:25:38 +01:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
selfie instanceof Object === false ||
|
|
|
|
selfie.version !== resourcesSelfieVersion ||
|
|
|
|
Array.isArray(selfie.resources) === false
|
|
|
|
) {
|
|
|
|
return callback(false);
|
|
|
|
}
|
2018-08-22 14:13:10 +02:00
|
|
|
this.resources = new Map();
|
|
|
|
for ( let entry of selfie.resources ) {
|
|
|
|
this.resources.set(entry[0], RedirectEntry.fromSelfie(entry[1]));
|
2018-02-15 23:25:38 +01:00
|
|
|
}
|
|
|
|
callback(true);
|
2018-08-22 14:13:10 +02:00
|
|
|
});
|
2018-02-15 23:25:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
RedirectEngine.prototype.invalidateResourcesSelfie = function() {
|
2018-08-11 16:39:43 +02:00
|
|
|
µBlock.cacheStorage.remove('resourcesSelfie');
|
2018-02-15 23:25:38 +01:00
|
|
|
};
|
|
|
|
|
2015-11-26 23:56:30 +01:00
|
|
|
/******************************************************************************/
|
2015-11-23 13:52:50 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return new RedirectEngine();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|