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: Created Dec. 17, 2014, 6:32 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
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /* Message passing */
24
25 global.ext.onMessage =
26 {
27 addListener: function(listener)
28 {
29 listener.__wrapper = function(event)
30 {
31 if (event.data.type != "message")
32 return;
33
34 var message = event.data.payload;
35 var messageId = event.data.messageId;
36 var sender = {
37 sendMessage: function(message)
38 {
39 event.source.postMessage({
40 type: "message",
41 messageId: -1,
42 payload: message
43 }, "*");
44 }
45 };
46 var callback = function(message)
47 {
48 event.source.postMessage({
49 type: "response",
50 messageId: messageId,
51 payload: message
52 }, "*");
53 };
54 listener(message, sender, callback);
55 };
56 window.addEventListener("message", listener.__wrapper, false);
57 },
58
59 removeListener: function(listener)
60 {
61 if ("__wrapper" in listener)
62 window.removeEventListener("message", listener.__wrapper, false);
63 }
64 };
65
20 /* I18n */ 66 /* I18n */
21 67
22 var getLocaleCandidates = function(selectedLocale) 68 var getLocaleCandidates = function(selectedLocale)
23 { 69 {
24 var candidates = []; 70 var candidates = [];
25 var defaultLocale = "en-US"; 71 var defaultLocale = "en-US";
26 72
27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second 73 // 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 74 // dash is dropped, since we only support language and region
29 var parts = selectedLocale.split("-"); 75 var parts = selectedLocale.split("-");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 return; 149 return;
104 150
105 var rawCatalog = JSON.parse(xhr.responseText); 151 var rawCatalog = JSON.parse(xhr.responseText);
106 for (var msgId in rawCatalog) 152 for (var msgId in rawCatalog)
107 { 153 {
108 if (!(msgId in catalog)) 154 if (!(msgId in catalog))
109 catalog[msgId] = parseMessage(rawCatalog[msgId]); 155 catalog[msgId] = parseMessage(rawCatalog[msgId]);
110 } 156 }
111 }; 157 };
112 158
113 if (!global.ext)
114 global.ext = {};
115
116 global.ext.i18n = { 159 global.ext.i18n = {
117 getMessage: function(msgId, substitutions) 160 getMessage: function(msgId, substitutions)
118 { 161 {
119 while (true) 162 while (true)
120 { 163 {
121 var message = catalog[msgId]; 164 var message = catalog[msgId];
122 if (message) 165 if (message)
123 { 166 {
124 var text = message[0]; 167 var text = message[0];
125 var placeholders = message[1]; 168 var placeholders = message[1];
126 169
127 if (!(substitutions instanceof Array)) 170 if (!(substitutions instanceof Array))
128 substitutions = [substitutions]; 171 substitutions = [substitutions];
129 172
130 for (var i = 0; i < placeholders.length; i++) 173 for (var i = 0; i < placeholders.length; i++)
131 text = replacePlaceholder(text, placeholders[i], substitutions[i]); 174 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
132 175
133 return text; 176 return text;
134 } 177 }
135 178
136 if (locales.length == 0) 179 if (locales.length == 0)
137 return ""; 180 return "";
138 181
139 readCatalog(locales.shift()); 182 readCatalog(locales.shift());
140 } 183 }
141 } 184 }
142 }; 185 };
143 })(this); 186 })(this);
OLDNEW
« no previous file with comments | « ext/background.js ('k') | ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld