| OLD | NEW |
| 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-2014 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 (function() { | 18 (function() |
| 19 /* Events */ | 19 { |
| 20 /* Message passing */ |
| 20 | 21 |
| 21 WrappedEventTarget = function(target, eventName, capture) | 22 var MessageProxy = ext._MessageProxy = function(messageDispatcher) |
| 22 { | 23 { |
| 23 this._listeners = []; | 24 this._messageDispatcher = messageDispatcher; |
| 24 this._wrappedListeners = []; | 25 this._responseCallbacks = {__proto__: null}; |
| 26 this._responseCallbackCounter = 0; |
| 27 }; |
| 28 MessageProxy.prototype = { |
| 29 _sendResponse: function(request, message) |
| 30 { |
| 31 var response = {}; |
| 32 for (var prop in request) |
| 33 response[prop] = request[prop]; |
| 34 response.payload = message; |
| 25 | 35 |
| 26 this._target = target; | 36 this._messageDispatcher.dispatchMessage("response", response); |
| 27 this._eventName = eventName; | 37 }, |
| 28 this._capture = capture; | 38 handleRequest: function(request, sender) |
| 29 }; | |
| 30 WrappedEventTarget.prototype = { | |
| 31 addListener: function(listener) | |
| 32 { | 39 { |
| 33 var wrappedListener = this._wrapListener(listener); | 40 var sendResponse; |
| 41 if ("callbackId" in request) |
| 42 sendResponse = this._sendResponse.bind(this, request); |
| 43 else |
| 44 sendResponse = function() {}; |
| 34 | 45 |
| 35 this._listeners.push(listener); | 46 ext.onMessage._dispatch(request.payload, sender, sendResponse); |
| 36 this._wrappedListeners.push(wrappedListener); | 47 }, |
| 48 handleResponse: function(response) |
| 49 { |
| 50 var callbackId = response.callbackId; |
| 51 var callback = this._responseCallbacks[callbackId]; |
| 52 if (callback) |
| 53 { |
| 54 delete this._responseCallbacks[callbackId]; |
| 55 callback(response.payload); |
| 56 } |
| 57 }, |
| 58 sendMessage: function(message, responseCallback, extra) |
| 59 { |
| 60 var request = {payload: message}; |
| 37 | 61 |
| 38 this._target.addEventListener( | 62 if (responseCallback) |
| 39 this._eventName, | 63 { |
| 40 wrappedListener, | 64 request.callbackId = ++this._responseCallbackCounter; |
| 41 this._capture | 65 this._responseCallbacks[request.callbackId] = responseCallback; |
| 42 ); | 66 } |
| 43 }, | |
| 44 removeListener: function(listener) | |
| 45 { | |
| 46 var idx = this._listeners.indexOf(listener); | |
| 47 | 67 |
| 48 if (idx != -1) | 68 for (var prop in extra) |
| 49 { | 69 request[prop] = extra[prop]; |
| 50 this._target.removeEventListener( | |
| 51 this._eventName, | |
| 52 this._wrappedListeners[idx], | |
| 53 this._capture | |
| 54 ); | |
| 55 | 70 |
| 56 this._listeners.splice(idx, 1); | 71 this._messageDispatcher.dispatchMessage("request", request); |
| 57 this._wrappedListeners.splice(idx, 1); | |
| 58 } | |
| 59 } | 72 } |
| 60 }; | 73 }; |
| 61 | 74 |
| 62 | 75 ext.onMessage = new ext._EventTarget(); |
| 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 ("SafariBrowserTab" in window && event.target instanceof SafariBrowse
rTab) | |
| 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 | 76 |
| 124 | 77 |
| 125 /* I18n */ | 78 /* I18n */ |
| 126 | 79 |
| 127 var I18n = function() | 80 var localeCandidates = null; |
| 81 var uiLocale; |
| 82 |
| 83 var getLocaleCandidates = function() |
| 128 { | 84 { |
| 129 this._localeCandidates = this._getLocaleCandidates(); | 85 var candidates = []; |
| 130 this._uiLocale = this._localeCandidates[0]; | 86 var defaultLocale = "en_US"; |
| 87 |
| 88 // e.g. "ja-jp-mac" -> "ja", "jp", note that the part after the second |
| 89 // dash is dropped, since we only support language and region |
| 90 var [language, region] = navigator.language.split("-"); |
| 91 |
| 92 if (region) |
| 93 candidates.push(language + "_" + region.toUpperCase()); |
| 94 |
| 95 candidates.push(language); |
| 96 |
| 97 if (candidates.indexOf(defaultLocale) == -1) |
| 98 candidates.push(defaultLocale); |
| 99 |
| 100 return candidates; |
| 131 }; | 101 }; |
| 132 I18n.prototype = { | 102 |
| 133 _getLocaleCandidates: function() | 103 var getCatalog = function(locale) |
| 104 { |
| 105 var xhr = new XMLHttpRequest(); |
| 106 |
| 107 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages
.json", false); |
| 108 |
| 109 try { |
| 110 xhr.send(); |
| 111 } |
| 112 catch (e) |
| 134 { | 113 { |
| 135 var candidates = []; | 114 return null; |
| 136 var defaultLocale = "en_US"; | 115 } |
| 137 | 116 |
| 138 var bits, i; | 117 if (xhr.status != 200 && xhr.status != 0) |
| 139 for (i = (bits = navigator.language.split("-")).length; i > 0; i--) | 118 return null; |
| 119 |
| 120 return JSON.parse(xhr.responseText); |
| 121 }; |
| 122 |
| 123 ext.i18n = { |
| 124 getMessage: function(msgId, substitutions) |
| 125 { |
| 126 if (!localeCandidates) |
| 140 { | 127 { |
| 141 var locale = bits.slice(0, i).join("_"); | 128 localeCandidates = getLocaleCandidates(); |
| 142 candidates.push(locale); | 129 uiLocale = localeCandidates[0]; |
| 143 | |
| 144 if (locale == defaultLocale) | |
| 145 return candidates; | |
| 146 } | 130 } |
| 147 | 131 |
| 148 candidates.push(defaultLocale); | 132 if (msgId == "@@ui_locale") |
| 149 return candidates; | 133 return uiLocale; |
| 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 | 134 |
| 157 try { | 135 for (var i = 0; i < localeCandidates.length; i++) |
| 158 xhr.send(); | |
| 159 } | |
| 160 catch (e) | |
| 161 { | 136 { |
| 162 return null; | 137 var catalog = getCatalog(localeCandidates[i]); |
| 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) | 138 if (!catalog) |
| 176 { | 139 { |
| 177 // if there is no catalog for this locale | 140 // if there is no catalog for this locale |
| 178 // candidate, don't try to load it again | 141 // candidate, don't try to load it again |
| 179 this._localeCandidates.splice(i--, 1); | 142 localeCandidates.splice(i--, 1); |
| 180 continue; | 143 continue; |
| 181 } | 144 } |
| 182 | 145 |
| 183 var msg = catalog[msgId]; | 146 var msg = catalog[msgId]; |
| 184 if (!msg) | 147 if (!msg) |
| 185 continue; | 148 continue; |
| 186 | 149 |
| 187 var msgstr = msg.message; | 150 var msgstr = msg.message; |
| 188 if (!msgstr) | 151 if (!msgstr) |
| 189 continue; | 152 continue; |
| 190 | 153 |
| 191 for (var placeholder in msg.placeholders) | 154 for (var placeholder in msg.placeholders) |
| 192 { | 155 { |
| 193 var placeholderDetails = msg.placeholders[placeholder]; | 156 var placeholderDetails = msg.placeholders[placeholder]; |
| 194 if (!placeholderDetails || !placeholderDetails.content) | 157 if (!placeholderDetails || !placeholderDetails.content) |
| 195 continue; | 158 continue; |
| 196 if (placeholderDetails.content.indexOf("$") != 0) | 159 if (placeholderDetails.content.indexOf("$") != 0) |
| 197 continue; | 160 continue; |
| 198 | 161 |
| 199 var placeholderIdx = parseInt(placeholderDetails.content.substr(1)); | 162 var placeholderIdx = parseInt(placeholderDetails.content.substr(1)); |
| 200 if (isNaN(placeholderIdx) || placeholderIdx < 1) | 163 if (isNaN(placeholderIdx) || placeholderIdx < 1) |
| 201 continue; | 164 continue; |
| 202 | 165 |
| 203 var placeholderValue; | 166 var placeholderValue; |
| 204 if (Object.prototype.toString.call(substitutions) == "[object Array]") | 167 if (typeof substitutions != "string") |
| 205 placeholderValue = substitutions[placeholderIdx - 1]; | 168 placeholderValue = substitutions[placeholderIdx - 1]; |
| 206 else if (placeholderIdx == 1) | 169 else if (placeholderIdx == 1) |
| 207 placeholderValue = substitutions; | 170 placeholderValue = substitutions; |
| 208 | 171 |
| 209 msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || "
"); | 172 msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || "
"); |
| 210 } | 173 } |
| 211 | 174 |
| 212 return msgstr; | 175 return msgstr; |
| 213 } | 176 } |
| 214 | 177 |
| 215 return ""; | 178 return ""; |
| 216 } | 179 } |
| 217 }; | 180 }; |
| 218 | 181 |
| 219 | 182 |
| 220 /* API */ | 183 /* Utils */ |
| 221 | 184 |
| 222 ext = { | 185 ext.getURL = function(path) |
| 223 getURL: function(path) | 186 { |
| 224 { | 187 return safari.extension.baseURI + path; |
| 225 return safari.extension.baseURI + path; | |
| 226 }, | |
| 227 i18n: new I18n() | |
| 228 }; | 188 }; |
| 229 })(); | 189 })(); |
| OLD | NEW |