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

Side by Side Diff: safari/common.js

Issue 16067002: Added Safari Support (Closed)
Patch Set: Adressed comments Created Nov. 13, 2013, 3:11 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 | « safari/background.js ('k') | safari/content.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-2013 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() {
19 /* Events */
20
21 WrappedEventTarget = function(target, eventName, capture)
22 {
23 this._listeners = [];
24 this._wrappedListeners = [];
25
26 this._target = target;
27 this._eventName = eventName;
28 this._capture = capture;
29 };
30 WrappedEventTarget.prototype = {
31 addListener: function(listener)
32 {
33 var wrappedListener = this._wrapListener(listener);
34
35 this._listeners.push(listener);
36 this._wrappedListeners.push(wrappedListener);
37
38 this._target.addEventListener(
39 this._eventName,
40 wrappedListener,
41 this._capture
42 );
43 },
44 removeListener: function(listener)
45 {
46 var idx = this._listeners.indexOf(listener);
47
48 if (idx != -1)
49 {
50 this._target.removeEventListener(
51 this._eventName,
52 this._wrappedListeners[idx],
53 this._capture
54 );
55
56 this._listeners.splice(idx, 1);
57 this._wrappedListeners.splice(idx, 1);
58 }
59 }
60 };
61
62
63 MessageEventTarget = function(target)
64 {
65 WrappedEventTarget.call(this, target, "message", false);
66 };
67 MessageEventTarget.prototype = {
68 __proto__: WrappedEventTarget.prototype,
69 _wrapListener: function(listener)
70 {
71 return function(event)
72 {
73 if (event.name.indexOf("request-") != 0)
74 return;
75
76 var sender = {};
77 var dispatcher;
78
79 if (event.target instanceof SafariBrowserTab)
80 {
81 dispatcher = event.target.page;
82 sender.tab = new Tab(event.target);
83 }
84 else
85 {
86 dispatcher = event.target.tab;
87 sender.tab = null;
88 }
89
90 listener(event.message, sender, function(message)
91 {
92 dispatcher.dispatchMessage("response-" + event.name.substr(8), message );
93 });
94 };
95 }
96 };
97
98
99 /* Message passing */
100
101 var requestCounter = 0;
102
103 sendMessage = function(message, responseCallback)
104 {
105 var requestId = ++requestCounter;
106
107 if (responseCallback)
108 {
109 var eventTarget = this._eventTarget;
110 var responseListener = function(event)
111 {
112 if (event.name == "response-" + requestId)
113 {
114 eventTarget.removeEventListener("message", responseListener, false);
115 responseCallback(event.message);
116 }
117 };
118 eventTarget.addEventListener("message", responseListener, false);
119 }
120
121 this._messageDispatcher.dispatchMessage("request-" + requestId, message);
122 };
123
124
125 /* I18n */
126
127 var I18n = function()
128 {
129 this._localeCandidates = this._getLocaleCandidates();
130 this._uiLocale = this._localeCandidates[0];
131 };
132 I18n.prototype = {
133 _getLocaleCandidates: function()
134 {
135 var candidates = [];
136 var defaultLocale = "en_US";
137
138 var bits, i;
139 for (i = (bits = navigator.language.split("-")).length; i > 0; i--)
140 {
141 var locale = bits.slice(0, i).join("_");
142 candidates.push(locale);
143
144 if (locale == defaultLocale)
145 return candidates;
146 }
147
148 candidates.push(defaultLocale);
149 return candidates;
150 },
151 _getCatalog: function(locale)
152 {
153 var xhr = new XMLHttpRequest();
154
155 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messag es.json", false);
156
157 try {
158 xhr.send();
159 }
160 catch (e)
161 {
162 return null;
163 }
164
165 return JSON.parse(xhr.responseText);
166 },
167 getMessage: function(msgId, substitutions)
168 {
169 if (msgId == "@@ui_locale")
170 return this._uiLocale;
171
172 for (var i = 0; i < this._localeCandidates.length; i++)
173 {
174 var catalog = this._getCatalog(this._localeCandidates[i]);
175 if (!catalog)
176 {
177 // if there is no catalog for this locale
178 // candidate, don't try to load it again
179 this._localeCandidates.splice(i--, 1);
180 continue;
181 }
182
183 var msg = catalog[msgId];
184 if (!msg)
185 continue;
186
187 var msgstr = msg.message;
188 if (!msgstr)
189 continue;
190
191 for (var placeholder in msg.placeholders)
192 {
193 var placeholderDetails = msg.placeholders[placeholder];
194 if (!placeholderDetails || !placeholderDetails.content)
195 continue;
196 if (placeholderDetails.content.indexOf("$") != 0)
197 continue;
198
199 var placeholderIdx = parseInt(placeholderDetails.content.substr(1));
200 if (isNaN(placeholderIdx) || placeholderIdx < 1)
201 continue;
202
203 var placeholderValue;
204 if (Object.prototype.toString.call(substitutions) == "[object Array]")
205 placeholderValue = substitutions[placeholderIdx - 1];
206 else if (placeholderIdx == 1)
207 placeholderValue = substitutions;
208
209 msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || " ");
210 }
211
212 return msgstr;
213 }
214
215 return "";
216 }
217 };
218
219
220 /* API */
221
222 ext = {
223 getURL: function(path)
224 {
225 return safari.extension.baseURI + path;
226 },
227 i18n: new I18n()
228 };
229 })();
OLDNEW
« no previous file with comments | « safari/background.js ('k') | safari/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld