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: Created Sept. 27, 2018, 5:35 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « 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 27 matching lines...) Expand all
38 38
39 const selectorGroupSize = 1024; 39 const selectorGroupSize = 1024;
40 40
41 let userStyleSheetsSupported = true; 41 let userStyleSheetsSupported = true;
42 42
43 let snippetsLibrarySource = ""; 43 let snippetsLibrarySource = "";
44 let executableCode = new Map(); 44 let executableCode = new Map();
45 45
46 function addStyleSheet(tabId, frameId, styleSheet) 46 function addStyleSheet(tabId, frameId, styleSheet)
47 { 47 {
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
48 try 58 try
49 { 59 {
50 let promise = browser.tabs.insertCSS(tabId, {
51 code: styleSheet,
52 cssOrigin: "user",
53 frameId,
54 matchAboutBlank: true,
55 runAt: "document_start"
56 });
57
58 // See error handling notes in the catch block. 60 // See error handling notes in the catch block.
59 promise.catch(() => {}); 61 browser.tabs.insertCSS(tabId, details).catch(() => {});
60 } 62 }
61 catch (error) 63 catch (error)
62 { 64 {
63 // If the error is about the "cssOrigin" option, this is an older version 65 // If the error is about the "cssOrigin" option, this is an older version
64 // of Chromium (65 and below) or Firefox (52 and below) that does not 66 // of Chromium (65 and below) or Firefox (52 and below) that does not
65 // support user style sheets. 67 // support user style sheets.
66 if (/\bcssOrigin\b/.test(error.message)) 68 if (userStyleSheetsSupported && /\bcssOrigin\b/.test(error.message))
69 {
67 userStyleSheetsSupported = false; 70 userStyleSheetsSupported = false;
68 71
72 return addStyleSheet(tabId, frameId, styleSheet);
73 }
74
69 // For other errors, we simply return false to indicate failure. 75 // For other errors, we simply return false to indicate failure.
70 // 76 //
71 // One common error that occurs frequently is when a frame is not found 77 // One common error that occurs frequently is when a frame is not found
72 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when 78 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when
73 // the code in the parent document has removed the frame before the 79 // the code in the parent document has removed the frame before the
74 // background page has had a chance to respond to the content script's 80 // background page has had a chance to respond to the content script's
75 // "content.applyFilters" message. We simply ignore such errors, because 81 // "content.applyFilters" message. We simply ignore such errors, because
76 // otherwise they show up in the log too often and make debugging 82 // otherwise they show up in the log too often and make debugging
77 // difficult. 83 // difficult.
78 // 84 //
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 // return a rejected promise. 198 // return a rejected promise.
193 return Promise.reject(error); 199 return Promise.reject(error);
194 } 200 }
195 } 201 }
196 202
197 port.on("content.applyFilters", (message, sender) => 203 port.on("content.applyFilters", (message, sender) =>
198 { 204 {
199 let selectors = []; 205 let selectors = [];
200 let emulatedPatterns = []; 206 let emulatedPatterns = [];
201 let trace = HitLogger.hasListener(sender.page.id); 207 let trace = HitLogger.hasListener(sender.page.id);
202 let inline = !userStyleSheetsSupported;
203 208
204 let {elemhide, snippets} = message.filterTypes || 209 let {elemhide, snippets} = message.filterTypes ||
205 {elemhide: true, snippets: true}; 210 {elemhide: true, snippets: true};
206 211
207 if (!checkWhitelisted(sender.page, sender.frame, null, 212 if (!checkWhitelisted(sender.page, sender.frame, null,
208 RegExpFilter.typeMap.DOCUMENT)) 213 RegExpFilter.typeMap.DOCUMENT))
209 { 214 {
210 let docDomain = extractHostFromFrame(sender.frame); 215 let docDomain = extractHostFromFrame(sender.frame);
211 216
212 if (snippets) 217 if (snippets)
(...skipping 20 matching lines...) Expand all
233 { 238 {
234 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 239 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
235 RegExpFilter.typeMap.GENERICHIDE); 240 RegExpFilter.typeMap.GENERICHIDE);
236 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly); 241 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly);
237 242
238 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain)) 243 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
239 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 244 emulatedPatterns.push({selector: filter.selector, text: filter.text});
240 } 245 }
241 } 246 }
242 247
243 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 248 updateFrameStyles(sender.page.id, sender.frame.id, selectors, "standard");
244 selectors, "standard"))
245 {
246 inline = true;
247 }
248 249
249 let response = {trace, inline, emulatedPatterns}; 250 let response = {trace, emulatedPatterns};
250 if (trace || inline) 251 if (trace)
251 response.selectors = selectors; 252 response.selectors = selectors;
252 253
253 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep
254 // adding them, which could cause problems with emulation filters as
255 // described in issue #5864. Instead, we can just ask the content script to
256 // add styles for emulation filters inline.
257 if (!styleSheetRemovalSupported)
258 response.inlineEmulated = true;
259
260 return response; 254 return response;
261 }); 255 });
262 256
263 port.on("elemhide.injectSelectors", (message, sender) => 257 port.on("elemhide.injectSelectors", (message, sender) =>
264 { 258 {
265 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 259 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
266 message.groupName, message.appendOnly); 260 message.groupName, message.appendOnly);
267 }); 261 });
268 262
269 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 263 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
270 .then(response => response.ok ? response.text() : "") 264 .then(response => response.ok ? response.text() : "")
271 .then(text => 265 .then(text =>
272 { 266 {
273 snippetsLibrarySource = text; 267 snippetsLibrarySource = text;
274 }); 268 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld