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); |
+}); |