LEFT | RIGHT |
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-present 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 "use strict"; |
| 19 |
| 20 let Utils = exports.Utils = { |
19 systemPrincipal: null, | 21 systemPrincipal: null, |
20 getString: function(id) | 22 getString(id) |
21 { | 23 { |
22 if (typeof ext !== "undefined" && "i18n" in ext) | 24 return ext.i18n.getMessage("global_" + id); |
23 return ext.i18n.getMessage("global_" + id); | |
24 else | |
25 return id; | |
26 }, | 25 }, |
27 runAsync: function(callback) | 26 runAsync(callback) |
28 { | 27 { |
29 if (document.readyState == "loading") | 28 if (document.readyState == "loading") |
30 { | 29 { |
31 // Make sure to not run asynchronous actions before all | 30 // Make sure to not run asynchronous actions before all |
32 // scripts loaded. This caused issues on Opera in the past. | 31 // scripts loaded. This caused issues on Opera in the past. |
33 let onDOMContentLoaded = function() | 32 let onDOMContentLoaded = () => |
34 { | 33 { |
35 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); | 34 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); |
36 callback(); | 35 callback(); |
37 }; | 36 }; |
38 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); | 37 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); |
39 } | 38 } |
40 else | 39 else |
41 { | 40 { |
42 setTimeout(callback, 0); | 41 setTimeout(callback, 0); |
43 } | 42 } |
44 }, | 43 }, |
45 get appLocale() | 44 get appLocale() |
46 { | 45 { |
47 var locale; | 46 let locale = ext.i18n.getUILanguage(); |
48 try | |
49 { | |
50 locale = ext.i18n.getMessage("@@ui_locale"); | |
51 } | |
52 catch (exception) | |
53 { | |
54 // Edge does not yet (?) support @@ui_locale, throwing an exception here. | |
55 } | |
56 if (!locale) | |
57 locale = ext.i18n.getUILanguage(); | |
58 locale = locale.replace(/_/g, "-"); | |
59 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); | 47 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); |
60 return this.appLocale; | 48 return this.appLocale; |
61 }, | 49 }, |
62 get readingDirection() | 50 get readingDirection() |
63 { | 51 { |
64 var direction; | 52 let direction = ext.i18n.getMessage("@@bidi_dir"); |
65 try | 53 // This fallback is only necessary for Microsoft Edge |
66 { | |
67 direction = ext.i18n.getMessage("@@bidi_dir"); | |
68 } | |
69 catch (exception) | |
70 { | |
71 // Edge does not yet (?) support @@bidi_dir, throwing an exception here. | |
72 } | |
73 if (!direction) | 54 if (!direction) |
74 direction = /^(ar|fa|he|ug|ur)(_|$)/.test(this.appLocale) ? "rtl" : "ltr"; | 55 direction = /^(?:ar|fa|he|ug|ur)\b/.test(this.appLocale) ? "rtl" : "ltr"; |
75 Object.defineProperty(this, "readingDirection", {value: direction, enumerabl
e: true}); | 56 Object.defineProperty( |
| 57 this, |
| 58 "readingDirection", |
| 59 {value: direction, enumerable: true} |
| 60 ); |
76 return this.readingDirection; | 61 return this.readingDirection; |
77 }, | 62 }, |
78 generateChecksum: function(lines) | 63 generateChecksum(lines) |
79 { | 64 { |
80 // We cannot calculate MD5 checksums yet :-( | 65 // We cannot calculate MD5 checksums yet :-( |
81 return null; | 66 return null; |
82 }, | 67 }, |
83 checkLocalePrefixMatch: function(prefixes) | 68 checkLocalePrefixMatch(prefixes) |
84 { | 69 { |
85 if (!prefixes) | 70 if (!prefixes) |
86 return null; | 71 return null; |
87 | 72 |
88 var list = prefixes.split(","); | 73 for (let prefix of prefixes.split(",")) |
89 for (var i = 0; i < list.length; i++) | 74 { |
90 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) | 75 if (new RegExp("^" + prefix + "\\b").test(this.appLocale)) |
91 return list[i]; | 76 return prefix; |
| 77 } |
92 | 78 |
93 return null; | 79 return null; |
94 }, | 80 }, |
95 | 81 |
96 chooseFilterSubscription: function(subscriptions) | 82 chooseFilterSubscription(subscriptions) |
97 { | 83 { |
98 var selectedItem = null; | 84 let selectedItem = null; |
99 var selectedPrefix = null; | 85 let selectedPrefix = null; |
100 var matchCount = 0; | 86 let matchCount = 0; |
101 for (var i = 0; i < subscriptions.length; i++) | 87 for (let subscription of subscriptions) |
102 { | 88 { |
103 var subscription = subscriptions[i]; | |
104 if (!selectedItem) | 89 if (!selectedItem) |
105 selectedItem = subscription; | 90 selectedItem = subscription; |
106 | 91 |
107 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge
tAttribute("prefixes")); | 92 let prefix = Utils.checkLocalePrefixMatch( |
| 93 subscription.getAttribute("prefixes") |
| 94 ); |
108 if (prefix) | 95 if (prefix) |
109 { | 96 { |
110 if (!selectedPrefix || selectedPrefix.length < prefix.length) | 97 if (!selectedPrefix || selectedPrefix.length < prefix.length) |
111 { | 98 { |
112 selectedItem = subscription; | 99 selectedItem = subscription; |
113 selectedPrefix = prefix; | 100 selectedPrefix = prefix; |
114 matchCount = 1; | 101 matchCount = 1; |
115 } | 102 } |
116 else if (selectedPrefix && selectedPrefix.length == prefix.length) | 103 else if (selectedPrefix && selectedPrefix.length == prefix.length) |
117 { | 104 { |
118 matchCount++; | 105 matchCount++; |
119 | 106 |
120 // If multiple items have a matching prefix of the same length: | 107 // If multiple items have a matching prefix of the same length: |
121 // Select one of the items randomly, probability should be the same | 108 // Select one of the items randomly, probability should be the same |
122 // for all items. So we replace the previous match here with | 109 // for all items. So we replace the previous match here with |
123 // probability 1/N (N being the number of matches). | 110 // probability 1/N (N being the number of matches). |
124 if (Math.random() * matchCount < 1) | 111 if (Math.random() * matchCount < 1) |
125 { | 112 { |
126 selectedItem = subscription; | 113 selectedItem = subscription; |
127 selectedPrefix = prefix; | 114 selectedPrefix = prefix; |
128 } | 115 } |
129 } | 116 } |
130 } | 117 } |
131 } | 118 } |
132 return selectedItem; | 119 return selectedItem; |
133 }, | 120 }, |
134 | 121 |
135 getDocLink: function(linkID) | 122 getDocLink(linkID) |
136 { | 123 { |
137 var Prefs = require("prefs").Prefs; | 124 let docLink = require("prefs").Prefs.documentation_link; |
138 var docLink = Prefs.documentation_link; | 125 return docLink.replace(/%LINK%/g, linkID) |
139 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale
); | 126 .replace(/%LANG%/g, Utils.appLocale); |
140 }, | 127 }, |
141 | 128 |
142 yield: function() | 129 yield() |
143 { | 130 { |
144 } | 131 } |
145 }; | 132 }; |
LEFT | RIGHT |