LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 61 |
62 | 62 |
63 MessageEventTarget = function(target) | 63 MessageEventTarget = function(target) |
64 { | 64 { |
65 WrappedEventTarget.call(this, target, "message", false); | 65 WrappedEventTarget.call(this, target, "message", false); |
66 }; | 66 }; |
67 MessageEventTarget.prototype = { | 67 MessageEventTarget.prototype = { |
68 __proto__: WrappedEventTarget.prototype, | 68 __proto__: WrappedEventTarget.prototype, |
69 _wrapListener: function(listener) | 69 _wrapListener: function(listener) |
70 { | 70 { |
71 return function(event) { | 71 return function(event) |
| 72 { |
72 if (event.name.indexOf("request-") != 0) | 73 if (event.name.indexOf("request-") != 0) |
73 return; | 74 return; |
74 | 75 |
75 var sender = {}; | 76 var sender = {}; |
76 var dispatcher; | 77 var dispatcher; |
77 | 78 |
78 if (event.target instanceof SafariBrowserTab) | 79 if ("SafariBrowserTab" in window && event.target instanceof SafariBrowse
rTab) |
79 { | 80 { |
80 dispatcher = event.target.page; | 81 dispatcher = event.target.page; |
81 sender.tab = new Tab(event.target); | 82 sender.tab = new Tab(event.target); |
82 } | 83 } |
83 else | 84 else |
84 { | 85 { |
85 dispatcher = event.target.tab; | 86 dispatcher = event.target.tab; |
86 sender.tab = null; | 87 sender.tab = null; |
87 } | 88 } |
88 | 89 |
89 listener(event.message, sender, function(message) { | 90 listener(event.message, sender, function(message) |
| 91 { |
90 dispatcher.dispatchMessage("response-" + event.name.substr(8), message
); | 92 dispatcher.dispatchMessage("response-" + event.name.substr(8), message
); |
91 }); | 93 }); |
92 }; | 94 }; |
93 } | 95 } |
94 }; | 96 }; |
95 | 97 |
96 | 98 |
97 /* Message passing */ | 99 /* Message passing */ |
98 | 100 |
99 var requestCounter = 0; | 101 var requestCounter = 0; |
100 | 102 |
101 sendMessage = function(message, responseCallback) | 103 sendMessage = function(message, responseCallback) |
102 { | 104 { |
103 var requestId = ++requestCounter; | 105 var requestId = ++requestCounter; |
104 | 106 |
105 if (responseCallback) { | 107 if (responseCallback) |
| 108 { |
106 var eventTarget = this._eventTarget; | 109 var eventTarget = this._eventTarget; |
107 var responseListener = function(event) | 110 var responseListener = function(event) |
108 { | 111 { |
109 if (event.name == "response-" + requestId) | 112 if (event.name == "response-" + requestId) |
110 { | 113 { |
111 eventTarget.removeEventListener("message", responseListener, false); | 114 eventTarget.removeEventListener("message", responseListener, false); |
112 responseCallback(event.message); | 115 responseCallback(event.message); |
113 } | 116 } |
114 }; | 117 }; |
115 eventTarget.addEventListener("message", responseListener, false); | 118 eventTarget.addEventListener("message", responseListener, false); |
116 } | 119 } |
117 | 120 |
118 this._messageDispatcher.dispatchMessage("request-" + requestId, message); | 121 this._messageDispatcher.dispatchMessage("request-" + requestId, message); |
119 }; | 122 }; |
120 | 123 |
121 | 124 |
122 /* I18n */ | 125 /* I18n */ |
123 | 126 |
124 var I18n = function() | 127 var I18n = function() |
125 { | 128 { |
126 this._localeCandidates = this._getLocaleCandidates(); | 129 this._localeCandidates = this._getLocaleCandidates(); |
127 this._uiLocale = this._localeCandidates[0]; | 130 this._uiLocale = this._localeCandidates[0]; |
128 }; | 131 }; |
129 I18n.prototype = { | 132 I18n.prototype = { |
130 _getLocaleCandidates: function() | 133 _getLocaleCandidates: function() |
131 { | 134 { |
132 var bits, i, locale; | |
133 var candidates = []; | 135 var candidates = []; |
134 var default_locale = "en_US"; | 136 var defaultLocale = "en_US"; |
135 | 137 |
| 138 var bits, i; |
136 for (i = (bits = navigator.language.split("-")).length; i > 0; i--) | 139 for (i = (bits = navigator.language.split("-")).length; i > 0; i--) |
137 { | 140 { |
138 locale = bits.slice(0, i).join("_"); | 141 var locale = bits.slice(0, i).join("_"); |
139 candidates.push(locale); | 142 candidates.push(locale); |
140 | 143 |
141 if (locale == default_locale) | 144 if (locale == defaultLocale) |
142 return candidates; | 145 return candidates; |
143 } | 146 } |
144 | 147 |
145 candidates.push(default_locale); | 148 candidates.push(defaultLocale); |
146 return candidates; | 149 return candidates; |
147 }, | 150 }, |
148 _getCatalog: function(locale) | 151 _getCatalog: function(locale) |
149 { | 152 { |
150 var xhr = new XMLHttpRequest(); | 153 var xhr = new XMLHttpRequest(); |
151 | 154 |
152 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messag
es.json", false); | 155 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messag
es.json", false); |
153 | 156 |
154 try { | 157 try { |
155 xhr.send(); | 158 xhr.send(); |
156 } | 159 } |
157 catch (e) | 160 catch (e) |
158 { | 161 { |
159 return null; | 162 return null; |
160 } | 163 } |
161 | 164 |
162 return JSON.parse(xhr.responseText); | 165 return JSON.parse(xhr.responseText); |
163 }, | 166 }, |
164 getMessage: function(msgId, substitutions) | 167 getMessage: function(msgId, substitutions) |
165 { | 168 { |
166 if (msgId == "@@ui_locale") | 169 if (msgId == "@@ui_locale") |
167 return this._uiLocale; | 170 return this._uiLocale; |
168 | 171 |
169 for (var i = 0; i < this._localeCandidates.length; i++) | 172 for (var i = 0; i < this._localeCandidates.length; i++) |
170 { | 173 { |
171 var catalog = this._getCatalog(this._localeCandidates[i]); | 174 var catalog = this._getCatalog(this._localeCandidates[i]); |
172 if (!catalog) | 175 if (!catalog) |
173 { | 176 { |
174 // if there is no catalog for this locale | 177 // if there is no catalog for this locale |
175 // candidate, dont"t try to load it again | 178 // candidate, don't try to load it again |
176 this._localeCandidates.splice(i--, 1); | 179 this._localeCandidates.splice(i--, 1); |
177 continue; | 180 continue; |
178 } | 181 } |
179 | 182 |
180 var msg = catalog[msgId]; | 183 var msg = catalog[msgId]; |
181 if (!msg) | 184 if (!msg) |
182 continue; | 185 continue; |
183 | 186 |
184 var msgstr = msg.message; | 187 var msgstr = msg.message; |
185 if (!msgstr) | 188 if (!msgstr) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 /* API */ | 220 /* API */ |
218 | 221 |
219 ext = { | 222 ext = { |
220 getURL: function(path) | 223 getURL: function(path) |
221 { | 224 { |
222 return safari.extension.baseURI + path; | 225 return safari.extension.baseURI + path; |
223 }, | 226 }, |
224 i18n: new I18n() | 227 i18n: new I18n() |
225 }; | 228 }; |
226 })(); | 229 })(); |
LEFT | RIGHT |