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

Delta Between Two Patch Sets: lib/contentFiltering.js

Issue 29737561: Issue 6539, 6782 - Implement support for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Use JSON.stringify Created March 31, 2018, 8:06 a.m.
Right Patch Set: Add explanatory comment in catch block Created July 19, 2018, 3:51 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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 cssInjection */ 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} = require("../adblockpluscore/lib/elemHide"); 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
25 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets");
25 const {checkWhitelisted} = require("./whitelisting"); 26 const {checkWhitelisted} = require("./whitelisting");
26 const {extractHostFromFrame} = require("./url"); 27 const {extractHostFromFrame} = require("./url");
27 const {port} = require("./messaging"); 28 const {port} = require("./messaging");
28 const {HitLogger} = require("./hitLogger"); 29 const {HitLogger} = require("./hitLogger");
29 const info = require("info"); 30 const info = require("info");
30 31
31 // Chromium's support for tabs.removeCSS is still a work in progress and the 32 // Chromium's support for tabs.removeCSS is still a work in progress and the
32 // API is likely to be different from Firefox's; for now we just don't use it 33 // API is likely to be different from Firefox's; for now we just don't use it
33 // at all, even if it's available. 34 // at all, even if it's available.
34 // See https://crbug.com/608854 35 // See https://crbug.com/608854
35 const styleSheetRemovalSupported = info.platform == "gecko"; 36 const styleSheetRemovalSupported = info.platform == "gecko";
36 37
37 const selectorGroupSize = 1024; 38 const selectorGroupSize = 1024;
38 39
39 let userStyleSheetsSupported = true; 40 let userStyleSheetsSupported = true;
41
42 let snippetsLibrarySource = "";
43 let executableCode = new Map();
40 44
41 function* splitSelectors(selectors) 45 function* splitSelectors(selectors)
42 { 46 {
43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 47 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
44 // even fewer compound selectors, in a rule. The exact number of selectors 48 // even fewer compound selectors, in a rule. The exact number of selectors
45 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). 49 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
46 // Since we don't know the sizes of the selectors here, we simply split them 50 // Since we don't know the sizes of the selectors here, we simply split them
47 // into groups of 1,024, based on the reasonable assumption that the average 51 // into groups of 1,024, based on the reasonable assumption that the average
48 // selector won't have a size greater than 8. The alternative would be to 52 // selector won't have a size greater than 8. The alternative would be to
49 // calculate the sizes of the selectors and divide them up accordingly, but 53 // calculate the sizes of the selectors and divide them up accordingly, but
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // support user style sheets. 92 // support user style sheets.
89 if (/\bcssOrigin\b/.test(error.message)) 93 if (/\bcssOrigin\b/.test(error.message))
90 userStyleSheetsSupported = false; 94 userStyleSheetsSupported = false;
91 95
92 // For other errors, we simply return false to indicate failure. 96 // For other errors, we simply return false to indicate failure.
93 // 97 //
94 // One common error that occurs frequently is when a frame is not found 98 // One common error that occurs frequently is when a frame is not found
95 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when 99 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when
96 // the code in the parent document has removed the frame before the 100 // the code in the parent document has removed the frame before the
97 // background page has had a chance to respond to the content script's 101 // background page has had a chance to respond to the content script's
98 // "elemhide.getSelectors" message. We simply ignore such errors, because 102 // "content.applyFilters" message. We simply ignore such errors, because
99 // otherwise they show up in the log too often and make debugging 103 // otherwise they show up in the log too often and make debugging
100 // difficult. 104 // difficult.
101 // 105 //
102 // Also note that the missing frame error is thrown synchronously on 106 // Also note that the missing frame error is thrown synchronously on
103 // Firefox, while on Chromium it is an asychronous promise rejection. In 107 // Firefox, while on Chromium it is an asychronous promise rejection. In
104 // the latter case, we cannot indicate failure to the caller, but we still 108 // the latter case, we cannot indicate failure to the caller, but we still
105 // explicitly ignore the error. 109 // explicitly ignore the error.
106 return false; 110 return false;
107 } 111 }
108 112
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 // Sometimes the old and new style sheets can be exactly the same. In such a 157 // Sometimes the old and new style sheets can be exactly the same. In such a
154 // case, do not remove the "old" style sheet, because it is in fact the new 158 // case, do not remove the "old" style sheet, because it is in fact the new
155 // style sheet now. 159 // style sheet now.
156 if (oldStyleSheet && oldStyleSheet != styleSheet) 160 if (oldStyleSheet && oldStyleSheet != styleSheet)
157 removeStyleSheet(tabId, frameId, oldStyleSheet); 161 removeStyleSheet(tabId, frameId, oldStyleSheet);
158 162
159 frame.injectedStyleSheets.set(groupName, styleSheet); 163 frame.injectedStyleSheets.set(groupName, styleSheet);
160 return true; 164 return true;
161 } 165 }
162 166
163 port.on("elemhide.getSelectors", (message, sender) => 167 function getExecutableCode(script)
168 {
169 let code = executableCode.get(script);
170 if (code)
171 return code;
172
173 code = compileScript(script, [snippetsLibrarySource]);
174
175 executableCode.set(script, code);
176 return code;
177 }
178
179 function executeScript(script, tabId, frameId)
180 {
181 try
182 {
183 browser.tabs.executeScript(tabId, {
184 code: getExecutableCode(script),
185 frameId,
186 matchAboutBlank: true,
187 runAt: "document_start"
188 })
189 .catch(error =>
190 {
191 // Sometimes a frame is added and removed very quickly, in such cases we
192 // simply ignore the error.
193 if (error.message == "The frame was removed.")
194 return;
195
196 throw error;
197 });
198 }
199 catch (error)
200 {
201 // See the comment in the catch block associated with the call to
202 // tabs.insertCSS for why we catch and ignore any errors here.
203 }
204 }
205
206 port.on("content.applyFilters", (message, sender) =>
164 { 207 {
165 let selectors = []; 208 let selectors = [];
166 let emulatedPatterns = []; 209 let emulatedPatterns = [];
167 let trace = HitLogger.hasListener(sender.page.id); 210 let trace = HitLogger.hasListener(sender.page.id);
168 let inline = !userStyleSheetsSupported; 211 let inline = !userStyleSheetsSupported;
169 212
213 let {elemhide, snippets} = message.filterTypes ||
214 {elemhide: true, snippets: true};
215
170 if (!checkWhitelisted(sender.page, sender.frame, null, 216 if (!checkWhitelisted(sender.page, sender.frame, null,
171 RegExpFilter.typeMap.DOCUMENT | 217 RegExpFilter.typeMap.DOCUMENT))
172 RegExpFilter.typeMap.ELEMHIDE))
173 { 218 {
174 let hostname = extractHostFromFrame(sender.frame); 219 let hostname = extractHostFromFrame(sender.frame);
175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 220
176 RegExpFilter.typeMap.GENERICHIDE); 221 if (snippets)
177 222 {
178 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); 223 for (let script of Snippets.getScriptsForDomain(hostname))
179 224 executeScript(script, sender.page.id, sender.frame.id);
180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 225 }
181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 226
227 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.ELEMHIDE))
229 {
230 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
231 RegExpFilter.typeMap.GENERICHIDE);
232 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly);
233
234 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
235 emulatedPatterns.push({selector: filter.selector, text: filter.text});
236 }
182 } 237 }
183 238
184 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 239 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
185 selectors, "standard")) 240 selectors, "standard"))
186 { 241 {
187 inline = true; 242 inline = true;
188 } 243 }
189 244
190 let response = {trace, inline, emulatedPatterns}; 245 let response = {trace, inline, emulatedPatterns};
191 if (trace || inline) 246 if (trace || inline)
192 response.selectors = selectors; 247 response.selectors = selectors;
193 248
194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep 249 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep
195 // adding them, which could cause problems with emulation filters as 250 // adding them, which could cause problems with emulation filters as
196 // described in issue #5864. Instead, we can just ask the content script to 251 // described in issue #5864. Instead, we can just ask the content script to
197 // add styles for emulation filters inline. 252 // add styles for emulation filters inline.
198 if (!styleSheetRemovalSupported) 253 if (!styleSheetRemovalSupported)
199 response.inlineEmulated = true; 254 response.inlineEmulated = true;
200 255
201 return response; 256 return response;
202 }); 257 });
203 258
204 port.on("elemhide.injectSelectors", (message, sender) => 259 port.on("elemhide.injectSelectors", (message, sender) =>
205 { 260 {
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 261 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
207 message.groupName, message.appendOnly); 262 message.groupName, message.appendOnly);
208 }); 263 });
264
265 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
266 .then(response => response.ok ? response.text() : "")
267 .then(text =>
268 {
269 snippetsLibrarySource = text;
270 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld