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

Side by Side Diff: options.js

Issue 29333528: Issue 3515 - Use fetch() API instead XMLHttpRequest (Platform) (Closed)
Patch Set: Created Jan. 15, 2016, 1:57 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
« background.js ('K') | « options.html ('k') | no next file » | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 checkbox.checked = descriptor.toggle(); 180 checkbox.checked = descriptor.toggle();
181 181
182 Prefs[id] = checkbox.checked; 182 Prefs[id] = checkbox.checked;
183 }, false); 183 }, false);
184 } 184 }
185 185
186 var delayedSubscriptionSelection = null; 186 var delayedSubscriptionSelection = null;
187 187
188 function loadRecommendations() 188 function loadRecommendations()
189 { 189 {
190 var request = new XMLHttpRequest(); 190 fetch("subscriptions.xml")
191 request.open("GET", "subscriptions.xml"); 191 .then(function(response)
192 request.onload = function() 192 {
193 { 193 return response.text();
194 var selectedIndex = 0; 194 })
195 var selectedPrefix = null; 195 .then(function(text)
196 var matchCount = 0; 196 {
197 var selectedIndex = 0;
198 var selectedPrefix = null;
199 var matchCount = 0;
197 200
198 var list = document.getElementById("subscriptionSelector"); 201 var list = document.getElementById("subscriptionSelector");
199 var elements = request.responseXML.documentElement.getElementsByTagName("sub scription"); 202 var doc = new DOMParser().parseFromString(text, "application/xml");
200 for (var i = 0; i < elements.length; i++) 203 var elements = doc.documentElement.getElementsByTagName("subscription");
201 {
202 var element = elements[i];
203 var option = new Option();
204 option.text = element.getAttribute("title") + " (" + element.getAttribute( "specialization") + ")";
205 option._data = {
206 title: element.getAttribute("title"),
207 url: element.getAttribute("url"),
208 homepage: element.getAttribute("homepage")
209 };
210 204
211 var prefix = Utils.checkLocalePrefixMatch(element.getAttribute("prefixes") ); 205 for (var i = 0; i < elements.length; i++)
212 if (prefix)
213 { 206 {
214 option.style.fontWeight = "bold"; 207 var element = elements[i];
215 option.style.backgroundColor = "#E0FFE0"; 208 var option = new Option();
216 option.style.color = "#000000"; 209 option.text = element.getAttribute("title") + " (" + element.getAttribut e("specialization") + ")";
kzar 2016/01/17 17:14:06 Nit: Mind fixing the long lines here as well while
Sebastian Noack 2016/01/19 14:58:10 Done.
217 if (!selectedPrefix || selectedPrefix.length < prefix.length) 210 option._data = {
211 title: element.getAttribute("title"),
212 url: element.getAttribute("url"),
213 homepage: element.getAttribute("homepage")
214 };
215
216 var prefix = Utils.checkLocalePrefixMatch(element.getAttribute("prefixes "));
217 if (prefix)
218 { 218 {
219 selectedIndex = i; 219 option.style.fontWeight = "bold";
220 selectedPrefix = prefix; 220 option.style.backgroundColor = "#E0FFE0";
221 matchCount = 1; 221 option.style.color = "#000000";
222 } 222 if (!selectedPrefix || selectedPrefix.length < prefix.length)
223 else if (selectedPrefix && selectedPrefix.length == prefix.length)
224 {
225 matchCount++;
226
227 // If multiple items have a matching prefix of the same length:
228 // Select one of the items randomly, probability should be the same
229 // for all items. So we replace the previous match here with
230 // probability 1/N (N being the number of matches).
231 if (Math.random() * matchCount < 1)
232 { 223 {
233 selectedIndex = i; 224 selectedIndex = i;
234 selectedPrefix = prefix; 225 selectedPrefix = prefix;
226 matchCount = 1;
227 }
228 else if (selectedPrefix && selectedPrefix.length == prefix.length)
229 {
230 matchCount++;
231
232 // If multiple items have a matching prefix of the same length:
233 // Select one of the items randomly, probability should be the same
234 // for all items. So we replace the previous match here with
235 // probability 1/N (N being the number of matches).
236 if (Math.random() * matchCount < 1)
237 {
238 selectedIndex = i;
239 selectedPrefix = prefix;
240 }
235 } 241 }
236 } 242 }
243 list.appendChild(option);
237 } 244 }
245
246 var option = new Option();
247 option.text = i18n.getMessage("filters_addSubscriptionOther_label") + "\u2 026";
248 option._data = null;
238 list.appendChild(option); 249 list.appendChild(option);
239 }
240 250
241 var option = new Option(); 251 list.selectedIndex = selectedIndex;
242 option.text = i18n.getMessage("filters_addSubscriptionOther_label") + "\u202 6";
243 option._data = null;
244 list.appendChild(option);
245 252
246 list.selectedIndex = selectedIndex; 253 if (delayedSubscriptionSelection)
247 254 startSubscriptionSelection.apply(null, delayedSubscriptionSelection);
248 if (delayedSubscriptionSelection) 255 });
249 startSubscriptionSelection.apply(null, delayedSubscriptionSelection);
250 };
251 request.send(null);
252 } 256 }
253 257
254 function startSubscriptionSelection(title, url) 258 function startSubscriptionSelection(title, url)
255 { 259 {
256 var list = document.getElementById("subscriptionSelector"); 260 var list = document.getElementById("subscriptionSelector");
257 if (list.length == 0) 261 if (list.length == 0)
258 { 262 {
259 delayedSubscriptionSelection = [title, url]; 263 delayedSubscriptionSelection = [title, url];
260 return; 264 return;
261 } 265 }
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 links[i].href = arguments[i + 1]; 686 links[i].href = arguments[i + 1];
683 links[i].setAttribute("target", "_blank"); 687 links[i].setAttribute("target", "_blank");
684 } 688 }
685 else if (typeof arguments[i + 1] == "function") 689 else if (typeof arguments[i + 1] == "function")
686 { 690 {
687 links[i].href = "javascript:void(0);"; 691 links[i].href = "javascript:void(0);";
688 links[i].addEventListener("click", arguments[i + 1], false); 692 links[i].addEventListener("click", arguments[i + 1], false);
689 } 693 }
690 } 694 }
691 } 695 }
OLDNEW
« background.js ('K') | « options.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld