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: Rebased and addressed comments Created Jan. 19, 2016, 2:29 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 | « 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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 checkbox.checked = descriptor.toggle(); 190 checkbox.checked = descriptor.toggle();
191 191
192 Prefs[id] = checkbox.checked; 192 Prefs[id] = checkbox.checked;
193 }, false); 193 }, false);
194 } 194 }
195 195
196 var delayedSubscriptionSelection = null; 196 var delayedSubscriptionSelection = null;
197 197
198 function loadRecommendations() 198 function loadRecommendations()
199 { 199 {
200 var request = new XMLHttpRequest(); 200 fetch("subscriptions.xml")
201 request.open("GET", "subscriptions.xml"); 201 .then(function(response)
202 request.onload = function() 202 {
203 { 203 return response.text();
204 var selectedIndex = 0; 204 })
205 var selectedPrefix = null; 205 .then(function(text)
206 var matchCount = 0; 206 {
207 var selectedIndex = 0;
208 var selectedPrefix = null;
209 var matchCount = 0;
207 210
208 var list = document.getElementById("subscriptionSelector"); 211 var list = document.getElementById("subscriptionSelector");
209 var elements = request.responseXML.documentElement.getElementsByTagName("sub scription"); 212 var doc = new DOMParser().parseFromString(text, "application/xml");
210 for (var i = 0; i < elements.length; i++) 213 var elements = doc.documentElement.getElementsByTagName("subscription");
211 {
212 var element = elements[i];
213 var option = new Option();
214 option.text = element.getAttribute("title") + " (" + element.getAttribute( "specialization") + ")";
215 option._data = {
216 title: element.getAttribute("title"),
217 url: element.getAttribute("url"),
218 homepage: element.getAttribute("homepage")
219 };
220 214
221 var prefix = Utils.checkLocalePrefixMatch(element.getAttribute("prefixes") ); 215 for (var i = 0; i < elements.length; i++)
222 if (prefix)
223 { 216 {
224 option.style.fontWeight = "bold"; 217 var element = elements[i];
225 option.style.backgroundColor = "#E0FFE0"; 218 var option = new Option();
226 option.style.color = "#000000"; 219 option.text = element.getAttribute("title") + " (" +
227 if (!selectedPrefix || selectedPrefix.length < prefix.length) 220 element.getAttribute("specialization") + ")";
221 option._data = {
222 title: element.getAttribute("title"),
223 url: element.getAttribute("url"),
224 homepage: element.getAttribute("homepage")
225 };
226
227 var prefixes = element.getAttribute("prefixes");
228 var prefix = Utils.checkLocalePrefixMatch(prefixes);
229 if (prefix)
228 { 230 {
229 selectedIndex = i; 231 option.style.fontWeight = "bold";
230 selectedPrefix = prefix; 232 option.style.backgroundColor = "#E0FFE0";
231 matchCount = 1; 233 option.style.color = "#000000";
232 } 234 if (!selectedPrefix || selectedPrefix.length < prefix.length)
233 else if (selectedPrefix && selectedPrefix.length == prefix.length)
234 {
235 matchCount++;
236
237 // If multiple items have a matching prefix of the same length:
238 // Select one of the items randomly, probability should be the same
239 // for all items. So we replace the previous match here with
240 // probability 1/N (N being the number of matches).
241 if (Math.random() * matchCount < 1)
242 { 235 {
243 selectedIndex = i; 236 selectedIndex = i;
244 selectedPrefix = prefix; 237 selectedPrefix = prefix;
238 matchCount = 1;
239 }
240 else if (selectedPrefix && selectedPrefix.length == prefix.length)
241 {
242 matchCount++;
243
244 // If multiple items have a matching prefix of the same length:
245 // Select one of the items randomly, probability should be the same
246 // for all items. So we replace the previous match here with
247 // probability 1/N (N being the number of matches).
248 if (Math.random() * matchCount < 1)
249 {
250 selectedIndex = i;
251 selectedPrefix = prefix;
252 }
245 } 253 }
246 } 254 }
255 list.appendChild(option);
247 } 256 }
257
258 var option = new Option();
259 var label = i18n.getMessage("filters_addSubscriptionOther_label");
260 option.text = label + "\u2026";
261 option._data = null;
248 list.appendChild(option); 262 list.appendChild(option);
249 }
250 263
251 var option = new Option(); 264 list.selectedIndex = selectedIndex;
252 option.text = i18n.getMessage("filters_addSubscriptionOther_label") + "\u202 6";
253 option._data = null;
254 list.appendChild(option);
255 265
256 list.selectedIndex = selectedIndex; 266 if (delayedSubscriptionSelection)
257 267 startSubscriptionSelection.apply(null, delayedSubscriptionSelection);
258 if (delayedSubscriptionSelection) 268 });
259 startSubscriptionSelection.apply(null, delayedSubscriptionSelection);
260 };
261 request.send(null);
262 } 269 }
263 270
264 function startSubscriptionSelection(title, url) 271 function startSubscriptionSelection(title, url)
265 { 272 {
266 var list = document.getElementById("subscriptionSelector"); 273 var list = document.getElementById("subscriptionSelector");
267 if (list.length == 0) 274 if (list.length == 0)
268 { 275 {
269 delayedSubscriptionSelection = [title, url]; 276 delayedSubscriptionSelection = [title, url];
270 return; 277 return;
271 } 278 }
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 links[i].href = arguments[i + 1]; 699 links[i].href = arguments[i + 1];
693 links[i].setAttribute("target", "_blank"); 700 links[i].setAttribute("target", "_blank");
694 } 701 }
695 else if (typeof arguments[i + 1] == "function") 702 else if (typeof arguments[i + 1] == "function")
696 { 703 {
697 links[i].href = "javascript:void(0);"; 704 links[i].href = "javascript:void(0);";
698 links[i].addEventListener("click", arguments[i + 1], false); 705 links[i].addEventListener("click", arguments[i + 1], false);
699 } 706 }
700 } 707 }
701 } 708 }
OLDNEW
« no previous file with comments | « options.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld