Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: ext/common.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Using Services.vc Created Dec. 19, 2014, 5:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 sendMessage: function(message)
30 {
31 this._source.postMessage({
32 type: "message",
33 messageId: -1,
34 payload: message
35 }, "*");
36 }
37 };
38
39 global.ext.Page = Page;
40
41 /* Message passing */
42
43 global.ext.onMessage =
44 {
45 addListener: function(listener)
46 {
47 listener._extWrapper = function(event)
48 {
49 if (event.data.type != "message")
50 return;
51
52 var message = event.data.payload;
53 var messageId = event.data.messageId;
54 var sender = {
55 page: new Page(event.source)
56 };
57 var callback = function(message)
58 {
59 event.source.postMessage({
60 type: "response",
61 messageId: messageId,
62 payload: message
63 }, "*");
64 };
65 listener(message, sender, callback);
66 };
67 window.addEventListener("message", listener._extWrapper, false);
68 },
69
70 removeListener: function(listener)
71 {
72 if ("_extWrapper" in listener)
73 window.removeEventListener("message", listener._extWrapper, false);
74 }
75 };
76
20 /* I18n */ 77 /* I18n */
21 78
22 var getLocaleCandidates = function(selectedLocale) 79 var getLocaleCandidates = function(selectedLocale)
23 { 80 {
24 var candidates = []; 81 var candidates = [];
25 var defaultLocale = "en-US"; 82 var defaultLocale = "en-US";
26 83
27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second 84 // 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 85 // dash is dropped, since we only support language and region
29 var parts = selectedLocale.split("-"); 86 var parts = selectedLocale.split("-");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 return; 160 return;
104 161
105 var rawCatalog = JSON.parse(xhr.responseText); 162 var rawCatalog = JSON.parse(xhr.responseText);
106 for (var msgId in rawCatalog) 163 for (var msgId in rawCatalog)
107 { 164 {
108 if (!(msgId in catalog)) 165 if (!(msgId in catalog))
109 catalog[msgId] = parseMessage(rawCatalog[msgId]); 166 catalog[msgId] = parseMessage(rawCatalog[msgId]);
110 } 167 }
111 }; 168 };
112 169
113 if (!global.ext)
114 global.ext = {};
115
116 global.ext.i18n = { 170 global.ext.i18n = {
117 getMessage: function(msgId, substitutions) 171 getMessage: function(msgId, substitutions)
118 { 172 {
119 while (true) 173 while (true)
120 { 174 {
121 var message = catalog[msgId]; 175 var message = catalog[msgId];
122 if (message) 176 if (message)
123 { 177 {
124 var text = message[0]; 178 var text = message[0];
125 var placeholders = message[1]; 179 var placeholders = message[1];
126 180
127 if (!(substitutions instanceof Array)) 181 if (!(substitutions instanceof Array))
128 substitutions = [substitutions]; 182 substitutions = [substitutions];
129 183
130 for (var i = 0; i < placeholders.length; i++) 184 for (var i = 0; i < placeholders.length; i++)
131 text = replacePlaceholder(text, placeholders[i], substitutions[i]); 185 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
132 186
133 return text; 187 return text;
134 } 188 }
135 189
136 if (locales.length == 0) 190 if (locales.length == 0)
137 return ""; 191 return "";
138 192
139 readCatalog(locales.shift()); 193 readCatalog(locales.shift());
140 } 194 }
141 } 195 }
142 }; 196 };
143 })(this); 197 })(this);
OLDNEW
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | messageResponder.js » ('J')

Powered by Google App Engine
This is Rietveld