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

Delta Between Two Patch Sets: lib/cssInjection.js

Issue 29738592: Issue 6507 - Inject style sheet proactively from background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created March 31, 2018, 5:47 p.m.
Right Patch Set: Add delay Created April 6, 2018, 6:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « include.preload.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 20 matching lines...) Expand all
31 // Chromium's support for tabs.removeCSS is still a work in progress and the 31 // 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 32 // 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. 33 // at all, even if it's available.
34 // See https://crbug.com/608854 34 // See https://crbug.com/608854
35 const styleSheetRemovalSupported = info.platform == "gecko"; 35 const styleSheetRemovalSupported = info.platform == "gecko";
36 36
37 const selectorGroupSize = 1024; 37 const selectorGroupSize = 1024;
38 38
39 let userStyleSheetsSupported = true; 39 let userStyleSheetsSupported = true;
40 40
41 function delay(time, func = () => {}, ...args)
Manish Jethani 2018/04/06 06:20:02 This simply wraps setTimeout into a promise, which
42 {
43 return new Promise((resolve, reject) =>
44 {
45 setTimeout(() =>
46 {
47 try
48 {
49 resolve(func(...args));
50 }
51 catch (error)
52 {
53 reject(error);
54 }
55 },
56 time);
57 });
58 }
59
41 function* splitSelectors(selectors) 60 function* splitSelectors(selectors)
42 { 61 {
43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 62 // 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 63 // 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). 64 // 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 65 // 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 66 // 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 67 // 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 68 // 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 69 // this approach is more efficient and has worked well in practice. In theory
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 193
175 selectors = ElemHide.getSelectorsForDomain( 194 selectors = ElemHide.getSelectorsForDomain(
176 hostname, 195 hostname,
177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 196 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
178 ); 197 );
179 198
180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 199 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 200 emulatedPatterns.push({selector: filter.selector, text: filter.text});
182 } 201 }
183 202
184 if (!inline && !updateFrameStyles(page.id, frame.id, 203 if (!inline && !updateFrameStyles(page.id, frame.id, selectors, "standard"))
185 selectors, "standard"))
186 {
187 inline = true; 204 inline = true;
188 } 205
189 206 let message = {type: "elemhide.apply", trace, inline};
190 let message = {type: "elemhide.apply", trace, inline, emulatedPatterns};
191 if (trace || inline) 207 if (trace || inline)
192 message.selectors = selectors; 208 message.selectors = selectors;
209
210 if (emulatedPatterns.length > 0)
211 message.emulatedPatterns = emulatedPatterns;
193 212
194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep 213 // 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 214 // adding them, which could cause problems with emulation filters as
196 // described in issue #5864. Instead, we can just ask the content script to 215 // described in issue #5864. Instead, we can just ask the content script to
197 // add styles for emulation filters inline. 216 // add styles for emulation filters inline.
198 if (!styleSheetRemovalSupported) 217 if (!styleSheetRemovalSupported)
199 message.inlineEmulated = true; 218 message.inlineEmulated = true;
200 219
201 if (!message.selectors && message.emulatedPatterns.length == 0) 220 // In most cases on modern browsers there's nothing for the content script to
221 // do.
222 if (!message.selectors && !message.emulatedPatterns)
202 return; 223 return;
203 224
204 // The content script is loaded at about the same time as we get the 225 // The content script is loaded at about the same time as we get the
205 // webNavigation.onCommitted event; sometimes we have to retry a couple of 226 // webNavigation.onCommitted event; sometimes we have to retry a couple of
206 // times before we get through. 227 // times before we get through.
207 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) 228 browser.tabs.sendMessage(page.id, message, {frameId: frame.id})
208 .catch(() => 229 .catch(() =>
209 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) 230 delay(250, browser.tabs.sendMessage, page.id, message, {frameId: frame.id})
Manish Jethani 2018/04/06 06:20:02 Unfortunately sometimes the content script hasn't
210 ) 231 )
211 .catch(() => 232 .catch(() =>
212 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) 233 delay(1000, browser.tabs.sendMessage, page.id, message, {frameId: frame.id})
213 ); 234 );
214 } 235 }
215 236
216 browser.webNavigation.onCommitted.addListener(details => 237 browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) =>
217 { 238 {
218 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(details.url)) 239 // There's a bug in Chrome that causes webNavigation.onCommitted to get
219 return; 240 // dispatched twice if there's a URL filter present, therefore we must listen
220 241 // for all URLs and do an explicit check here.
221 let page = new ext.Page({id: details.tabId, url: details.url}); 242 // https://crbug.com/827855
222 let frame = ext.getFrame(details.tabId, details.frameId); 243 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(url))
244 return;
245
246 let page = new ext.Page({id: tabId, url: url});
247 let frame = ext.getFrame(tabId, frameId);
223 248
224 if (!frame) 249 if (!frame)
225 return; 250 return;
226 251
227 doElementHiding(page, frame); 252 doElementHiding(page, frame);
228 }); 253 });
229 254
230 port.on("elemhide.needApply", (message, sender) => 255 port.on("elemhide.needApply", (message, sender) =>
231 { 256 {
232 doElementHiding(sender.page, sender.frame); 257 doElementHiding(sender.page, sender.frame);
233 }); 258 });
234 259
235 port.on("elemhide.injectSelectors", (message, sender) => 260 port.on("elemhide.injectSelectors", (message, sender) =>
236 { 261 {
237 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 262 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
238 message.groupName); 263 message.groupName);
239 }); 264 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld