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

Powered by Google App Engine
This is Rietveld