 Issue 4731979438227456:
  Issue 1663 - Emulate background page and implement proper message responder  (Closed)
    
  
    Issue 4731979438227456:
  Issue 1663 - Emulate background page and implement proper message responder  (Closed) 
  | Left: | ||
| Right: | 
| 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-2014 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(global) | 18 (function(global) | 
| 19 { | 19 { | 
| 20 if (!global.ext) | |
| 21 global.ext = {}; | |
| 22 | |
| 23 function Page(source) | |
| 24 { | |
| 25 this._source = source; | |
| 26 } | |
| 27 Page.prototype = | |
| 28 { | |
| 29 equals: function(page) | |
| 30 { | |
| 31 return page._source == this._source; | |
| 32 }, | |
| 33 | |
| 34 sendMessage: function(message) | |
| 35 { | |
| 36 this._source.postMessage({ | |
| 37 type: "message", | |
| 38 messageId: -1, | |
| 39 payload: message | |
| 40 }, "*"); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 /* Message passing */ | |
| 45 | |
| 46 global.ext.onMessage = | |
| 47 { | |
| 48 addListener: function(listener) | |
| 49 { | |
| 50 listener.__wrapper = function(event) | |
| 
Thomas Greiner
2014/12/18 17:32:06
Two underscores are easy to get wrong and detect w
 
Wladimir Palant
2014/12/18 22:04:44
Done.
 | |
| 51 { | |
| 52 if (event.data.type != "message") | |
| 53 return; | |
| 54 | |
| 55 var message = event.data.payload; | |
| 56 var messageId = event.data.messageId; | |
| 57 var sender = { | |
| 58 page: new Page(event.source) | |
| 59 }; | |
| 60 var callback = function(message) | |
| 61 { | |
| 62 event.source.postMessage({ | |
| 63 type: "response", | |
| 64 messageId: messageId, | |
| 65 payload: message | |
| 66 }, "*"); | |
| 67 }; | |
| 68 listener(message, sender, callback); | |
| 69 }; | |
| 70 window.addEventListener("message", listener.__wrapper, false); | |
| 71 }, | |
| 72 | |
| 73 removeListener: function(listener) | |
| 74 { | |
| 75 if ("__wrapper" in listener) | |
| 76 window.removeEventListener("message", listener.__wrapper, false); | |
| 77 } | |
| 78 }; | |
| 79 | |
| 20 /* I18n */ | 80 /* I18n */ | 
| 21 | 81 | 
| 22 var getLocaleCandidates = function(selectedLocale) | 82 var getLocaleCandidates = function(selectedLocale) | 
| 23 { | 83 { | 
| 24 var candidates = []; | 84 var candidates = []; | 
| 25 var defaultLocale = "en-US"; | 85 var defaultLocale = "en-US"; | 
| 26 | 86 | 
| 27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | 87 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | 
| 28 // dash is dropped, since we only support language and region | 88 // dash is dropped, since we only support language and region | 
| 29 var parts = selectedLocale.split("-"); | 89 var parts = selectedLocale.split("-"); | 
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 return; | 163 return; | 
| 104 | 164 | 
| 105 var rawCatalog = JSON.parse(xhr.responseText); | 165 var rawCatalog = JSON.parse(xhr.responseText); | 
| 106 for (var msgId in rawCatalog) | 166 for (var msgId in rawCatalog) | 
| 107 { | 167 { | 
| 108 if (!(msgId in catalog)) | 168 if (!(msgId in catalog)) | 
| 109 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 169 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 
| 110 } | 170 } | 
| 111 }; | 171 }; | 
| 112 | 172 | 
| 113 if (!global.ext) | |
| 114 global.ext = {}; | |
| 115 | |
| 116 global.ext.i18n = { | 173 global.ext.i18n = { | 
| 117 getMessage: function(msgId, substitutions) | 174 getMessage: function(msgId, substitutions) | 
| 118 { | 175 { | 
| 119 while (true) | 176 while (true) | 
| 120 { | 177 { | 
| 121 var message = catalog[msgId]; | 178 var message = catalog[msgId]; | 
| 122 if (message) | 179 if (message) | 
| 123 { | 180 { | 
| 124 var text = message[0]; | 181 var text = message[0]; | 
| 125 var placeholders = message[1]; | 182 var placeholders = message[1]; | 
| 126 | 183 | 
| 127 if (!(substitutions instanceof Array)) | 184 if (!(substitutions instanceof Array)) | 
| 128 substitutions = [substitutions]; | 185 substitutions = [substitutions]; | 
| 129 | 186 | 
| 130 for (var i = 0; i < placeholders.length; i++) | 187 for (var i = 0; i < placeholders.length; i++) | 
| 131 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 188 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 
| 132 | 189 | 
| 133 return text; | 190 return text; | 
| 134 } | 191 } | 
| 135 | 192 | 
| 136 if (locales.length == 0) | 193 if (locales.length == 0) | 
| 137 return ""; | 194 return ""; | 
| 138 | 195 | 
| 139 readCatalog(locales.shift()); | 196 readCatalog(locales.shift()); | 
| 140 } | 197 } | 
| 141 } | 198 } | 
| 142 }; | 199 }; | 
| 143 })(this); | 200 })(this); | 
| OLD | NEW |