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: Flatten out logic and remove blank line Created Sept. 20, 2017, 2:14 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
75 {
76 selectors = [];
77 }
78 60
79 if (selectors.length == 0 || userStylesheetsSupported && 61 if (!inject && selectors.length > 0)
80 hideElements(sender.page.id, sender.frame.id, selectors)) 62 hideElements(sender.page.id, sender.frame.id, selectors);
81 {
82 if (trace)
83 return {selectors, trace: true, inject: false};
84 63
85 return {trace: false, inject: false}; 64 let response = {trace, inject};
86 } 65 if (trace || inject)
66 response.selectors = selectors;
87 67
88 return {selectors, trace, inject: true}; 68 return response;
89 }); 69 });
90 70
91 port.on("elemhide.injectSelectors", (msg, sender) => 71 port.on("elemhide.injectSelectors", (msg, sender) =>
92 { 72 {
93 return hideElements(sender.page.id, sender.frame.id, msg.selectors); 73 hideElements(sender.page.id, sender.frame.id, msg.selectors);
94 }); 74 });
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