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: Skip false properties Created Sept. 15, 2017, 9:23 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
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 let userStyleSheetsSupported = "extensionTypes" in chrome &&
Sebastian Noack 2017/09/15 22:52:44 Use const here?
Manish Jethani 2017/09/17 13:51:37 Done.
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 = null;
61 let trace = devtools && devtools.hasPanel(sender.page); 46 let trace = devtools && devtools.hasPanel(sender.page);
62 47
63 if (!checkWhitelisted(sender.page, sender.frame, 48 if (!checkWhitelisted(sender.page, sender.frame,
64 RegExpFilter.typeMap.DOCUMENT | 49 RegExpFilter.typeMap.DOCUMENT |
65 RegExpFilter.typeMap.ELEMHIDE)) 50 RegExpFilter.typeMap.ELEMHIDE))
66 { 51 {
67 let specificOnly = checkWhitelisted(sender.page, sender.frame, 52 let specificOnly = checkWhitelisted(sender.page, sender.frame,
68 RegExpFilter.typeMap.GENERICHIDE); 53 RegExpFilter.typeMap.GENERICHIDE);
69 selectors = ElemHide.getSelectorsForDomain( 54 selectors = ElemHide.getSelectorsForDomain(
70 extractHostFromFrame(sender.frame), 55 extractHostFromFrame(sender.frame),
71 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 56 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
72 ); 57 );
73 } 58 }
74 else
75 {
76 selectors = [];
77 }
78 59
79 if (selectors.length == 0 || userStylesheetsSupported && 60 if (!selectors || selectors.length == 0)
80 hideElements(sender.page.id, sender.frame.id, selectors)) 61 return {};
Sebastian Noack 2017/09/15 22:52:44 Actually, we have to pass "trace", even if there a
Manish Jethani 2017/09/17 13:51:37 Done. See my other comments.
81 {
82 if (trace)
83 return {selectors, trace: true, inject: false};
84 62
85 return {trace: false, inject: false}; 63 if (!userStyleSheetsSupported)
Manish Jethani 2017/09/15 21:26:09 I've kept the userStyleSheetsSupported flag since
86 } 64 return {selectors, trace, inject: true};
87 65
88 return {selectors, trace, inject: true}; 66 hideElements(sender.page.id, sender.frame.id, selectors);
67
68 if (trace)
Manish Jethani 2017/09/15 21:26:09 This return statement could be merged into the nex
Sebastian Noack 2017/09/15 22:52:44 I see the point for not including selectors=null (
Manish Jethani 2017/09/17 13:51:37 Done.
69 return {selectors, trace: true};
70
71 return {};
89 }); 72 });
90 73
91 port.on("elemhide.injectSelectors", (msg, sender) => 74 port.on("elemhide.injectSelectors", (msg, sender) =>
92 { 75 {
93 return hideElements(sender.page.id, sender.frame.id, msg.selectors); 76 hideElements(sender.page.id, sender.frame.id, msg.selectors);
94 }); 77 });
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