-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentFiltering.js
executable file
·313 lines (269 loc) · 10.4 KB
/
contentFiltering.js
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
/*
* Copyright (c) 2021 Hieu Le and the UCI Networking Group
* <https://athinagroup.eng.uci.edu>.
*
* CV-Inspector Adblock Plus 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.
*
* CV-Inspector Adblock Plus 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 CV-Inspector Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/** @module contentFiltering */
"use strict";
const {contentTypes} = require("../adblockpluscore/lib/contentTypes");
const {elemHide, createStyleSheet,
rulesFromStyleSheet} = require("../adblockpluscore/lib/elemHide");
const {elemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
const {snippets, compileScript} = require("../adblockpluscore/lib/snippets");
const {checkWhitelisted} = require("./whitelisting");
const {extractHostFromFrame} = require("./url");
const {port} = require("./messaging");
const {HitLogger, logRequest} = require("./hitLogger");
const info = require("info");
// Chromium's support for tabs.removeCSS is still a work in progress and the
// API is likely to be different from Firefox's; for now we just don't use it
// at all, even if it's available.
// See https://crbug.com/608854
const styleSheetRemovalSupported = info.platform == "gecko";
let userStyleSheetsSupported = true;
let snippetsLibrarySource = "";
let executableCode = new Map();
function addStyleSheet(tabId, frameId, styleSheet)
{
try
{
let promise = browser.tabs.insertCSS(tabId, {
code: styleSheet,
cssOrigin: "user",
frameId,
matchAboutBlank: true,
runAt: "document_start"
});
// See error handling notes in the catch block.
promise.catch(() => {});
}
catch (error)
{
// If the error is about the "cssOrigin" option, this is an older version
// of Chromium (65 and below) or Firefox (52 and below) that does not
// support user style sheets.
if (/\bcssOrigin\b/.test(error.message))
userStyleSheetsSupported = false;
// For other errors, we simply return false to indicate failure.
//
// One common error that occurs frequently is when a frame is not found
// (e.g. "Error: No frame with id 574 in tab 266"), which can happen when
// the code in the parent document has removed the frame before the
// background page has had a chance to respond to the content script's
// "content.applyFilters" message. We simply ignore such errors, because
// otherwise they show up in the log too often and make debugging
// difficult.
//
// Also note that the missing frame error is thrown synchronously on
// Firefox, while on Chromium it is an asychronous promise rejection. In
// the latter case, we cannot indicate failure to the caller, but we still
// explicitly ignore the error.
return false;
}
return true;
}
function removeStyleSheet(tabId, frameId, styleSheet)
{
if (!styleSheetRemovalSupported)
return;
browser.tabs.removeCSS(tabId, {
code: styleSheet,
cssOrigin: "user",
frameId,
matchAboutBlank: true
});
}
function updateFrameStyles(tabId, frameId, styleSheet, groupName = "standard",
appendOnly = false)
{
let frame = ext.getFrame(tabId, frameId);
if (!frame)
return false;
if (!frame.state.injectedStyleSheets)
frame.state.injectedStyleSheets = new Map();
let oldStyleSheet = frame.state.injectedStyleSheets.get(groupName);
if (appendOnly && oldStyleSheet)
styleSheet = oldStyleSheet + styleSheet;
// Ideally we would compare the old and new style sheets and skip this code
// if they're the same. But first we need to ensure that there are no edge
// cases that would cause the old style sheet to be a leftover from a
// previous instance of the frame (see issue #7180). For now, we add the new
// style sheet regardless.
// Add the new style sheet first to keep previously hidden elements from
// reappearing momentarily.
if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
return false;
// Sometimes the old and new style sheets can be exactly the same. In such a
// case, do not remove the "old" style sheet, because it is in fact the new
// style sheet now.
if (oldStyleSheet && oldStyleSheet != styleSheet)
removeStyleSheet(tabId, frameId, oldStyleSheet);
// The standard style sheet is ~660 KB per frame (as of Adblock Plus 3.3.2).
// Keeping it in memory would only really be useful on Firefox, which allows
// us to remove it via the tabs.removeCSS API. By choosing not to hold on to
// it, we save potentially several megabytes per tab (#6967).
if (groupName != "standard")
frame.state.injectedStyleSheets.set(groupName, styleSheet);
return true;
}
function getExecutableCode(script)
{
let code = executableCode.get(script);
if (code)
return code;
code = compileScript(script, [snippetsLibrarySource]);
executableCode.set(script, code);
return code;
}
function executeScript(script, tabId, frameId)
{
try
{
let details = {
code: getExecutableCode(script),
matchAboutBlank: true,
runAt: "document_start"
};
// Microsoft Edge throws when passing frameId to tabs.executeScript
// and always executes code in the context of the top-level frame,
// so for sub-frames we let it fail.
if (frameId != 0)
details.frameId = frameId;
return browser.tabs.executeScript(tabId, details).catch(error =>
{
// Sometimes a frame is added and removed very quickly, in such cases we
// simply ignore the error.
if (error.message == "The frame was removed.")
return;
// Sometimes the frame in question is just not found. We don't know why
// this is exactly, but we simply ignore the error.
if (/^No frame with id \d+ in tab \d+\.$/.test(error.message))
return;
throw error;
});
}
catch (error)
{
// See the comment in the catch block associated with the call to
// tabs.insertCSS for why we catch any error here and simply
// return a rejected promise.
return Promise.reject(error);
}
}
/**
* @typedef {object} contentApplyFiltersResult
* @property {boolean} trace
* true if we're logging requests (our developer tools pane is open).
* @property {boolean} inline
* true if user stylesheets aren't supported but there is CSS to inject.
* @property {string[]} [rules]
* Array of CSS rules, included if the "inline" property is true.
* @property {string[]} [selectors]
* Array of selectors, included if "trace" property is true.
* @property {string[]} [exceptions]
* Array of exceptions, included if "trace" property is true.
*/
/**
* Apply content (snippet and element hiding) filters to the page.
*
* @event "content.applyFilters"
* @property {object} filterTypes - which filter types to apply, either
* "elemhide, "snippets" or both.
* @returns {contentApplyFiltersResult}
*/
port.on("content.applyFilters", (message, sender) =>
{
let styleSheet = {code: "", selectors: []};
let emulatedPatterns = [];
let trace = true; //HitLogger.hasListener(sender.page.id);
let inline = !userStyleSheetsSupported;
let filterTypes = message.filterTypes || {elemhide: true, snippets: true};
if (!checkWhitelisted(sender.page, sender.frame, null, contentTypes.DOCUMENT))
{
let docDomain = extractHostFromFrame(sender.frame);
if (filterTypes.snippets)
{
for (let filter of snippets.getFiltersForDomain(docDomain))
{
executeScript(filter.script, sender.page.id, sender.frame.id).then(() =>
{
let tabIds = [sender.page.id];
if (filter)
filterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
logRequest(tabIds, {
url: sender.frame.url.href,
type: "SNIPPET",
docDomain
}, filter);
});
}
}
if (filterTypes.elemhide &&
!checkWhitelisted(sender.page, sender.frame, null,
contentTypes.ELEMHIDE))
{
let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
contentTypes.GENERICHIDE);
styleSheet = elemHide.generateStyleSheetForDomain(docDomain, specificOnly,
trace, trace);
for (let filter of elemHideEmulation.getRulesForDomain(docDomain))
emulatedPatterns.push({selector: filter.selector, text: filter.text});
}
}
if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
styleSheet.code))
{
inline = true;
}
let response = {trace, inline, emulatedPatterns};
if (inline)
response.rules = [...rulesFromStyleSheet(styleSheet.code)];
if (trace)
{
response.selectors = styleSheet.selectors;
if(styleSheet.exceptions) {
response.exceptions = styleSheet.exceptions.map(({text, selector}) =>
({text, selector}));
}
}
return response;
});
/**
* Inject the given CSS selectors into the page.
* @event "content.injectSelectors"
* @property {string[]} selectors - The CSS selectors to inject
* @property {string} groupName - The group name, e.g. "standard" or
* "collapsing".
* @property {boolean} appendOnly - true if given selectors should never be
* removed, for example element collapsing
* selectors.
* @returns {?string[]} Array of CSS rules which were injected, if any.
*/
port.on("content.injectSelectors", (message, sender) =>
{
let styleSheet = createStyleSheet(message.selectors);
if (!userStyleSheetsSupported ||
!updateFrameStyles(sender.page.id, sender.frame.id, styleSheet,
message.groupName, message.appendOnly))
{
return [...rulesFromStyleSheet(styleSheet)];
}
});
fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
.then(response => response.ok ? response.text() : "")
.then(text =>
{
snippetsLibrarySource = text;
});