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

Side by Side Diff: lib/elemHideHelper.js

Issue 29410607: Issue 5090 - Use user stylesheets for element hiding if possible (Closed)
Patch Set: Further simplify addSelectors Created June 2, 2017, 3:39 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
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 /** @module elemHideHelper */
19
20 "use strict";
21
22 const {RegExpFilter} = require("filterClasses");
23 const {ElemHide} = require("elemHide");
24 const {checkWhitelisted} = require("whitelisting");
25 const {extractHostFromFrame} = require("url");
26 const {port} = require("messaging");
27 const devtools = require("devtools");
28
29 let userStylesheetsSupported = true;
30
31 function hideElements(tabId, frameId, selectors)
32 {
33 let code = selectors.join(", ") + "{display: none !important;}";
34
35 try
36 {
37 chrome.tabs.insertCSS(tabId,
38 {
39 code,
40 cssOrigin: "user",
41 frameId,
42 matchAboutBlank: true
43 }
44 );
45 return true;
46 }
47 catch (error)
48 {
49 if (/\bError processing cssOrigin\b/.test(error.message) == -1)
50 throw error;
51
52 userStylesheetsSupported = false;
53 return false;
54 }
55 }
56
57 port.on("get-selectors", (msg, sender) =>
Sebastian Noack 2017/07/07 16:16:09 In new code we use component.method (camel-cased)
Manish Jethani 2017/07/09 12:09:38 Done.
58 {
59 let selectors;
60 let trace = devtools && devtools.hasPanel(sender.page);
61
62 if (!checkWhitelisted(sender.page, sender.frame,
63 RegExpFilter.typeMap.DOCUMENT |
64 RegExpFilter.typeMap.ELEMHIDE))
65 {
66 let specificOnly = checkWhitelisted(sender.page, sender.frame,
67 RegExpFilter.typeMap.GENERICHIDE);
68 selectors = ElemHide.getSelectorsForDomain(
69 extractHostFromFrame(sender.frame),
70 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
71 );
72 }
73 else
74 {
75 selectors = [];
76 }
77
78 if (selectors.length == 0 || userStylesheetsSupported &&
79 hideElements(sender.page.id, sender.frame.id, selectors))
80 {
81 if (trace)
82 return {selectors, trace: true, inject: false};
83
84 return {trace: false, inject: false};
85 }
86
87 return {selectors, trace, inject: true};
88 });
89
90 port.on("hide-elements", (msg, sender) =>
91 {
92 return hideElements(sender.page.id, sender.frame.id, msg.selectors);
93 });
OLDNEW

Powered by Google App Engine
This is Rietveld