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

Side by Side Diff: ext/content.js

Issue 6180766664884224: Issue 1663 - Made first-run page use an asynchronous messaging protocol (Closed)
Patch Set: More common approach to wrapping code Created Dec. 17, 2014, 1:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | firstRun.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 (function(global)
19 {
20 if (!global.ext)
21 global.ext = {};
22
23 function getURLParameters(data)
24 {
25 if (window.location.search)
26 {
27 var params = window.location.search.substr(1).split("&");
28 for (var i = 0; i < params.length; i++)
29 {
30 var parts = params[i].split("=", 2);
31 if (parts.length == 2 && parts[0] in data)
32 data[parts[0]] = decodeURIComponent(parts[1]);
33 }
34 }
35 }
36
37 var subscriptions =[
38 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
39 "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
40 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
41 ];
42
43 var listenerFilter = null;
44
45 global.ext.backgroundPage = {
46 sendMessage: function(message, responseCallback)
47 {
48 var respond = function(response)
49 {
50 setTimeout(responseCallback.bind(responseCallback, response), 0);
51 };
52
53 var dispatchListenerNotification = function(action)
54 {
55 var match = /^subscription\.(.*)/.exec(action);
56 if (match && listenerFilter && listenerFilter.indexOf(match[1]) >= 0)
57 {
58 global.ext.onMessage._dispatch({
59 type: "subscriptions.listen",
60 action: match[1],
61 args: Array.prototype.slice.call(arguments, 1)
62 });
63 }
64 };
65
66 switch (message.type)
67 {
68 case "app.doclink":
69 respond("https://adblockplus.org/redirect?link=" + encodeURIComponent( message.args[0]));
70 break;
71 case "app.info":
72 var response = {platform: "gecko", platformVersion: "34.0", applicatio n: "firefox", applicationVersion: "34.0"};
73 getURLParameters(response);
74 respond(response);
75 break;
76 case "app.issues":
77 var response = {seenDataCorruption: false, filterlistsReinitialized: f alse};
78 getURLParameters(response);
79 respond(response);
80 break;
81 case "app.options":
82 window.open("http://example.com/options.html", "_blank");
83 break;
84 case "subscriptions.get":
85 respond(subscriptions);
86 break;
87 case "filters.blocked":
88 var params = {blockedURLs: ""};
89 getURLParameters(params);
90 var blocked = params.blockedURLs.split(",");
91 respond(blocked.indexOf(message.url) >= 0);
92 break;
93 case "subscriptions.toggle":
94 var index = subscriptions.indexOf(message.url);
95 if (index >= 0)
96 {
97 subscriptions.splice(index, 1);
98 dispatchListenerNotification("subscription.removed", message.url);
99 }
100 else
101 {
102 subscriptions.push(message.url);
103 dispatchListenerNotification("subscription.added", message.url);
104 }
105 break;
106 case "subscriptions.listen":
107 listenerFilter = message.filter;
108 break;
109 }
110 }
111 };
112
113 var EventTarget = function(cancelable)
114 {
115 this._listeners = [];
116 this._cancelable = cancelable;
117 };
118 EventTarget.prototype = {
119 addListener: function(listener)
120 {
121 if (this._listeners.indexOf(listener) == -1)
122 this._listeners.push(listener);
123 },
124 removeListener: function(listener)
125 {
126 var idx = this._listeners.indexOf(listener);
127 if (idx != -1)
128 this._listeners.splice(idx, 1);
129 },
130 _dispatch: function()
131 {
132 var result = null;
133
134 for (var i = 0; i < this._listeners.length; i++)
135 {
136 result = this._listeners[i].apply(null, arguments);
137
138 if (this._cancelable && result === false)
139 break;
140 }
141
142 return result;
143 }
144 };
145 global.ext.onMessage = new EventTarget();
146 })(this);
OLDNEW
« no previous file with comments | « README.md ('k') | firstRun.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld