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

Unified Diff: lib/child/cssProperties.js

Issue 29338638: Issue 2401 - Integrate CSS property rule handling in Firefox (Closed)
Patch Set: Addressed comments Created March 30, 2016, 6:41 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 | « no previous file | lib/child/main.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/child/cssProperties.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/child/cssProperties.js
@@ -0,0 +1,97 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 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/>.
+ */
+
+"use strict";
+
+(function()
+{
+ let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
+ let {port} = require("messaging");
+ let {getFrames} = require("child/utils");
+
+ let senderWindow = null;
+
+ let scope = {
+ ext: {
+ backgroundPage: {
+ sendMessage: function(data, callback)
+ {
+ data = {payload: data, frames: getFrames(senderWindow)};
+ if (typeof callback == "function")
+ port.emitWithResponse("cssPropertiesRequest", data).then(callback);
+ else
+ port.emit("cssPropertiesRequest", data);
+ }
+ }
+ }
+ };
+
+ function addUserCSS(window, cssCode)
+ {
+ let uri = Services.io.newURI("data:text/css," + encodeURIComponent(cssCode),
+ null, null);
+ let utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDOMWindowUtils);
+ utils.loadSheet(uri, Ci.nsIDOMWindowUtils.USER_SHEET);
+ }
+
+ function initCSSPropertyFilters()
+ {
+ Services.scriptloader.loadSubScript(
+ "chrome://adblockplus/content/cssProperties.js", scope);
+
+ let onContentWindow = (subject, topic, data) =>
+ {
+ if (!(subject instanceof Ci.nsIDOMWindow))
+ return;
+
+ let onReady = event =>
+ {
+ subject.removeEventListener("DOMContentLoaded", onReady);
+ let handler = new scope.CSSPropertyFilters(subject, selectors =>
+ {
+ if (selectors.length == 0)
+ return;
+
+ addUserCSS(subject, selectors.map(
+ selector => selector + "{display: none !important;}"
+ ).join("\n"));
+ });
+
+ // HACK: The content script just calls ext.backgroundPage.sendMessage
+ // without indicating which window this is about. We'll store the window
+ // here because we know that messaging happens synchronously.
+ senderWindow = subject;
+ handler.load(() => handler.apply());
+ senderWindow = null;
+ };
+
+ subject.addEventListener("DOMContentLoaded", onReady);
+ };
+
+ Services.obs.addObserver(onContentWindow, "content-document-global-created",
+ false);
+ onShutdown.add(() =>
+ {
+ Services.obs.removeObserver(onContentWindow,
+ "content-document-global-created");
+ });
+ }
+
+ initCSSPropertyFilters();
+})();
« no previous file with comments | « no previous file | lib/child/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld