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

Unified Diff: ext/content.js

Issue 6180766664884224: Issue 1663 - Made first-run page use an asynchronous messaging protocol (Closed)
Patch Set: Adressed comments Created Dec. 18, 2014, 7:30 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 | « README.md ('k') | firstRun.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/content.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/ext/content.js
@@ -0,0 +1,154 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 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/>.
+ */
+
+(function(global)
+{
+ if (!global.ext)
+ global.ext = {};
+
+ function updateFromURL(data)
+ {
+ if (window.location.search)
+ {
+ var params = window.location.search.substr(1).split("&");
+ for (var i = 0; i < params.length; i++)
+ {
+ var parts = params[i].split("=", 2);
+ if (parts.length == 2 && parts[0] in data)
+ data[parts[0]] = decodeURIComponent(parts[1]);
+ }
+ }
+ }
+
+ var subscriptions =[
+ "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
+ "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
+ "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
+ ];
+
+ var listenerFilter = null;
+
+ global.ext.backgroundPage = {
+ sendMessage: function(message, responseCallback)
+ {
+ var respond = function(response)
+ {
+ setTimeout(responseCallback.bind(responseCallback, response), 0);
+ };
+
+ var dispatchListenerNotification = function(action)
+ {
+ var match = /^subscription\.(.*)/.exec(action);
+ if (match && listenerFilter && listenerFilter.indexOf(match[1]) >= 0)
+ {
+ global.ext.onMessage._dispatch({
+ type: "subscriptions.listen",
+ action: match[1],
+ args: Array.prototype.slice.call(arguments, 1)
+ });
+ }
+ };
+
+ switch (message.type)
+ {
+ case "app.get":
+ if (message.what == "issues")
+ {
+ var response = {seenDataCorruption: false, filterlistsReinitialized: false};
+ updateFromURL(response);
+
+ var info = {platform: "gecko", platformVersion: "34.0", application: "firefox", applicationVersion: "34.0"};
+ updateFromURL(info);
+ response.legacySafariVersion = (info.platform == "safari" && (
+ parseInt(info.platformVersion, 10) < 6 || // beforeload breaks websites in Safari 5
+ info.platformVersion == "6.1" || // extensions are broken in 6.1 and 7.0
+ info.platformVersion == "7.0"));
+
+ respond(response);
+ }
+ else if (message.what == "doclink")
+ respond("https://adblockplus.org/redirect?link=" + encodeURIComponent(message.link));
+ else
+ respond(null);
+ break;
+ case "app.open":
+ if (message.what == "options")
+ window.open("http://example.com/options.html", "_blank");
+ break;
+ case "subscriptions.get":
+ respond(subscriptions);
+ break;
+ case "filters.blocked":
+ var params = {blockedURLs: ""};
+ updateFromURL(params);
+ var blocked = params.blockedURLs.split(",");
+ respond(blocked.indexOf(message.url) >= 0);
+ break;
+ case "subscriptions.toggle":
+ var index = subscriptions.indexOf(message.url);
+ if (index >= 0)
+ {
+ subscriptions.splice(index, 1);
+ dispatchListenerNotification("subscription.removed", message.url);
+ }
+ else
+ {
+ subscriptions.push(message.url);
+ dispatchListenerNotification("subscription.added", message.url);
+ }
+ break;
+ case "subscriptions.listen":
+ listenerFilter = message.filter;
+ break;
+ }
+ }
+ };
+
+ var EventTarget = function(cancelable)
+ {
+ this._listeners = [];
+ this._cancelable = cancelable;
+ };
+ EventTarget.prototype = {
+ addListener: function(listener)
+ {
+ if (this._listeners.indexOf(listener) == -1)
+ this._listeners.push(listener);
+ },
+ removeListener: function(listener)
+ {
+ var idx = this._listeners.indexOf(listener);
+ if (idx != -1)
+ this._listeners.splice(idx, 1);
+ },
+ _dispatch: function()
+ {
+ var result = null;
+
+ for (var i = 0; i < this._listeners.length; i++)
+ {
+ result = this._listeners[i].apply(null, arguments);
+
+ if (this._cancelable && result === false)
+ break;
+ }
+
+ return result;
+ }
+ };
+ global.ext.onMessage = new EventTarget();
+})(this);
« no previous file with comments | « README.md ('k') | firstRun.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld