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

Side by Side Diff: ext/common.js

Issue 29375899: Issue 4871 - Start using ESLint for adblockplusui (Closed)
Patch Set: Avoid violating operator-linebreak rule Created March 15, 2017, 4:43 a.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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 "use strict";
19
20 (function()
19 { 21 {
20 if (!global.ext) 22 if (typeof ext == "undefined")
21 global.ext = {}; 23 window.ext = {};
22 24
23 function Page(source) 25 function Page(source)
24 { 26 {
25 this._source = source; 27 this._source = source;
26 } 28 }
27 Page.prototype = 29 Page.prototype =
28 { 30 {
29 sendMessage: function(message) 31 sendMessage(message)
30 { 32 {
31 this._source.postMessage({ 33 this._source.postMessage({
32 type: "message", 34 type: "message",
33 messageId: -1, 35 messageId: -1,
34 payload: message 36 payload: message
35 }, "*"); 37 }, "*");
36 } 38 }
37 }; 39 };
38 40
39 global.ext.Page = Page; 41 window.ext.Page = Page;
40 42
41 /* Message passing */ 43 /* Message passing */
42 44
43 global.ext.onMessage = 45 window.ext.onMessage =
44 { 46 {
45 addListener: function(listener) 47 addListener(listener)
46 { 48 {
47 listener._extWrapper = function(event) 49 listener._extWrapper = function(event)
48 { 50 {
49 if (event.data.type != "message") 51 if (event.data.type != "message")
50 return; 52 return;
51 53
52 var message = event.data.payload; 54 let {messageId} = event.data;
53 var messageId = event.data.messageId; 55 let sender = {
54 var sender = {
55 page: new Page(event.source) 56 page: new Page(event.source)
56 }; 57 };
57 var callback = function(message) 58 let callback = function(message)
58 { 59 {
59 event.source.postMessage({ 60 event.source.postMessage({
60 type: "response", 61 type: "response",
61 messageId: messageId, 62 messageId,
62 payload: message 63 payload: message
63 }, "*"); 64 }, "*");
64 }; 65 };
65 listener(message, sender, callback); 66 listener(event.data.payload, sender, callback);
66 }; 67 };
67 window.addEventListener("message", listener._extWrapper, false); 68 window.addEventListener("message", listener._extWrapper, false);
68 }, 69 },
69 70
70 removeListener: function(listener) 71 removeListener(listener)
71 { 72 {
72 if ("_extWrapper" in listener) 73 if ("_extWrapper" in listener)
73 window.removeEventListener("message", listener._extWrapper, false); 74 window.removeEventListener("message", listener._extWrapper, false);
74 } 75 }
75 }; 76 };
76 77
77 /* I18n */ 78 /* I18n */
78 79
79 var getLocaleCandidates = function(selectedLocale) 80 let getLocaleCandidates = function(selectedLocale)
80 { 81 {
81 var candidates = []; 82 let candidates = [];
82 var defaultLocale = "en-US"; 83 let defaultLocale = "en-US";
83 84
84 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second 85 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second
85 // dash is dropped, since we only support language and region 86 // dash is dropped, since we only support language and region
86 var parts = selectedLocale.split("-"); 87 let parts = selectedLocale.split("-");
87 var language = parts[0]; 88 let language = parts[0];
88 var region = (parts[1] || "").toUpperCase(); 89 let region = (parts[1] || "").toUpperCase();
89 90
90 if (region) 91 if (region)
91 candidates.push(language + "-" + region); 92 candidates.push(language + "-" + region);
92 93
93 candidates.push(language); 94 candidates.push(language);
94 95
95 if (candidates.indexOf(defaultLocale) == -1) 96 if (candidates.indexOf(defaultLocale) == -1)
96 candidates.push(defaultLocale); 97 candidates.push(defaultLocale);
97 98
98 return candidates; 99 return candidates;
99 }; 100 };
100 101
101 var initCatalog = function(uiLocale) 102 let initCatalog = function(uiLocale)
102 { 103 {
103 var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr"; 104 let bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr";
104 var catalog = Object.create(null); 105 let catalog = Object.create(null);
105 106
106 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []]; 107 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []];
107 catalog["@@bidi_dir" ] = [bidiDir, []]; 108 catalog["@@bidi_dir"] = [bidiDir, []];
108 109
109 return catalog; 110 return catalog;
110 }; 111 };
111 112
112 var selectedLocale = window.navigator.language; 113 let selectedLocale = window.navigator.language;
113 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); 114 let match = /[?&]locale=([\w-]+)/.exec(window.location.search);
114 if (match) 115 if (match)
115 selectedLocale = match[1]; 116 selectedLocale = match[1];
116 117
117 var locales = getLocaleCandidates(selectedLocale); 118 let locales = getLocaleCandidates(selectedLocale);
118 var catalog = initCatalog(locales[0]); 119 let catalog = initCatalog(locales[0]);
119 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json"; 120 let catalogFile = window.location.pathname.replace(/.*\//, "")
121 .replace(/\..*/, "") + ".json";
120 122
121 var replacePlaceholder = function(text, placeholder, content) 123 let replacePlaceholder = function(text, placeholder, content)
122 { 124 {
123 return text.split("$" + placeholder + "$").join(content || ""); 125 return text.split("$" + placeholder + "$").join(content || "");
124 }; 126 };
125 127
126 var parseMessage = function(rawMessage) 128 let parseMessage = function(rawMessage)
127 { 129 {
128 var text = rawMessage.message; 130 let text = rawMessage.message;
129 var placeholders = []; 131 let placeholders = [];
130 132
131 for (var placeholder in rawMessage.placeholders) 133 for (let placeholder in rawMessage.placeholders)
132 { 134 {
133 var content = rawMessage.placeholders[placeholder].content; 135 let {content} = rawMessage.placeholders[placeholder];
134 136
135 if (/^\$\d+$/.test(content)) 137 if (/^\$\d+$/.test(content))
136 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; 138 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
137 else 139 else
138 text = replacePlaceholder(text, placeholder, content); 140 text = replacePlaceholder(text, placeholder, content);
139 } 141 }
140 142
141 return [text, placeholders]; 143 return [text, placeholders];
142 }; 144 };
143 145
144 var readCatalog = function(locale, catalogFile) 146 let readCatalog = function(locale, file)
145 { 147 {
146 var xhr = new XMLHttpRequest(); 148 let xhr = new XMLHttpRequest();
147 xhr.open("GET", "locale/" + locale + "/" + catalogFile, false); 149 xhr.open("GET", "locale/" + locale + "/" + file, false);
148 xhr.overrideMimeType("text/plain"); 150 xhr.overrideMimeType("text/plain");
149 151
150 try 152 try
151 { 153 {
152 xhr.send(); 154 xhr.send();
153 } 155 }
154 catch (e) 156 catch (e)
155 { 157 {
156 return; 158 return;
157 } 159 }
158 160
159 if (xhr.status != 200 && xhr.status != 0) 161 if (xhr.status != 200 && xhr.status != 0)
160 return; 162 return;
161 163
162 var rawCatalog = JSON.parse(xhr.responseText); 164 let rawCatalog = JSON.parse(xhr.responseText);
163 for (var msgId in rawCatalog) 165 for (let msgId in rawCatalog)
164 { 166 {
165 if (!(msgId in catalog)) 167 if (!(msgId in catalog))
166 catalog[msgId] = parseMessage(rawCatalog[msgId]); 168 catalog[msgId] = parseMessage(rawCatalog[msgId]);
167 } 169 }
168 }; 170 };
169 171
170 global.ext.i18n = { 172 window.ext.i18n = {
171 getMessage: function(msgId, substitutions) 173 getMessage(msgId, substitutions)
172 { 174 {
173 while (true) 175 while (true)
174 { 176 {
175 var message = catalog[msgId]; 177 let message = catalog[msgId];
176 if (message) 178 if (message)
177 { 179 {
178 var text = message[0]; 180 let text = message[0];
179 var placeholders = message[1]; 181 let placeholders = message[1];
180 182
181 if (!(substitutions instanceof Array)) 183 if (!(substitutions instanceof Array))
182 substitutions = [substitutions]; 184 substitutions = [substitutions];
183 185
184 for (var i = 0; i < placeholders.length; i++) 186 for (let i = 0; i < placeholders.length; i++)
185 text = replacePlaceholder(text, placeholders[i], substitutions[i]); 187 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
186 188
187 return text; 189 return text;
188 } 190 }
189 191
190 if (locales.length == 0) 192 if (locales.length == 0)
191 return ""; 193 return "";
192 194
193 var locale = locales.shift(); 195 let locale = locales.shift();
194 readCatalog(locale, "common.json"); 196 readCatalog(locale, "common.json");
195 readCatalog(locale, catalogFile); 197 readCatalog(locale, catalogFile);
196 } 198 }
197 } 199 }
198 }; 200 };
199 })(this); 201 }());
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