| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (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) { | |
|
Felix Dahlke
2013/11/10 01:07:00
Opening brace should go on its own line.
Felix Dahlke
2013/11/12 09:50:24
This hasn't been addressed.
| |
| 72 if (event.name.indexOf("request-") != 0) | |
| 73 return; | |
| 74 | |
| 75 var sender = {}; | |
| 76 var dispatcher; | |
| 77 | |
| 78 if (event.target instanceof SafariBrowserTab) | |
| 79 { | |
| 80 dispatcher = event.target.page; | |
| 81 sender.tab = new Tab(event.target); | |
| 82 } | |
| 83 else | |
| 84 { | |
| 85 dispatcher = event.target.tab; | |
| 86 sender.tab = null; | |
| 87 } | |
| 88 | |
| 89 listener(event.message, sender, function(message) { | |
|
Felix Dahlke
2013/11/10 01:07:00
Opening brace should go on its own line.
Felix Dahlke
2013/11/12 09:50:24
This hasn't been addressed.
| |
| 90 dispatcher.dispatchMessage("response-" + event.name.substr(8), message ); | |
| 91 }); | |
| 92 }; | |
| 93 } | |
| 94 }; | |
| 95 | |
| 96 | |
| 97 /* Message passing */ | |
| 98 | |
| 99 var requestCounter = 0; | |
| 100 | |
| 101 sendMessage = function(message, responseCallback) | |
| 102 { | |
| 103 var requestId = ++requestCounter; | |
| 104 | |
| 105 if (responseCallback) { | |
|
Felix Dahlke
2013/11/10 01:07:00
Opening brace should go on its own line.
Felix Dahlke
2013/11/12 09:50:24
This hasn't been addressed.
| |
| 106 var eventTarget = this._eventTarget; | |
| 107 var responseListener = function(event) | |
| 108 { | |
| 109 if (event.name == "response-" + requestId) | |
| 110 { | |
| 111 eventTarget.removeEventListener("message", responseListener, false); | |
| 112 responseCallback(event.message); | |
| 113 } | |
| 114 }; | |
| 115 eventTarget.addEventListener("message", responseListener, false); | |
| 116 } | |
| 117 | |
| 118 this._messageDispatcher.dispatchMessage("request-" + requestId, message); | |
| 119 }; | |
| 120 | |
| 121 | |
| 122 /* I18n */ | |
| 123 | |
| 124 var I18n = function() | |
| 125 { | |
| 126 this._localeCandidates = this._getLocaleCandidates(); | |
| 127 this._uiLocale = this._localeCandidates[0]; | |
| 128 }; | |
| 129 I18n.prototype = { | |
| 130 _getLocaleCandidates: function() | |
| 131 { | |
| 132 var bits, i, locale; | |
|
Felix Dahlke
2013/11/10 01:07:00
We typically declare variables where they are firs
| |
| 133 var candidates = []; | |
| 134 var default_locale = "en_US"; | |
|
Felix Dahlke
2013/11/10 01:07:00
camelCase please :)
| |
| 135 | |
| 136 for (i = (bits = navigator.language.split("-")).length; i > 0; i--) | |
| 137 { | |
| 138 locale = bits.slice(0, i).join("_"); | |
| 139 candidates.push(locale); | |
| 140 | |
| 141 if (locale == default_locale) | |
| 142 return candidates; | |
| 143 } | |
| 144 | |
| 145 candidates.push(default_locale); | |
| 146 return candidates; | |
| 147 }, | |
| 148 _getCatalog: function(locale) | |
| 149 { | |
| 150 var xhr = new XMLHttpRequest(); | |
| 151 | |
| 152 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messag es.json", false); | |
| 153 | |
| 154 try { | |
| 155 xhr.send(); | |
| 156 } | |
| 157 catch (e) | |
| 158 { | |
| 159 return null; | |
| 160 } | |
| 161 | |
| 162 return JSON.parse(xhr.responseText); | |
| 163 }, | |
| 164 getMessage: function(msgId, substitutions) | |
| 165 { | |
| 166 if (msgId == "@@ui_locale") | |
| 167 return this._uiLocale; | |
| 168 | |
| 169 for (var i = 0; i < this._localeCandidates.length; i++) | |
| 170 { | |
| 171 var catalog = this._getCatalog(this._localeCandidates[i]); | |
| 172 if (!catalog) | |
| 173 { | |
| 174 // if there is no catalog for this locale | |
| 175 // candidate, dont"t try to load it again | |
|
Felix Dahlke
2013/11/10 01:07:00
Typo: s/dont"t/don't/
| |
| 176 this._localeCandidates.splice(i--, 1); | |
| 177 continue; | |
| 178 } | |
| 179 | |
| 180 var msg = catalog[msgId]; | |
| 181 if (!msg) | |
| 182 continue; | |
| 183 | |
| 184 var msgstr = msg.message; | |
| 185 if (!msgstr) | |
| 186 continue; | |
| 187 | |
| 188 for (var placeholder in msg.placeholders) | |
| 189 { | |
| 190 var placeholderDetails = msg.placeholders[placeholder]; | |
| 191 if (!placeholderDetails || !placeholderDetails.content) | |
| 192 continue; | |
| 193 if (placeholderDetails.content.indexOf("$") != 0) | |
| 194 continue; | |
| 195 | |
| 196 var placeholderIdx = parseInt(placeholderDetails.content.substr(1)); | |
| 197 if (isNaN(placeholderIdx) || placeholderIdx < 1) | |
| 198 continue; | |
| 199 | |
| 200 var placeholderValue; | |
| 201 if (Object.prototype.toString.call(substitutions) == "[object Array]") | |
| 202 placeholderValue = substitutions[placeholderIdx - 1]; | |
| 203 else if (placeholderIdx == 1) | |
| 204 placeholderValue = substitutions; | |
| 205 | |
| 206 msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || " "); | |
| 207 } | |
| 208 | |
| 209 return msgstr; | |
| 210 } | |
| 211 | |
| 212 return ""; | |
| 213 } | |
| 214 }; | |
| 215 | |
| 216 | |
| 217 /* API */ | |
| 218 | |
| 219 ext = { | |
| 220 getURL: function(path) | |
| 221 { | |
| 222 return safari.extension.baseURI + path; | |
| 223 }, | |
| 224 i18n: new I18n() | |
| 225 }; | |
| 226 })(); | |
| OLD | NEW |