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