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: Move get-selectors and hide-elements to elemHideHelper Created May 1, 2017, 11:04 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
diff --git a/background.js b/lib/elemHideHelper.js
similarity index 56%
copy from background.js
copy to lib/elemHideHelper.js
index cb7a861be117f6a390b74ffa1370bb9de50ba33f..83de8426f36c396ac69b747d586e935bf979732d 100644
--- a/background.js
+++ b/lib/elemHideHelper.js
@@ -15,6 +15,8 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+/** @module elemHideHelper */
+
"use strict";
const {RegExpFilter} = require("filterClasses");
@@ -23,6 +25,28 @@ const {checkWhitelisted} = require("whitelisting");
const {extractHostFromFrame} = require("url");
const {port} = require("messaging");
const devtools = require("devtools");
+const info = require("info");
+
+let userStylesheetsSupported = info.platform == "gecko" &&
+ info.platformVersion.split(".")[0] >= 53;
+
+function hideElements(tabId, frameId, selectors, callback)
+{
+ let code = selectors.join(", ") + "{display: none !important;}";
+
+ chrome.tabs.insertCSS(tabId,
+ {
+ code,
+ cssOrigin: "user",
+ frameId,
+ matchAboutBlank: true
+ },
+ () =>
+ {
+ callback(chrome.runtime.lastError);
+ }
+ );
+}
port.on("get-selectors", (msg, sender) =>
{
@@ -45,22 +69,41 @@ port.on("get-selectors", (msg, sender) =>
selectors = [];
}
- return {selectors, trace};
+ if (!userStylesheetsSupported)
+ return {selectors, trace, inject: true};
+
+ if (selectors.length == 0)
+ {
+ if (trace)
+ return {selectors, trace, inject: false};
+
+ return {trace, inject: false};
+ }
+
+ return new Promise(resolve =>
+ {
+ hideElements(sender.page.id, sender.frame.id, selectors, error =>
+ {
+ let response = {trace, inject: !!error};
+
+ if (trace || error)
+ response.selectors = selectors;
+
+ resolve(response);
+ });
+ });
});
-port.on("forward", (msg, sender) =>
+port.on("hide-elements", (msg, sender) =>
{
- let targetPage;
- if (msg.targetPageId)
- targetPage = ext.getPage(msg.targetPageId);
- else
- targetPage = sender.page;
+ if (!msg.selectors || msg.selectors.length == 0)
+ return {success: true};
- if (targetPage)
+ return new Promise(resolve =>
{
- msg.payload.sender = sender.page.id;
- if (msg.expectsResponse)
- return new Promise(targetPage.sendMessage.bind(targetPage, msg.payload));
- targetPage.sendMessage(msg.payload);
- }
+ hideElements(sender.page.id, sender.frame.id, msg.selectors, error =>
+ {
+ resolve({success: !error});
+ });
+ });
});
« 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