 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: | 
| 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-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) | 20 if (!global.ext) | 
| 21 global.ext = {}; | 21 global.ext = {}; | 
| 22 | 22 | 
| 23 function Page(source) | 23 function Page(source) | 
| 24 { | 24 { | 
| 25 this._source = source; | 25 this._source = source; | 
| 26 } | 26 } | 
| 27 Page.prototype = | 27 Page.prototype = | 
| 28 { | 28 { | 
| 29 equals: function(page) | |
| 30 { | |
| 31 return page._source == this._source; | |
| 32 }, | |
| 33 | |
| 34 sendMessage: function(message) | 29 sendMessage: function(message) | 
| 35 { | 30 { | 
| 36 this._source.postMessage({ | 31 this._source.postMessage({ | 
| 37 type: "message", | 32 type: "message", | 
| 38 messageId: -1, | 33 messageId: -1, | 
| 39 payload: message | 34 payload: message | 
| 40 }, "*"); | 35 }, "*"); | 
| 41 } | 36 } | 
| 42 } | 37 }; | 
| 38 | |
| 39 global.ext.Page = Page; | |
| 43 | 40 | 
| 44 /* Message passing */ | 41 /* Message passing */ | 
| 45 | 42 | 
| 46 global.ext.onMessage = | 43 global.ext.onMessage = | 
| 47 { | 44 { | 
| 48 addListener: function(listener) | 45 addListener: function(listener) | 
| 49 { | 46 { | 
| 50 listener.__wrapper = function(event) | 47 listener._extWrapper = 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 { | 48 { | 
| 52 if (event.data.type != "message") | 49 if (event.data.type != "message") | 
| 53 return; | 50 return; | 
| 54 | 51 | 
| 55 var message = event.data.payload; | 52 var message = event.data.payload; | 
| 56 var messageId = event.data.messageId; | 53 var messageId = event.data.messageId; | 
| 57 var sender = { | 54 var sender = { | 
| 58 page: new Page(event.source) | 55 page: new Page(event.source) | 
| 59 }; | 56 }; | 
| 60 var callback = function(message) | 57 var callback = function(message) | 
| 61 { | 58 { | 
| 62 event.source.postMessage({ | 59 event.source.postMessage({ | 
| 63 type: "response", | 60 type: "response", | 
| 64 messageId: messageId, | 61 messageId: messageId, | 
| 65 payload: message | 62 payload: message | 
| 66 }, "*"); | 63 }, "*"); | 
| 67 }; | 64 }; | 
| 68 listener(message, sender, callback); | 65 listener(message, sender, callback); | 
| 69 }; | 66 }; | 
| 70 window.addEventListener("message", listener.__wrapper, false); | 67 window.addEventListener("message", listener._extWrapper, false); | 
| 71 }, | 68 }, | 
| 72 | 69 | 
| 73 removeListener: function(listener) | 70 removeListener: function(listener) | 
| 74 { | 71 { | 
| 75 if ("__wrapper" in listener) | 72 if ("_extWrapper" in listener) | 
| 76 window.removeEventListener("message", listener.__wrapper, false); | 73 window.removeEventListener("message", listener._extWrapper, false); | 
| 77 } | 74 } | 
| 78 }; | 75 }; | 
| 79 | 76 | 
| 80 /* I18n */ | 77 /* I18n */ | 
| 81 | 78 | 
| 82 var getLocaleCandidates = function(selectedLocale) | 79 var getLocaleCandidates = function(selectedLocale) | 
| 83 { | 80 { | 
| 84 var candidates = []; | 81 var candidates = []; | 
| 85 var defaultLocale = "en-US"; | 82 var defaultLocale = "en-US"; | 
| 86 | 83 | 
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 } | 188 } | 
| 192 | 189 | 
| 193 if (locales.length == 0) | 190 if (locales.length == 0) | 
| 194 return ""; | 191 return ""; | 
| 195 | 192 | 
| 196 readCatalog(locales.shift()); | 193 readCatalog(locales.shift()); | 
| 197 } | 194 } | 
| 198 } | 195 } | 
| 199 }; | 196 }; | 
| 200 })(this); | 197 })(this); | 
| LEFT | RIGHT |