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: Polish up Created March 31, 2018, 5:59 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 }
189 205
190 let message = {type: "elemhide.apply", trace, inline}; 206 let message = {type: "elemhide.apply", trace, inline};
191 if (trace || inline) 207 if (trace || inline)
192 message.selectors = selectors; 208 message.selectors = selectors;
193 209
194 if (emulatedPatterns.length > 0) 210 if (emulatedPatterns.length > 0)
195 message.emulatedPatterns = emulatedPatterns; 211 message.emulatedPatterns = emulatedPatterns;
196 212
197 // 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
198 // adding them, which could cause problems with emulation filters as 214 // adding them, which could cause problems with emulation filters as
199 // 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
200 // add styles for emulation filters inline. 216 // add styles for emulation filters inline.
201 if (!styleSheetRemovalSupported) 217 if (!styleSheetRemovalSupported)
202 message.inlineEmulated = true; 218 message.inlineEmulated = true;
203 219
204 // In most cases on modern browsers there's nothing for the content script to 220 // In most cases on modern browsers there's nothing for the content script to
205 // do. 221 // do.
206 if (!message.selectors && !message.emulatedPatterns) 222 if (!message.selectors && !message.emulatedPatterns)
207 return; 223 return;
208 224
209 // 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
210 // webNavigation.onCommitted event; sometimes we have to retry a couple of 226 // webNavigation.onCommitted event; sometimes we have to retry a couple of
211 // times before we get through. 227 // times before we get through.
212 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) 228 browser.tabs.sendMessage(page.id, message, {frameId: frame.id})
213 .catch(() => 229 .catch(() =>
214 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
215 ) 231 )
216 .catch(() => 232 .catch(() =>
217 browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) 233 delay(1000, browser.tabs.sendMessage, page.id, message, {frameId: frame.id})
218 ); 234 );
219 } 235 }
220 236
221 browser.webNavigation.onCommitted.addListener(details => 237 browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) =>
222 { 238 {
223 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(details.url)) 239 // There's a bug in Chrome that causes webNavigation.onCommitted to get
224 return; 240 // dispatched twice if there's a URL filter present, therefore we must listen
225 241 // for all URLs and do an explicit check here.
226 let page = new ext.Page({id: details.tabId, url: details.url}); 242 // https://crbug.com/827855
227 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);
228 248
229 if (!frame) 249 if (!frame)
230 return; 250 return;
231 251
232 doElementHiding(page, frame); 252 doElementHiding(page, frame);
233 }); 253 });
234 254
235 port.on("elemhide.needApply", (message, sender) => 255 port.on("elemhide.needApply", (message, sender) =>
236 { 256 {
237 doElementHiding(sender.page, sender.frame); 257 doElementHiding(sender.page, sender.frame);
238 }); 258 });
239 259
240 port.on("elemhide.injectSelectors", (message, sender) => 260 port.on("elemhide.injectSelectors", (message, sender) =>
241 { 261 {
242 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 262 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
243 message.groupName); 263 message.groupName);
244 }); 264 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld