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

Side by Side Diff: lib/cssInjection.js

Issue 29679796: Issue 6298 - Split injected CSS hiding rule into groups of 1,024 selectors (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Jan. 25, 2018, 5:12 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
« include.preload.js ('K') | « 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 11 matching lines...) Expand all
22 const {RegExpFilter} = require("filterClasses"); 22 const {RegExpFilter} = require("filterClasses");
23 const {ElemHide} = require("elemHide"); 23 const {ElemHide} = require("elemHide");
24 const {ElemHideEmulation} = require("elemHideEmulation"); 24 const {ElemHideEmulation} = require("elemHideEmulation");
25 const {checkWhitelisted} = require("whitelisting"); 25 const {checkWhitelisted} = require("whitelisting");
26 const {extractHostFromFrame} = require("url"); 26 const {extractHostFromFrame} = require("url");
27 const {port} = require("messaging"); 27 const {port} = require("messaging");
28 const devtools = require("devtools"); 28 const devtools = require("devtools");
29 29
30 const userStyleSheetsSupported = "extensionTypes" in browser && 30 const userStyleSheetsSupported = "extensionTypes" in browser &&
31 "CSSOrigin" in browser.extensionTypes; 31 "CSSOrigin" in browser.extensionTypes;
32 const selectorGroupSize = 200;
33
34 function createStyleSheet(selectors)
35 {
36 let styleSheet = "";
37
38 // Chromium's Blink engine supports only up to 8,192 selectors; more
kzar 2018/01/25 17:46:47 If we're limited to 8192 why do we go with 200?
Manish Jethani 2018/01/25 18:42:41 It's "up to 8,192" selectors. If you have 8,192 s
Manish Jethani 2018/01/25 18:44:28 This was not technically true by the way but now [
Manish Jethani 2018/01/30 06:19:43 Now that I think about it, maybe we should increas
kzar1 2018/01/30 11:39:59 Sounds good to me, but let's rewrite the comment t
Manish Jethani 2018/01/30 13:15:43 Unfortunately this comment is confusing and techni
39 // specifically, it ignores any selectors that start at index 8192 or beyond
40 // in the list of plain selectors. In order to avoid spilling outside of this
41 // range, we simply add multiple rules in groups of up to 200 selectors each.
42 // See issue #6298 and https://crbug.com/804179
43 for (let i = 0; i < selectors.length; i += selectorGroupSize)
44 {
45 styleSheet += selectors.slice(i, i + selectorGroupSize).join(", ") +
46 " {display: none !important;}\n";
47 }
48
49 return styleSheet;
50 }
32 51
33 function addStyleSheet(tabId, frameId, styleSheet) 52 function addStyleSheet(tabId, frameId, styleSheet)
34 { 53 {
35 browser.tabs.insertCSS(tabId, { 54 browser.tabs.insertCSS(tabId, {
36 code: styleSheet, 55 code: styleSheet,
37 cssOrigin: "user", 56 cssOrigin: "user",
38 frameId, 57 frameId,
39 matchAboutBlank: true, 58 matchAboutBlank: true,
40 runAt: "document_start" 59 runAt: "document_start"
41 }); 60 });
42 } 61 }
43 62
44 function removeStyleSheet(tabId, frameId, styleSheet) 63 function removeStyleSheet(tabId, frameId, styleSheet)
45 { 64 {
46 browser.tabs.removeCSS(tabId, { 65 browser.tabs.removeCSS(tabId, {
47 code: styleSheet, 66 code: styleSheet,
48 cssOrigin: "user", 67 cssOrigin: "user",
49 frameId, 68 frameId,
50 matchAboutBlank: true 69 matchAboutBlank: true
51 }); 70 });
52 } 71 }
53 72
54 function updateFrameStyles(tabId, frameId, selectors, groupName) 73 function updateFrameStyles(tabId, frameId, selectors, groupName)
55 { 74 {
56 let styleSheet = null; 75 let styleSheet = null;
57 if (selectors.length > 0) 76 if (selectors.length > 0)
58 styleSheet = selectors.join(", ") + "{display: none !important;}"; 77 styleSheet = createStyleSheet(selectors);
59 78
60 let frame = ext.getFrame(tabId, frameId); 79 let frame = ext.getFrame(tabId, frameId);
61 if (!frame.injectedStyleSheets) 80 if (!frame.injectedStyleSheets)
62 frame.injectedStyleSheets = new Map(); 81 frame.injectedStyleSheets = new Map();
63 82
64 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 83 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
65 84
66 // Ideally we would compare the old and new style sheets and skip this code 85 // Ideally we would compare the old and new style sheets and skip this code
67 // if they're the same, but the old style sheet can be a leftover from a 86 // if they're the same, but the old style sheet can be a leftover from a
68 // previous instance of the frame. We must add the new style sheet 87 // previous instance of the frame. We must add the new style sheet
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 response.selectors = selectors; 133 response.selectors = selectors;
115 134
116 return response; 135 return response;
117 }); 136 });
118 137
119 port.on("elemhide.injectSelectors", (message, sender) => 138 port.on("elemhide.injectSelectors", (message, sender) =>
120 { 139 {
121 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 140 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
122 message.groupName); 141 message.groupName);
123 }); 142 });
OLDNEW
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld