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

Side by Side Diff: lib/contentFiltering.js

Issue 29893559: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Insert individual rules Created Oct. 2, 2018, 3:39 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 return; 91 return;
91 92
92 browser.tabs.removeCSS(tabId, { 93 browser.tabs.removeCSS(tabId, {
93 code: styleSheet, 94 code: styleSheet,
94 cssOrigin: "user", 95 cssOrigin: "user",
95 frameId, 96 frameId,
96 matchAboutBlank: true 97 matchAboutBlank: true
97 }); 98 });
98 } 99 }
99 100
100 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly) 101 function updateFrameStyles(tabId, frameId, styleSheet, groupName = "standard",
102 appendOnly = false)
101 { 103 {
102 let styleSheet = "";
103 if (selectors.length > 0)
104 styleSheet = createStyleSheet(selectors);
105
106 let frame = ext.getFrame(tabId, frameId); 104 let frame = ext.getFrame(tabId, frameId);
107 if (!frame) 105 if (!frame)
108 return false; 106 return false;
109 107
110 if (!frame.injectedStyleSheets) 108 if (!frame.injectedStyleSheets)
111 frame.injectedStyleSheets = new Map(); 109 frame.injectedStyleSheets = new Map();
112 110
113 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 111 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
114 112
115 if (appendOnly && oldStyleSheet) 113 if (appendOnly && oldStyleSheet)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 { 185 {
188 // 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
189 // tabs.insertCSS for why we catch any error here and simply 187 // tabs.insertCSS for why we catch any error here and simply
190 // return a rejected promise. 188 // return a rejected promise.
191 return Promise.reject(error); 189 return Promise.reject(error);
192 } 190 }
193 } 191 }
194 192
195 port.on("content.applyFilters", (message, sender) => 193 port.on("content.applyFilters", (message, sender) =>
196 { 194 {
197 let selectors = []; 195 let styleSheet = {code: "", 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,
233 trace);
235 234
236 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) 235 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
237 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 236 emulatedPatterns.push({selector: filter.selector, text: filter.text});
238 } 237 }
239 } 238 }
240 239
241 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 240 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
242 selectors, "standard")) 241 styleSheet.code))
243 { 242 {
244 inline = true; 243 inline = true;
245 } 244 }
246 245
247 let response = {trace, inline, emulatedPatterns}; 246 let response = {trace, inline, emulatedPatterns};
248 if (trace || inline) 247
249 response.selectors = selectors; 248 if (inline)
249 response.rules = [...rulesFromStyleSheet(styleSheet.code)];
Sebastian Noack 2018/10/02 17:14:02 Why do we have to copy this array?
Sebastian Noack 2018/10/02 17:15:38 Ah, just figured it's a generator. I think I perso
Manish Jethani 2018/10/02 17:23:24 Yes, the spread operator is significantly faster (
Sebastian Noack 2018/10/02 17:28:21 I'd take that benchmark with a grain of salt, sinc
250 else if (trace)
Sebastian Noack 2018/10/02 17:14:02 Doesn't the content scripts needs the original sel
Manish Jethani 2018/10/02 19:07:46 Ah, you're right. Done.
251 response.selectors = styleSheet.selectors;
250 252
251 return response; 253 return response;
252 }); 254 });
253 255
254 port.on("content.injectSelectors", (message, sender) => 256 port.on("content.injectSelectors", (message, sender) =>
255 { 257 {
256 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 258 let styleSheet = createStyleSheet(message.selectors);
257 message.groupName, message.appendOnly); 259 if (!userStyleSheetsSupported ||
260 !updateFrameStyles(sender.page.id, sender.frame.id, styleSheet,
261 message.groupName, message.appendOnly))
262 {
263 return [...rulesFromStyleSheet(styleSheet)];
264 }
258 }); 265 });
259 266
260 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 267 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
261 .then(response => response.ok ? response.text() : "") 268 .then(response => response.ok ? response.text() : "")
262 .then(text => 269 .then(text =>
263 { 270 {
264 snippetsLibrarySource = text; 271 snippetsLibrarySource = text;
265 }); 272 });
OLDNEW
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld