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: Make hideElements return a promise Created May 24, 2017, 5:07 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') | metadata.chrome » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 return new Promise(resolve =>
34 {
35 let code = selectors.join(", ") + "{display: none !important;}";
36
37 try
38 {
39 chrome.tabs.insertCSS(tabId,
40 {
41 code,
42 cssOrigin: "user",
43 frameId,
44 matchAboutBlank: true
45 },
46 () =>
47 {
48 resolve(!chrome.runtime.lastError);
Sebastian Noack 2017/05/24 18:14:06 Asynchronous errors are still silenced, as they on
Manish Jethani 2017/05/25 02:52:26 We need to resolve the promise, and if it failed t
Sebastian Noack 2017/05/30 15:50:31 What is the scenario in which we'd get an asynchro
Manish Jethani 2017/05/31 06:21:39 I couldn't get Firefox to give an error in the res
Sebastian Noack 2017/05/31 11:13:30 In the case of invalid CSS, falling back to the co
Manish Jethani 2017/05/31 14:41:15 OK, that makes sense. Let's ignore any possible er
49 }
50 );
51 }
52 catch (error)
53 {
54 if (/\bError processing cssOrigin\b/.test(error.message) == -1)
55 throw error;
56
57 userStylesheetsSupported = false;
58 resolve(false);
59 }
60 });
61 }
62
63 port.on("get-selectors", (msg, sender) =>
64 {
65 let selectors;
66 let trace = devtools && devtools.hasPanel(sender.page);
67
68 if (!checkWhitelisted(sender.page, sender.frame,
69 RegExpFilter.typeMap.DOCUMENT |
70 RegExpFilter.typeMap.ELEMHIDE))
71 {
72 let specificOnly = checkWhitelisted(sender.page, sender.frame,
73 RegExpFilter.typeMap.GENERICHIDE);
74 selectors = ElemHide.getSelectorsForDomain(
75 extractHostFromFrame(sender.frame),
76 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
77 );
78 }
79 else
80 {
81 selectors = [];
82 }
83
84 if (!userStylesheetsSupported)
85 return {selectors, trace, inject: true};
86
87 if (selectors.length == 0)
Sebastian Noack 2017/05/24 18:14:05 Why do we have to treat no selectors as a special
Manish Jethani 2017/05/25 02:52:26 I thought you said a little bit earlier in the rev
Sebastian Noack 2017/05/30 15:50:31 Yes, but the content script is already checking fo
Manish Jethani 2017/05/31 06:21:39 First we try to insert the selectors in the backgr
88 {
89 if (trace)
90 return {selectors, trace, inject: false};
91
92 return {trace, inject: false};
93 }
94
95 return hideElements(sender.page.id, sender.frame.id, selectors)
96 .then(success =>
97 {
98 let response = {trace, inject: !success};
99
100 if (trace || !success)
101 response.selectors = selectors;
102
103 return response;
104 });
105 });
106
107 port.on("hide-elements", (msg, sender) =>
108 {
109 if (!msg.selectors || msg.selectors.length == 0)
110 return true;
111
112 return hideElements(sender.page.id, sender.frame.id, msg.selectors);
113 });
OLDNEW
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld