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: Updated implementation Created Sept. 28, 2018, 1:23 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
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 return; 92 return;
93 93
94 browser.tabs.removeCSS(tabId, { 94 browser.tabs.removeCSS(tabId, {
95 code: styleSheet, 95 code: styleSheet,
96 cssOrigin: "user", 96 cssOrigin: "user",
97 frameId, 97 frameId,
98 matchAboutBlank: true 98 matchAboutBlank: true
99 }); 99 });
100 } 100 }
101 101
102 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly) 102 function updateFrameStyles(tabId, frameId, styleSheet, groupName = "standard",
103 appendOnly = false)
103 { 104 {
104 let styleSheet = "";
105 if (selectors.length > 0)
106 styleSheet = createStyleSheet(selectors);
107
108 let frame = ext.getFrame(tabId, frameId); 105 let frame = ext.getFrame(tabId, frameId);
109 if (!frame) 106 if (!frame)
110 return false; 107 return false;
111 108
112 if (!frame.injectedStyleSheets) 109 if (!frame.injectedStyleSheets)
113 frame.injectedStyleSheets = new Map(); 110 frame.injectedStyleSheets = new Map();
114 111
115 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 112 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
116 113
117 if (appendOnly && oldStyleSheet) 114 if (appendOnly && oldStyleSheet)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 { 186 {
190 // See the comment in the catch block associated with the call to 187 // See the comment in the catch block associated with the call to
191 // tabs.insertCSS for why we catch any error here and simply 188 // tabs.insertCSS for why we catch any error here and simply
192 // return a rejected promise. 189 // return a rejected promise.
193 return Promise.reject(error); 190 return Promise.reject(error);
194 } 191 }
195 } 192 }
196 193
197 port.on("content.applyFilters", (message, sender) => 194 port.on("content.applyFilters", (message, sender) =>
198 { 195 {
196 let styleSheet = "";
199 let selectors = []; 197 let selectors = [];
200 let emulatedPatterns = []; 198 let emulatedPatterns = [];
201 let trace = HitLogger.hasListener(sender.page.id); 199 let trace = HitLogger.hasListener(sender.page.id);
202 let inline = !userStyleSheetsSupported; 200 let inline = !userStyleSheetsSupported;
203 201
204 let {elemhide, snippets} = message.filterTypes || 202 let {elemhide, snippets} = message.filterTypes ||
205 {elemhide: true, snippets: true}; 203 {elemhide: true, snippets: true};
206 204
207 if (!checkWhitelisted(sender.page, sender.frame, null, 205 if (!checkWhitelisted(sender.page, sender.frame, null,
208 RegExpFilter.typeMap.DOCUMENT)) 206 RegExpFilter.typeMap.DOCUMENT))
(...skipping 18 matching lines...) Expand all
227 }); 225 });
228 } 226 }
229 } 227 }
230 228
231 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 229 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
232 RegExpFilter.typeMap.ELEMHIDE)) 230 RegExpFilter.typeMap.ELEMHIDE))
233 { 231 {
234 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 232 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
235 RegExpFilter.typeMap.GENERICHIDE); 233 RegExpFilter.typeMap.GENERICHIDE);
236 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly); 234 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly);
235 styleSheet = createStyleSheet(selectors);
237 236
238 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) 237 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
239 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 238 emulatedPatterns.push({selector: filter.selector, text: filter.text});
240 } 239 }
241 } 240 }
242 241
243 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 242 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
244 selectors, "standard")) 243 styleSheet))
245 { 244 {
246 inline = true; 245 inline = true;
247 } 246 }
248 247
249 let response = {trace, inline, emulatedPatterns}; 248 let response = {trace, inline, emulatedPatterns};
250 if (trace || inline) 249
250 if (inline)
251 response.styleSheet = styleSheet;
252
253 if (trace)
251 response.selectors = selectors; 254 response.selectors = selectors;
252 255
253 return response; 256 return response;
254 }); 257 });
255 258
256 port.on("elemhide.injectSelectors", (message, sender) => 259 port.on("elemhide.injectSelectors", (message, sender) =>
257 { 260 {
258 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 261 updateFrameStyles(sender.page.id, sender.frame.id,
262 createStyleSheet(message.selectors),
259 message.groupName, message.appendOnly); 263 message.groupName, message.appendOnly);
260 }); 264 });
261 265
262 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 266 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
263 .then(response => response.ok ? response.text() : "") 267 .then(response => response.ok ? response.text() : "")
264 .then(text => 268 .then(text =>
265 { 269 {
266 snippetsLibrarySource = text; 270 snippetsLibrarySource = text;
267 }); 271 });
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