Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/contentFiltering.js

Issue 29893559: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created Sept. 27, 2018, 5:35 a.m.
Right Patch Set: Pass selectors if and only if trace is true Created Oct. 2, 2018, 7:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « include.preload.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module contentFiltering */ 18 /** @module contentFiltering */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
23 const {ElemHide, createStyleSheet} = require("../adblockpluscore/lib/elemHide"); 23 const {ElemHide, createStyleSheet,
24 rulesFromStyleSheet} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); 25 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
25 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 26 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier");
26 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); 27 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets");
27 const {checkWhitelisted} = require("./whitelisting"); 28 const {checkWhitelisted} = require("./whitelisting");
28 const {extractHostFromFrame} = require("./url"); 29 const {extractHostFromFrame} = require("./url");
29 const {port} = require("./messaging"); 30 const {port} = require("./messaging");
30 const {HitLogger, logRequest} = require("./hitLogger"); 31 const {HitLogger, logRequest} = require("./hitLogger");
31 const info = require("info"); 32 const info = require("info");
32 33
33 // Chromium's support for tabs.removeCSS is still a work in progress and the 34 // Chromium's support for tabs.removeCSS is still a work in progress and the
34 // API is likely to be different from Firefox's; for now we just don't use it 35 // API is likely to be different from Firefox's; for now we just don't use it
35 // at all, even if it's available. 36 // at all, even if it's available.
36 // See https://crbug.com/608854 37 // See https://crbug.com/608854
37 const styleSheetRemovalSupported = info.platform == "gecko"; 38 const styleSheetRemovalSupported = info.platform == "gecko";
38 39
39 const selectorGroupSize = 1024;
40
41 let userStyleSheetsSupported = true; 40 let userStyleSheetsSupported = true;
42 41
43 let snippetsLibrarySource = ""; 42 let snippetsLibrarySource = "";
44 let executableCode = new Map(); 43 let executableCode = new Map();
45 44
46 function addStyleSheet(tabId, frameId, styleSheet) 45 function addStyleSheet(tabId, frameId, styleSheet)
47 { 46 {
48 let details = {
49 code: styleSheet,
50 frameId,
51 matchAboutBlank: true,
52 runAt: "document_start"
53 };
54
55 if (userStyleSheetsSupported)
56 details.cssOrigin = "user";
57
58 try 47 try
59 { 48 {
49 let promise = browser.tabs.insertCSS(tabId, {
50 code: styleSheet,
51 cssOrigin: "user",
52 frameId,
53 matchAboutBlank: true,
54 runAt: "document_start"
55 });
56
60 // See error handling notes in the catch block. 57 // See error handling notes in the catch block.
61 browser.tabs.insertCSS(tabId, details).catch(() => {}); 58 promise.catch(() => {});
62 } 59 }
63 catch (error) 60 catch (error)
64 { 61 {
65 // If the error is about the "cssOrigin" option, this is an older version 62 // If the error is about the "cssOrigin" option, this is an older version
66 // of Chromium (65 and below) or Firefox (52 and below) that does not 63 // of Chromium (65 and below) or Firefox (52 and below) that does not
67 // support user style sheets. 64 // support user style sheets.
68 if (userStyleSheetsSupported && /\bcssOrigin\b/.test(error.message)) 65 if (/\bcssOrigin\b/.test(error.message))
69 {
70 userStyleSheetsSupported = false; 66 userStyleSheetsSupported = false;
71
72 return addStyleSheet(tabId, frameId, styleSheet);
73 }
74 67
75 // For other errors, we simply return false to indicate failure. 68 // For other errors, we simply return false to indicate failure.
76 // 69 //
77 // One common error that occurs frequently is when a frame is not found 70 // One common error that occurs frequently is when a frame is not found
78 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when 71 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when
79 // the code in the parent document has removed the frame before the 72 // the code in the parent document has removed the frame before the
80 // background page has had a chance to respond to the content script's 73 // background page has had a chance to respond to the content script's
81 // "content.applyFilters" message. We simply ignore such errors, because 74 // "content.applyFilters" message. We simply ignore such errors, because
82 // otherwise they show up in the log too often and make debugging 75 // otherwise they show up in the log too often and make debugging
83 // difficult. 76 // difficult.
(...skipping 14 matching lines...) Expand all
98 return; 91 return;
99 92
100 browser.tabs.removeCSS(tabId, { 93 browser.tabs.removeCSS(tabId, {
101 code: styleSheet, 94 code: styleSheet,
102 cssOrigin: "user", 95 cssOrigin: "user",
103 frameId, 96 frameId,
104 matchAboutBlank: true 97 matchAboutBlank: true
105 }); 98 });
106 } 99 }
107 100
108 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly) 101 function updateFrameStyles(tabId, frameId, styleSheet, groupName = "standard",
109 { 102 appendOnly = false)
110 let styleSheet = ""; 103 {
111 if (selectors.length > 0)
112 styleSheet = createStyleSheet(selectors);
113
114 let frame = ext.getFrame(tabId, frameId); 104 let frame = ext.getFrame(tabId, frameId);
115 if (!frame) 105 if (!frame)
116 return false; 106 return false;
117 107
118 if (!frame.injectedStyleSheets) 108 if (!frame.injectedStyleSheets)
119 frame.injectedStyleSheets = new Map(); 109 frame.injectedStyleSheets = new Map();
120 110
121 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 111 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
122 112
123 if (appendOnly && oldStyleSheet) 113 if (appendOnly && oldStyleSheet)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 { 185 {
196 // See the comment in the catch block associated with the call to 186 // See the comment in the catch block associated with the call to
197 // tabs.insertCSS for why we catch any error here and simply 187 // tabs.insertCSS for why we catch any error here and simply
198 // return a rejected promise. 188 // return a rejected promise.
199 return Promise.reject(error); 189 return Promise.reject(error);
200 } 190 }
201 } 191 }
202 192
203 port.on("content.applyFilters", (message, sender) => 193 port.on("content.applyFilters", (message, sender) =>
204 { 194 {
205 let selectors = []; 195 let styleSheet = {code: "", selectors: []};
206 let emulatedPatterns = []; 196 let emulatedPatterns = [];
207 let trace = HitLogger.hasListener(sender.page.id); 197 let trace = HitLogger.hasListener(sender.page.id);
198 let inline = !userStyleSheetsSupported;
208 199
209 let {elemhide, snippets} = message.filterTypes || 200 let {elemhide, snippets} = message.filterTypes ||
210 {elemhide: true, snippets: true}; 201 {elemhide: true, snippets: true};
211 202
212 if (!checkWhitelisted(sender.page, sender.frame, null, 203 if (!checkWhitelisted(sender.page, sender.frame, null,
213 RegExpFilter.typeMap.DOCUMENT)) 204 RegExpFilter.typeMap.DOCUMENT))
214 { 205 {
215 let docDomain = extractHostFromFrame(sender.frame); 206 let docDomain = extractHostFromFrame(sender.frame);
216 207
217 if (snippets) 208 if (snippets)
(...skipping 13 matching lines...) Expand all
231 }, filter); 222 }, filter);
232 }); 223 });
233 } 224 }
234 } 225 }
235 226
236 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 227 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
237 RegExpFilter.typeMap.ELEMHIDE)) 228 RegExpFilter.typeMap.ELEMHIDE))
238 { 229 {
239 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 230 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
240 RegExpFilter.typeMap.GENERICHIDE); 231 RegExpFilter.typeMap.GENERICHIDE);
241 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly); 232 styleSheet = ElemHide.generateStyleSheetForDomain(docDomain, specificOnly,
233 trace);
242 234
243 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) 235 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
244 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 236 emulatedPatterns.push({selector: filter.selector, text: filter.text});
245 } 237 }
246 } 238 }
247 239
248 updateFrameStyles(sender.page.id, sender.frame.id, selectors, "standard"); 240 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
249 241 styleSheet.code))
250 let response = {trace, emulatedPatterns}; 242 {
243 inline = true;
244 }
245
246 let response = {trace, inline, emulatedPatterns};
247
248 if (inline)
249 response.rules = [...rulesFromStyleSheet(styleSheet.code)];
250
251 if (trace) 251 if (trace)
252 response.selectors = selectors; 252 response.selectors = styleSheet.selectors;
253 253
254 return response; 254 return response;
255 }); 255 });
256 256
257 port.on("elemhide.injectSelectors", (message, sender) => 257 port.on("content.injectSelectors", (message, sender) =>
258 { 258 {
259 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 259 let styleSheet = createStyleSheet(message.selectors);
260 message.groupName, message.appendOnly); 260 if (!userStyleSheetsSupported ||
261 !updateFrameStyles(sender.page.id, sender.frame.id, styleSheet,
262 message.groupName, message.appendOnly))
263 {
264 return [...rulesFromStyleSheet(styleSheet)];
265 }
261 }); 266 });
262 267
263 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 268 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
264 .then(response => response.ok ? response.text() : "") 269 .then(response => response.ok ? response.text() : "")
265 .then(text => 270 .then(text =>
266 { 271 {
267 snippetsLibrarySource = text; 272 snippetsLibrarySource = text;
268 }); 273 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld