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

Side by Side Diff: lib/cssInjection.js

Issue 29738592: Issue 6507 - Inject style sheet proactively from background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Polish up Created March 31, 2018, 5: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') | 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // Sometimes the old and new style sheets can be exactly the same. In such a 150 // Sometimes the old and new style sheets can be exactly the same. In such a
151 // case, do not remove the "old" style sheet, because it is in fact the new 151 // case, do not remove the "old" style sheet, because it is in fact the new
152 // style sheet now. 152 // style sheet now.
153 if (oldStyleSheet && oldStyleSheet != styleSheet) 153 if (oldStyleSheet && oldStyleSheet != styleSheet)
154 removeStyleSheet(tabId, frameId, oldStyleSheet); 154 removeStyleSheet(tabId, frameId, oldStyleSheet);
155 155
156 frame.injectedStyleSheets.set(groupName, styleSheet); 156 frame.injectedStyleSheets.set(groupName, styleSheet);
157 return true; 157 return true;
158 } 158 }
159 159
160 port.on("elemhide.getSelectors", (message, sender) => 160 function doElementHiding(page, frame)
161 { 161 {
162 let selectors = []; 162 let selectors = [];
163 let emulatedPatterns = []; 163 let emulatedPatterns = [];
164 let trace = devtools && devtools.hasPanel(sender.page); 164 let trace = devtools && devtools.hasPanel(page);
165 let inline = !userStyleSheetsSupported; 165 let inline = !userStyleSheetsSupported;
166 166
167 if (!checkWhitelisted(sender.page, sender.frame, 167 if (!checkWhitelisted(page, frame,
168 RegExpFilter.typeMap.DOCUMENT | 168 RegExpFilter.typeMap.DOCUMENT |
169 RegExpFilter.typeMap.ELEMHIDE)) 169 RegExpFilter.typeMap.ELEMHIDE))
170 { 170 {
171 let hostname = extractHostFromFrame(sender.frame); 171 let hostname = extractHostFromFrame(frame);
172 let specificOnly = checkWhitelisted(sender.page, sender.frame, 172 let specificOnly = checkWhitelisted(page, frame,
173 RegExpFilter.typeMap.GENERICHIDE); 173 RegExpFilter.typeMap.GENERICHIDE);
174 174
175 selectors = ElemHide.getSelectorsForDomain( 175 selectors = ElemHide.getSelectorsForDomain(
176 hostname, 176 hostname,
177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 177 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
178 ); 178 );
179 179
180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 180 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
181 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 181 emulatedPatterns.push({selector: filter.selector, text: filter.text});
182 } 182 }
183 183
184 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 184 if (!inline && !updateFrameStyles(page.id, frame.id,
185 selectors, "standard")) 185 selectors, "standard"))
186 { 186 {
187 inline = true; 187 inline = true;
188 } 188 }
189 189
190 let response = {trace, inline, emulatedPatterns}; 190 let message = {type: "elemhide.apply", trace, inline};
191 if (trace || inline) 191 if (trace || inline)
192 response.selectors = selectors; 192 message.selectors = selectors;
193
194 if (emulatedPatterns.length > 0)
195 message.emulatedPatterns = emulatedPatterns;
193 196
194 // If we can't remove user style sheets using tabs.removeCSS, we'll only keep 197 // 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 198 // adding them, which could cause problems with emulation filters as
196 // described in issue #5864. Instead, we can just ask the content script to 199 // described in issue #5864. Instead, we can just ask the content script to
197 // add styles for emulation filters inline. 200 // add styles for emulation filters inline.
198 if (!styleSheetRemovalSupported) 201 if (!styleSheetRemovalSupported)
199 response.inlineEmulated = true; 202 message.inlineEmulated = true;
200 203
201 return response; 204 // In most cases on modern browsers there's nothing for the content script to
205 // do.
206 if (!message.selectors && !message.emulatedPatterns)
207 return;
208
209 // 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
211 // times before we get through.
212 browser.tabs.sendMessage(page.id, message, {frameId: frame.id})
213 .catch(() =>
214 browser.tabs.sendMessage(page.id, message, {frameId: frame.id})
215 )
216 .catch(() =>
217 browser.tabs.sendMessage(page.id, message, {frameId: frame.id})
218 );
219 }
220
221 browser.webNavigation.onCommitted.addListener(details =>
222 {
223 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(details.url))
224 return;
225
226 let page = new ext.Page({id: details.tabId, url: details.url});
227 let frame = ext.getFrame(details.tabId, details.frameId);
228
229 if (!frame)
230 return;
231
232 doElementHiding(page, frame);
233 });
234
235 port.on("elemhide.needApply", (message, sender) =>
236 {
237 doElementHiding(sender.page, sender.frame);
202 }); 238 });
203 239
204 port.on("elemhide.injectSelectors", (message, sender) => 240 port.on("elemhide.injectSelectors", (message, sender) =>
205 { 241 {
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 242 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
207 message.groupName); 243 message.groupName);
208 }); 244 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld