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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHideHelper.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/elemHideHelper.js
@@ -0,0 +1,113 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2017 eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @module elemHideHelper */
+
+"use strict";
+
+const {RegExpFilter} = require("filterClasses");
+const {ElemHide} = require("elemHide");
+const {checkWhitelisted} = require("whitelisting");
+const {extractHostFromFrame} = require("url");
+const {port} = require("messaging");
+const devtools = require("devtools");
+
+let userStylesheetsSupported = true;
+
+function hideElements(tabId, frameId, selectors)
+{
+ return new Promise(resolve =>
+ {
+ let code = selectors.join(", ") + "{display: none !important;}";
+
+ try
+ {
+ chrome.tabs.insertCSS(tabId,
+ {
+ code,
+ cssOrigin: "user",
+ frameId,
+ matchAboutBlank: true
+ },
+ () =>
+ {
+ 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
+ }
+ );
+ }
+ catch (error)
+ {
+ if (/\bError processing cssOrigin\b/.test(error.message) == -1)
+ throw error;
+
+ userStylesheetsSupported = false;
+ resolve(false);
+ }
+ });
+}
+
+port.on("get-selectors", (msg, sender) =>
+{
+ let selectors;
+ let trace = devtools && devtools.hasPanel(sender.page);
+
+ if (!checkWhitelisted(sender.page, sender.frame,
+ RegExpFilter.typeMap.DOCUMENT |
+ RegExpFilter.typeMap.ELEMHIDE))
+ {
+ let specificOnly = checkWhitelisted(sender.page, sender.frame,
+ RegExpFilter.typeMap.GENERICHIDE);
+ selectors = ElemHide.getSelectorsForDomain(
+ extractHostFromFrame(sender.frame),
+ specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
+ );
+ }
+ else
+ {
+ selectors = [];
+ }
+
+ if (!userStylesheetsSupported)
+ return {selectors, trace, inject: true};
+
+ 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
+ {
+ if (trace)
+ return {selectors, trace, inject: false};
+
+ return {trace, inject: false};
+ }
+
+ return hideElements(sender.page.id, sender.frame.id, selectors)
+ .then(success =>
+ {
+ let response = {trace, inject: !success};
+
+ if (trace || !success)
+ response.selectors = selectors;
+
+ return response;
+ });
+});
+
+port.on("hide-elements", (msg, sender) =>
+{
+ if (!msg.selectors || msg.selectors.length == 0)
+ return true;
+
+ return hideElements(sender.page.id, sender.frame.id, msg.selectors);
+});
« 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