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 synchronous Created June 1, 2017, 9:50 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,93 @@
+/*
+ * 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)
+{
+ let code = selectors.join(", ") + "{display: none !important;}";
+
+ try
+ {
+ chrome.tabs.insertCSS(tabId,
+ {
+ code,
+ cssOrigin: "user",
+ frameId,
+ matchAboutBlank: true
+ }
+ );
+ return true;
+ }
+ catch (error)
+ {
+ if (/\bError processing cssOrigin\b/.test(error.message) == -1)
+ throw error;
+
+ userStylesheetsSupported = false;
+ return 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 (selectors.length == 0 || userStylesheetsSupported &&
+ hideElements(sender.page.id, sender.frame.id, selectors))
+ {
+ if (trace)
+ return {selectors, trace: true, inject: false};
+
+ return {trace: false, inject: false};
+ }
+
+ return {selectors, trace, inject: true};
+});
+
+port.on("hide-elements", (msg, sender) =>
+{
+ 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