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

Delta Between Two Patch Sets: lib/utils.js

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

Powered by Google App Engine
This is Rietveld