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: Updated implementation Created Sept. 28, 2018, 1:23 p.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 const selectorGroupSize = 1024;
40 39
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 try 47 try
49 { 48 {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 { 185 {
187 // 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
188 // tabs.insertCSS for why we catch any error here and simply 187 // tabs.insertCSS for why we catch any error here and simply
189 // return a rejected promise. 188 // return a rejected promise.
190 return Promise.reject(error); 189 return Promise.reject(error);
191 } 190 }
192 } 191 }
193 192
194 port.on("content.applyFilters", (message, sender) => 193 port.on("content.applyFilters", (message, sender) =>
195 { 194 {
196 let styleSheet = ""; 195 let styleSheet = {code: "", selectors: []};
197 let selectors = [];
198 let emulatedPatterns = []; 196 let emulatedPatterns = [];
199 let trace = HitLogger.hasListener(sender.page.id); 197 let trace = HitLogger.hasListener(sender.page.id);
200 let inline = !userStyleSheetsSupported; 198 let inline = !userStyleSheetsSupported;
201 199
202 let {elemhide, snippets} = message.filterTypes || 200 let {elemhide, snippets} = message.filterTypes ||
203 {elemhide: true, snippets: true}; 201 {elemhide: true, snippets: true};
204 202
205 if (!checkWhitelisted(sender.page, sender.frame, null, 203 if (!checkWhitelisted(sender.page, sender.frame, null,
206 RegExpFilter.typeMap.DOCUMENT)) 204 RegExpFilter.typeMap.DOCUMENT))
207 { 205 {
(...skipping 16 matching lines...) Expand all
224 }, filter); 222 }, filter);
225 }); 223 });
226 } 224 }
227 } 225 }
228 226
229 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 227 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
230 RegExpFilter.typeMap.ELEMHIDE)) 228 RegExpFilter.typeMap.ELEMHIDE))
231 { 229 {
232 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 230 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
233 RegExpFilter.typeMap.GENERICHIDE); 231 RegExpFilter.typeMap.GENERICHIDE);
234 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly); 232 styleSheet = ElemHide.generateStyleSheetForDomain(docDomain, specificOnly,
235 styleSheet = createStyleSheet(selectors); 233 trace);
236 234
237 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) 235 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
238 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 236 emulatedPatterns.push({selector: filter.selector, text: filter.text});
239 } 237 }
240 } 238 }
241 239
242 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 240 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
243 styleSheet)) 241 styleSheet.code))
244 { 242 {
245 inline = true; 243 inline = true;
246 } 244 }
247 245
248 let response = {trace, inline, emulatedPatterns}; 246 let response = {trace, inline, emulatedPatterns};
249 247
250 if (inline) 248 if (inline)
251 response.styleSheet = styleSheet; 249 response.rules = [...rulesFromStyleSheet(styleSheet.code)];
252 250
253 if (trace) 251 if (trace)
254 response.selectors = selectors; 252 response.selectors = styleSheet.selectors;
255 253
256 return response; 254 return response;
257 }); 255 });
258 256
259 port.on("elemhide.injectSelectors", (message, sender) => 257 port.on("content.injectSelectors", (message, sender) =>
260 { 258 {
261 updateFrameStyles(sender.page.id, sender.frame.id, 259 let styleSheet = createStyleSheet(message.selectors);
262 createStyleSheet(message.selectors), 260 if (!userStyleSheetsSupported ||
263 message.groupName, message.appendOnly); 261 !updateFrameStyles(sender.page.id, sender.frame.id, styleSheet,
262 message.groupName, message.appendOnly))
263 {
264 return [...rulesFromStyleSheet(styleSheet)];
265 }
264 }); 266 });
265 267
266 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 268 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
267 .then(response => response.ok ? response.text() : "") 269 .then(response => response.ok ? response.text() : "")
268 .then(text => 270 .then(text =>
269 { 271 {
270 snippetsLibrarySource = text; 272 snippetsLibrarySource = text;
271 }); 273 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld