Index: ext/common.js |
=================================================================== |
--- a/ext/common.js |
+++ b/ext/common.js |
@@ -12,16 +12,62 @@ |
* GNU General Public License for more details. |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
(function(global) |
{ |
+ if (!global.ext) |
+ global.ext = {}; |
+ |
+ /* Message passing */ |
+ |
+ global.ext.onMessage = |
+ { |
+ addListener: function(listener) |
+ { |
+ listener.__wrapper = function(event) |
+ { |
+ if (event.data.type != "message") |
+ return; |
+ |
+ var message = event.data.payload; |
+ var messageId = event.data.messageId; |
+ var sender = { |
+ sendMessage: function(message) |
+ { |
+ event.source.postMessage({ |
+ type: "message", |
+ messageId: -1, |
+ payload: message |
+ }, "*"); |
+ } |
+ }; |
+ var callback = function(message) |
+ { |
+ event.source.postMessage({ |
+ type: "response", |
+ messageId: messageId, |
+ payload: message |
+ }, "*"); |
+ }; |
+ listener(message, sender, callback); |
+ }; |
+ window.addEventListener("message", listener.__wrapper, false); |
+ }, |
+ |
+ removeListener: function(listener) |
+ { |
+ if ("__wrapper" in listener) |
+ window.removeEventListener("message", listener.__wrapper, false); |
+ } |
+ }; |
+ |
/* I18n */ |
var getLocaleCandidates = function(selectedLocale) |
{ |
var candidates = []; |
var defaultLocale = "en-US"; |
// e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second |
@@ -105,19 +151,16 @@ |
var rawCatalog = JSON.parse(xhr.responseText); |
for (var msgId in rawCatalog) |
{ |
if (!(msgId in catalog)) |
catalog[msgId] = parseMessage(rawCatalog[msgId]); |
} |
}; |
- if (!global.ext) |
- global.ext = {}; |
- |
global.ext.i18n = { |
getMessage: function(msgId, substitutions) |
{ |
while (true) |
{ |
var message = catalog[msgId]; |
if (message) |
{ |