blob: 5389afd82dc96e01f3053584f9992b5d31c84124 (
plain)
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
|
/*
*
* This file is part of Privacy Badger <https://www.eff.org/privacybadger>
* Copyright (C) 2016 Electronic Frontier Foundation
*
* Privacy Badger is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Privacy Badger 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 Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
*/
require.scopes.surrogates = (function() {
const db = require('surrogatedb');
/**
* Blocking tracking scripts (trackers) can cause parts of webpages to break.
* Surrogate scripts are dummy pieces of JavaScript meant to supply just enough
* of the original tracker's functionality to allow pages to continue working.
*
* This method gets called within request-blocking listeners:
* It needs to be fast!
*
* @param {String} script_url The full URL of the script resource being requested.
*
* @param {String} script_hostname The hostname component of the script_url
* parameter. This is an optimization: the calling context should already have
* this information.
*
* @return {(String|Boolean)} The surrogate script as a data URI when there is a
* match, or boolean false when there is no match.
*/
function getSurrogateURI(script_url, script_hostname) {
// do we have an entry for the script hostname?
if (db.hostnames.hasOwnProperty(script_hostname)) {
const tokens = db.hostnames[script_hostname];
// it's a wildcard token
if (_.isString(tokens)) {
if (db.surrogates.hasOwnProperty(tokens)) {
// return the surrogate code
return 'data:application/javascript;base64,' + btoa(db.surrogates[tokens]);
}
}
// must be an array of suffix tokens
const qs_start = script_url.indexOf('?');
for (let i = 0; i < tokens.length; i++) {
// do any of the suffix tokens match the script URL?
const token = tokens[i];
let match = false;
if (qs_start == -1) {
if (script_url.endsWith(token)) {
match = true;
}
} else {
if (script_url.endsWith(token, qs_start)) {
match = true;
}
}
if (match) {
// there is a match, return the surrogate code
return 'data:application/javascript;base64,' + btoa(db.surrogates[token]);
}
}
}
return false;
}
const exports = {
getSurrogateURI: getSurrogateURI,
};
return exports;
})();
|