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

Side by Side Diff: lib/utils.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Created Jan. 13, 2017, 12:11 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 <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 var Utils = exports.Utils = { 18 let Utils = exports.Utils = {
19 systemPrincipal: null, 19 systemPrincipal: null,
20 getString: function(id) 20 getString: id => ext.i18n.getMessage("global_" + id),
21 { 21 runAsync: callback =>
22 return ext.i18n.getMessage("global_" + id);
23 },
24 runAsync: function(callback)
25 { 22 {
26 if (document.readyState == "loading") 23 if (document.readyState == "loading")
27 { 24 {
28 // Make sure to not run asynchronous actions before all 25 // Make sure to not run asynchronous actions before all
29 // scripts loaded. This caused issues on Opera in the past. 26 // scripts loaded. This caused issues on Opera in the past.
30 let onDOMContentLoaded = function() 27 let onDOMContentLoaded = () =>
31 { 28 {
32 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); 29 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
33 callback(); 30 callback();
34 }; 31 };
35 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); 32 document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
36 } 33 }
37 else 34 else
38 { 35 {
39 setTimeout(callback, 0); 36 setTimeout(callback, 0);
40 } 37 }
41 }, 38 },
42 get appLocale() 39 get appLocale()
43 { 40 {
44 var locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 41 let locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
45 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); 42 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true});
46 return this.appLocale; 43 return this.appLocale;
47 }, 44 },
48 generateChecksum: function(lines) 45 // We cannot calculate MD5 checksums yet :-(
49 { 46 generateChecksum: lines => null,
50 // We cannot calculate MD5 checksums yet :-(
51 return null;
52 },
53 checkLocalePrefixMatch: function(prefixes) 47 checkLocalePrefixMatch: function(prefixes)
54 { 48 {
55 if (!prefixes) 49 if (!prefixes)
56 return null; 50 return null;
57 51
58 var list = prefixes.split(","); 52 for (let prefix of prefixes.split(","))
59 for (var i = 0; i < list.length; i++) 53 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
60 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) 54 return prefix;
61 return list[i];
62 55
63 return null; 56 return null;
64 }, 57 },
65 58
66 chooseFilterSubscription: function(subscriptions) 59 chooseFilterSubscription: subscriptions =>
67 { 60 {
68 var selectedItem = null; 61 let selectedItem = null;
69 var selectedPrefix = null; 62 let selectedPrefix = null;
70 var matchCount = 0; 63 let matchCount = 0;
71 for (var i = 0; i < subscriptions.length; i++) 64 for (let subscription of subscriptions)
72 { 65 {
73 var subscription = subscriptions[i];
74 if (!selectedItem) 66 if (!selectedItem)
75 selectedItem = subscription; 67 selectedItem = subscription;
76 68
77 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 69 let prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes"));
78 if (prefix) 70 if (prefix)
79 { 71 {
80 if (!selectedPrefix || selectedPrefix.length < prefix.length) 72 if (!selectedPrefix || selectedPrefix.length < prefix.length)
81 { 73 {
82 selectedItem = subscription; 74 selectedItem = subscription;
83 selectedPrefix = prefix; 75 selectedPrefix = prefix;
84 matchCount = 1; 76 matchCount = 1;
85 } 77 }
86 else if (selectedPrefix && selectedPrefix.length == prefix.length) 78 else if (selectedPrefix && selectedPrefix.length == prefix.length)
87 { 79 {
88 matchCount++; 80 matchCount++;
89 81
90 // If multiple items have a matching prefix of the same length: 82 // If multiple items have a matching prefix of the same length:
91 // Select one of the items randomly, probability should be the same 83 // Select one of the items randomly, probability should be the same
92 // for all items. So we replace the previous match here with 84 // for all items. So we replace the previous match here with
93 // probability 1/N (N being the number of matches). 85 // probability 1/N (N being the number of matches).
94 if (Math.random() * matchCount < 1) 86 if (Math.random() * matchCount < 1)
95 { 87 {
96 selectedItem = subscription; 88 selectedItem = subscription;
97 selectedPrefix = prefix; 89 selectedPrefix = prefix;
98 } 90 }
99 } 91 }
100 } 92 }
101 } 93 }
102 return selectedItem; 94 return selectedItem;
103 }, 95 },
104 96
105 getDocLink: function(linkID) 97 getDocLink: linkID =>
106 { 98 {
107 var Prefs = require("prefs").Prefs; 99 let Prefs = require("prefs").Prefs;
108 var docLink = Prefs.documentation_link; 100 let docLink = Prefs.documentation_link;
109 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 101 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale );
110 }, 102 },
111 103
112 yield: function() 104 yield: () =>
113 { 105 {
114 } 106 }
115 }; 107 };
OLDNEW
« chrome/ext/common.js ('K') | « lib/url.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld