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