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: Adressed comments Created Dec. 18, 2014, 7:30 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 updateFromURL(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.get":
69 if (message.what == "issues")
70 {
71 var response = {seenDataCorruption: false, filterlistsReinitialized: false};
72 updateFromURL(response);
73
74 var info = {platform: "gecko", platformVersion: "34.0", application: "firefox", applicationVersion: "34.0"};
75 updateFromURL(info);
76 response.legacySafariVersion = (info.platform == "safari" && (
77 parseInt(info.platformVersion, 10) < 6 || // beforeload breaks we bsites in Safari 5
78 info.platformVersion == "6.1" || // extensions are broke n in 6.1 and 7.0
79 info.platformVersion == "7.0"));
80
81 respond(response);
82 }
83 else if (message.what == "doclink")
84 respond("https://adblockplus.org/redirect?link=" + encodeURIComponen t(message.link));
85 else
86 respond(null);
87 break;
88 case "app.open":
89 if (message.what == "options")
90 window.open("http://example.com/options.html", "_blank");
91 break;
92 case "subscriptions.get":
93 respond(subscriptions);
94 break;
95 case "filters.blocked":
96 var params = {blockedURLs: ""};
97 updateFromURL(params);
98 var blocked = params.blockedURLs.split(",");
99 respond(blocked.indexOf(message.url) >= 0);
100 break;
101 case "subscriptions.toggle":
102 var index = subscriptions.indexOf(message.url);
103 if (index >= 0)
104 {
105 subscriptions.splice(index, 1);
106 dispatchListenerNotification("subscription.removed", message.url);
107 }
108 else
109 {
110 subscriptions.push(message.url);
111 dispatchListenerNotification("subscription.added", message.url);
112 }
113 break;
114 case "subscriptions.listen":
115 listenerFilter = message.filter;
116 break;
117 }
118 }
119 };
120
121 var EventTarget = function(cancelable)
122 {
123 this._listeners = [];
124 this._cancelable = cancelable;
125 };
126 EventTarget.prototype = {
127 addListener: function(listener)
128 {
129 if (this._listeners.indexOf(listener) == -1)
130 this._listeners.push(listener);
131 },
132 removeListener: function(listener)
133 {
134 var idx = this._listeners.indexOf(listener);
135 if (idx != -1)
136 this._listeners.splice(idx, 1);
137 },
138 _dispatch: function()
139 {
140 var result = null;
141
142 for (var i = 0; i < this._listeners.length; i++)
143 {
144 result = this._listeners[i].apply(null, arguments);
145
146 if (this._cancelable && result === false)
147 break;
148 }
149
150 return result;
151 }
152 };
153 global.ext.onMessage = new EventTarget();
154 })(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