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

Delta Between Two Patch Sets: lib/cssInjection.js

Issue 29564767: Issue 242 - Use user style sheets on Chromium (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Rewrite Created Jan. 31, 2018, 8:19 a.m.
Right Patch Set: Do not check error message Created Feb. 1, 2018, 10:37 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 10 matching lines...) Expand all
21 21
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 const info = require("info"); 29 const info = require("info");
30 30
31 const userStyleSheetsSupported = ("extensionTypes" in browser && 31 // Chromium's support for tabs.removeCSS is still a work in progress and the
32 "CSSOrigin" in browser.extensionTypes) || 32 // API is likely to be different from Firefox's; for now we just don't use it
33 (info.platform == "chromium" && 33 // at all, even if it's available.
34 parseInt(info.platformVersion, 10) >= 66); 34 // See https://crbug.com/608854
Sebastian Noack 2018/01/31 11:23:37 Perhaps, we should consider going back to the try-
Manish Jethani 2018/01/31 12:36:27 What if Edge adds support for cssOrigin but behave
35 const styleSheetRemovalSupported = "removeCSS" in browser.tabs && 35 const styleSheetRemovalSupported = info.platform == "gecko";
36 info.platform != "chromium";
kzar 2018/01/31 11:11:32 IMO we only know removeCSS works on Gecko and so w
Sebastian Noack 2018/01/31 11:23:37 Why not just checking for presence of "removeCSS"?
kzar 2018/01/31 11:25:29 Well as we discussed in IRC don't we agree it woul
Sebastian Noack 2018/01/31 11:54:44 Well, we were talking about cssOrigin, which is no
kzar 2018/01/31 11:57:56 Fair enough, if you both agree to do it that way I
Manish Jethani 2018/01/31 12:36:27 Makes sense. Done.
Manish Jethani 2018/01/31 12:36:27 With tabs.removeCSS specifically we're thinking of
Manish Jethani 2018/01/31 12:36:27 See my other comment. In the case of tabs.removeCS
37 36
38 const selectorGroupSize = 1024; 37 const selectorGroupSize = 1024;
38
39 let userStyleSheetsSupported = true;
39 40
40 function* splitSelectors(selectors) 41 function* splitSelectors(selectors)
41 { 42 {
42 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 43 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
43 // even fewer compound selectors, in a rule. The exact number of selectors 44 // even fewer compound selectors, in a rule. The exact number of selectors
44 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2). 45 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
45 // Since we don't know the sizes of the selectors here, we simply split them 46 // Since we don't know the sizes of the selectors here, we simply split them
46 // into groups of 1,024, based on the reasonable assumption that the average 47 // into groups of 1,024, based on the reasonable assumption that the average
47 // selector won't have a size greater than 8. The alternative would be to 48 // selector won't have a size greater than 8. The alternative would be to
48 // calculate the sizes of the selectors and divide them up accordingly, but 49 // calculate the sizes of the selectors and divide them up accordingly, but
(...skipping 11 matching lines...) Expand all
60 yield selectorGroup.join(", ") + " {display: none !important;}"; 61 yield selectorGroup.join(", ") + " {display: none !important;}";
61 } 62 }
62 63
63 function createStyleSheet(selectors) 64 function createStyleSheet(selectors)
64 { 65 {
65 return Array.from(createRules(selectors)).join("\n"); 66 return Array.from(createRules(selectors)).join("\n");
66 } 67 }
67 68
68 function addStyleSheet(tabId, frameId, styleSheet) 69 function addStyleSheet(tabId, frameId, styleSheet)
69 { 70 {
70 browser.tabs.insertCSS(tabId, { 71 try
71 code: styleSheet, 72 {
72 cssOrigin: "user", 73 browser.tabs.insertCSS(tabId, {
73 frameId, 74 code: styleSheet,
74 matchAboutBlank: true, 75 cssOrigin: "user",
75 runAt: "document_start" 76 frameId,
76 }); 77 matchAboutBlank: true,
78 runAt: "document_start"
79 });
80 }
81 catch (error)
82 {
83 userStyleSheetsSupported = false;
84 }
85
86 return userStyleSheetsSupported;
77 } 87 }
78 88
79 function removeStyleSheet(tabId, frameId, styleSheet) 89 function removeStyleSheet(tabId, frameId, styleSheet)
80 { 90 {
81 // Chromium's support for tabs.removeCSS is still a work in progress and the
kzar 2018/01/31 11:11:32 IMO this comment should go by the styleSheetRemova
Manish Jethani 2018/01/31 12:36:27 Done.
82 // API is likely to be different from Firefox's; for now we just don't use it
83 // at all, even if it's available.
84 // See https://crbug.com/608854
85 if (!styleSheetRemovalSupported) 91 if (!styleSheetRemovalSupported)
86 return; 92 return;
87 93
88 browser.tabs.removeCSS(tabId, { 94 browser.tabs.removeCSS(tabId, {
89 code: styleSheet, 95 code: styleSheet,
90 cssOrigin: "user", 96 cssOrigin: "user",
91 frameId, 97 frameId,
92 matchAboutBlank: true 98 matchAboutBlank: true
93 }); 99 });
94 } 100 }
(...skipping 10 matching lines...) Expand all
105 111
106 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 112 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
107 113
108 // Ideally we would compare the old and new style sheets and skip this code 114 // Ideally we would compare the old and new style sheets and skip this code
109 // if they're the same, but the old style sheet can be a leftover from a 115 // if they're the same, but the old style sheet can be a leftover from a
110 // previous instance of the frame. We must add the new style sheet 116 // previous instance of the frame. We must add the new style sheet
111 // regardless. 117 // regardless.
112 118
113 // Add the new style sheet first to keep previously hidden elements from 119 // Add the new style sheet first to keep previously hidden elements from
114 // reappearing momentarily. 120 // reappearing momentarily.
115 if (styleSheet) 121 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
116 addStyleSheet(tabId, frameId, styleSheet); 122 return false;
117 123
118 // Sometimes the old and new style sheets can be exactly the same. In such a 124 // Sometimes the old and new style sheets can be exactly the same. In such a
119 // case, do not remove the "old" style sheet, because it is in fact the new 125 // case, do not remove the "old" style sheet, because it is in fact the new
120 // style sheet now. 126 // style sheet now.
121 if (oldStyleSheet && oldStyleSheet != styleSheet) 127 if (oldStyleSheet && oldStyleSheet != styleSheet)
122 removeStyleSheet(tabId, frameId, oldStyleSheet); 128 removeStyleSheet(tabId, frameId, oldStyleSheet);
123 129
124 frame.injectedStyleSheets.set(groupName, styleSheet); 130 frame.injectedStyleSheets.set(groupName, styleSheet);
131 return true;
125 } 132 }
126 133
127 port.on("elemhide.getSelectors", (message, sender) => 134 port.on("elemhide.getSelectors", (message, sender) =>
128 { 135 {
129 let selectors = []; 136 let selectors = [];
130 let emulatedPatterns = []; 137 let emulatedPatterns = [];
131 let trace = devtools && devtools.hasPanel(sender.page); 138 let trace = devtools && devtools.hasPanel(sender.page);
132 let inline = !userStyleSheetsSupported; 139 let inline = !userStyleSheetsSupported;
133 140
134 if (!checkWhitelisted(sender.page, sender.frame, 141 if (!checkWhitelisted(sender.page, sender.frame,
135 RegExpFilter.typeMap.DOCUMENT | 142 RegExpFilter.typeMap.DOCUMENT |
136 RegExpFilter.typeMap.ELEMHIDE)) 143 RegExpFilter.typeMap.ELEMHIDE))
137 { 144 {
138 let hostname = extractHostFromFrame(sender.frame); 145 let hostname = extractHostFromFrame(sender.frame);
139 let specificOnly = checkWhitelisted(sender.page, sender.frame, 146 let specificOnly = checkWhitelisted(sender.page, sender.frame,
140 RegExpFilter.typeMap.GENERICHIDE); 147 RegExpFilter.typeMap.GENERICHIDE);
141 148
142 selectors = ElemHide.getSelectorsForDomain( 149 selectors = ElemHide.getSelectorsForDomain(
143 hostname, 150 hostname,
144 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 151 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
145 ); 152 );
146 153
147 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 154 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
148 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 155 emulatedPatterns.push({selector: filter.selector, text: filter.text});
149 } 156 }
150 157
151 if (!inline) 158 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
152 updateFrameStyles(sender.page.id, sender.frame.id, selectors); 159 selectors, "standard"))
160 {
161 inline = true;
162 }
153 163
154 let response = {trace, inline, emulatedPatterns}; 164 let response = {trace, inline, emulatedPatterns};
155 if (trace || inline) 165 if (trace || inline)
156 response.selectors = selectors; 166 response.selectors = selectors;
157 167
158 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep 168 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep
159 // adding them, which could cause problems with emulated filters as described 169 // adding them, which could cause problems with emulation filters as
160 // in issue #5864. Instead, we can just ask the content script to add styles 170 // described in issue #5864. Instead, we can just ask the content script to
161 // for emulated filters inline. 171 // add styles for emulation filters inline.
162 if (!styleSheetRemovalSupported) 172 if (!styleSheetRemovalSupported)
163 response.inlineEmulated = true; 173 response.inlineEmulated = true;
164 174
165 return response; 175 return response;
166 }); 176 });
167 177
168 port.on("elemhide.injectSelectors", (message, sender) => 178 port.on("elemhide.injectSelectors", (message, sender) =>
169 { 179 {
170 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 180 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
171 message.groupName); 181 message.groupName);
172 }); 182 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld