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

Side by Side Diff: lib/cssInjection.js

Issue 29545645: Issue 5695 - Use tabs.insertCSS if extensionTypes.CSSOrigin exists (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Always pass selectors if trace or inject is true Created Sept. 19, 2017, 5:44 a.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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module cssInjection */ 18 /** @module cssInjection */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("filterClasses"); 22 const {RegExpFilter} = require("filterClasses");
23 const {ElemHide} = require("elemHide"); 23 const {ElemHide} = require("elemHide");
24 const {checkWhitelisted} = require("whitelisting"); 24 const {checkWhitelisted} = require("whitelisting");
25 const {extractHostFromFrame} = require("url"); 25 const {extractHostFromFrame} = require("url");
26 const {port} = require("messaging"); 26 const {port} = require("messaging");
27 const devtools = require("devtools"); 27 const devtools = require("devtools");
28 28
29 let userStylesheetsSupported = true; 29 const userStyleSheetsSupported = "extensionTypes" in chrome &&
30 "CSSOrigin" in chrome.extensionTypes;
30 31
31 function hideElements(tabId, frameId, selectors) 32 function hideElements(tabId, frameId, selectors)
32 { 33 {
33 let code = selectors.join(", ") + "{display: none !important;}"; 34 chrome.tabs.insertCSS(tabId, {
34 35 code: selectors.join(", ") + "{display: none !important;}",
35 try 36 cssOrigin: "user",
36 { 37 frameId,
37 chrome.tabs.insertCSS(tabId, 38 matchAboutBlank: true,
38 { 39 runAt: "document_start"
39 code, 40 });
40 cssOrigin: "user",
41 frameId,
42 matchAboutBlank: true,
43 runAt: "document_start"
44 }
45 );
46 return true;
47 }
48 catch (error)
49 {
50 if (/\bError processing cssOrigin\b/.test(error.message) == -1)
51 throw error;
52
53 userStylesheetsSupported = false;
54 return false;
55 }
56 } 41 }
57 42
58 port.on("elemhide.getSelectors", (msg, sender) => 43 port.on("elemhide.getSelectors", (msg, sender) =>
59 { 44 {
60 let selectors; 45 let selectors = [];
61 let trace = devtools && devtools.hasPanel(sender.page); 46 let trace = devtools && devtools.hasPanel(sender.page);
47 let inject = !userStyleSheetsSupported;
62 48
63 if (!checkWhitelisted(sender.page, sender.frame, 49 if (!checkWhitelisted(sender.page, sender.frame,
64 RegExpFilter.typeMap.DOCUMENT | 50 RegExpFilter.typeMap.DOCUMENT |
65 RegExpFilter.typeMap.ELEMHIDE)) 51 RegExpFilter.typeMap.ELEMHIDE))
66 { 52 {
67 let specificOnly = checkWhitelisted(sender.page, sender.frame, 53 let specificOnly = checkWhitelisted(sender.page, sender.frame,
68 RegExpFilter.typeMap.GENERICHIDE); 54 RegExpFilter.typeMap.GENERICHIDE);
69 selectors = ElemHide.getSelectorsForDomain( 55 selectors = ElemHide.getSelectorsForDomain(
70 extractHostFromFrame(sender.frame), 56 extractHostFromFrame(sender.frame),
71 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 57 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
72 ); 58 );
73 } 59
74 else 60 if (!inject && selectors.length > 0)
75 { 61 hideElements(sender.page.id, sender.frame.id, selectors);
76 selectors = [];
77 } 62 }
78 63
79 if (selectors.length == 0 || userStylesheetsSupported && 64 let response = {trace, inject};
80 hideElements(sender.page.id, sender.frame.id, selectors))
81 {
82 if (trace)
83 return {selectors, trace: true, inject: false};
84 65
Sebastian Noack 2017/09/19 23:09:17 Nit: I would remove this blank line, so that the c
Manish Jethani 2017/09/20 01:36:30 Acknowledged. But this is outdated now with the l
Sebastian Noack 2017/09/20 01:51:52 This comment is on the latest patch in this review
Manish Jethani 2017/09/20 02:24:00 Rietveld may have messed up, because it looks like
Manish Jethani 2017/09/20 02:24:00 I'm assuming you meant delete this blank line here
85 return {trace: false, inject: false}; 66 if (trace || inject)
86 } 67 response.selectors = selectors;
87 68
88 return {selectors, trace, inject: true}; 69 return response;
89 }); 70 });
90 71
91 port.on("elemhide.injectSelectors", (msg, sender) => 72 port.on("elemhide.injectSelectors", (msg, sender) =>
92 { 73 {
93 return hideElements(sender.page.id, sender.frame.id, msg.selectors); 74 hideElements(sender.page.id, sender.frame.id, msg.selectors);
94 }); 75 });
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