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

Side by Side Diff: lib/scriptInjection.js

Issue 29737561: Issue 6539, 6782 - Implement support for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Rebase, use snippets library from core Created July 13, 2018, 2:59 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
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | 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 cssInjection */ 18 /** @module scriptInjection */
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;
40 41
42 let snippetsLibrarySource = "";
43 let executableCode = new Map();
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
50 // this approach is more efficient and has worked well in practice. In theory 54 // this approach is more efficient and has worked well in practice. In theory
(...skipping 102 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
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 }
202 }
203
204 port.on("snippets.executeScripts", (message, sender) =>
205 {
206 if (checkWhitelisted(sender.page, sender.frame, null,
207 RegExpFilter.typeMap.DOCUMENT))
208 {
209 return;
210 }
211
212 let hostname = extractHostFromFrame(sender.frame);
213 for (let script of Snippets.getScriptsForDomain(hostname))
214 executeScript(script, sender.page.id, sender.frame.id);
215 });
216
163 port.on("elemhide.getSelectors", (message, sender) => 217 port.on("elemhide.getSelectors", (message, sender) =>
164 { 218 {
165 let selectors = []; 219 let selectors = [];
166 let emulatedPatterns = []; 220 let emulatedPatterns = [];
167 let trace = HitLogger.hasListener(sender.page.id); 221 let trace = HitLogger.hasListener(sender.page.id);
168 let inline = !userStyleSheetsSupported; 222 let inline = !userStyleSheetsSupported;
169 223
170 if (!checkWhitelisted(sender.page, sender.frame, null, 224 if (!checkWhitelisted(sender.page, sender.frame, null,
171 RegExpFilter.typeMap.DOCUMENT | 225 RegExpFilter.typeMap.DOCUMENT |
172 RegExpFilter.typeMap.ELEMHIDE)) 226 RegExpFilter.typeMap.ELEMHIDE))
(...skipping 26 matching lines...) Expand all
199 response.inlineEmulated = true; 253 response.inlineEmulated = true;
200 254
201 return response; 255 return response;
202 }); 256 });
203 257
204 port.on("elemhide.injectSelectors", (message, sender) => 258 port.on("elemhide.injectSelectors", (message, sender) =>
205 { 259 {
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 260 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
207 message.groupName, message.appendOnly); 261 message.groupName, message.appendOnly);
208 }); 262 });
263
264 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
265 .then(response => response.ok ? response.text() : "")
266 .then(text =>
267 {
268 snippetsLibrarySource = text;
269 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld