Left: | ||
Right: |
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 = ext.i18n.getMessage("@@ui_locale"); | 46 let locale = ext.i18n.getUILanguage(); |
48 if (locale === "") { | |
Sebastian Noack
2016/03/14 09:03:06
Nit: We always use == (instead ===) where possible
| |
49 locale = ext.i18n.getUILanguage(); | |
50 } | |
51 locale = locale.replace(/_/g, "-"); | |
52 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); | 47 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); |
53 return this.appLocale; | 48 return this.appLocale; |
54 }, | 49 }, |
55 get readingDirection() | 50 get readingDirection() |
56 { | 51 { |
57 var direction; | 52 let direction = ext.i18n.getMessage("@@bidi_dir"); |
58 if ("chromeRegistry" in this) { | 53 // This fallback is only necessary for Microsoft Edge |
59 direction = this.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "lt r"; | 54 if (!direction) |
Sebastian Noack
2016/03/14 09:03:07
This code path is specific to Adblock Plus for Fir
| |
60 } | 55 direction = /^(?:ar|fa|he|ug|ur)\b/.test(this.appLocale) ? "rtl" : "ltr"; |
61 else { | 56 Object.defineProperty( |
62 var direction = ext.i18n.getMessage("@bidi_dir"); | 57 this, |
63 if (direction === "") { | 58 "readingDirection", |
64 direction = /^(ar|fa|he|ug|ur)(_|$)/.test(appLocale) ? "rtl" : "ltr" ; | 59 {value: direction, enumerable: true} |
Sebastian Noack
2016/03/14 09:03:07
This logic is redundant with code in safari/ext/co
| |
65 } | 60 ); |
66 } | 61 return this.readingDirection; |
67 Object.defineProperty(this, "readingDirection", {value: direction, enumera ble: true}); | |
68 return this.readingDirection; | |
69 }, | 62 }, |
70 generateChecksum: function(lines) | 63 generateChecksum(lines) |
71 { | 64 { |
72 // We cannot calculate MD5 checksums yet :-( | 65 // We cannot calculate MD5 checksums yet :-( |
73 return null; | 66 return null; |
74 }, | 67 }, |
75 makeURI: function(url) | 68 checkLocalePrefixMatch(prefixes) |
76 { | |
77 return Services.io.newURI(url); | |
78 }, | |
79 | |
80 checkLocalePrefixMatch: function(prefixes) | |
81 { | 69 { |
82 if (!prefixes) | 70 if (!prefixes) |
83 return null; | 71 return null; |
84 | 72 |
85 var list = prefixes.split(","); | 73 for (let prefix of prefixes.split(",")) |
86 for (var i = 0; i < list.length; i++) | 74 { |
87 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) | 75 if (new RegExp("^" + prefix + "\\b").test(this.appLocale)) |
88 return list[i]; | 76 return prefix; |
77 } | |
89 | 78 |
90 return null; | 79 return null; |
91 }, | 80 }, |
92 | 81 |
93 chooseFilterSubscription: function(subscriptions) | 82 chooseFilterSubscription(subscriptions) |
94 { | 83 { |
95 var selectedItem = null; | 84 let selectedItem = null; |
96 var selectedPrefix = null; | 85 let selectedPrefix = null; |
97 var matchCount = 0; | 86 let matchCount = 0; |
98 for (var i = 0; i < subscriptions.length; i++) | 87 for (let subscription of subscriptions) |
99 { | 88 { |
100 var subscription = subscriptions[i]; | |
101 if (!selectedItem) | 89 if (!selectedItem) |
102 selectedItem = subscription; | 90 selectedItem = subscription; |
103 | 91 |
104 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); | 92 let prefix = Utils.checkLocalePrefixMatch( |
93 subscription.getAttribute("prefixes") | |
94 ); | |
105 if (prefix) | 95 if (prefix) |
106 { | 96 { |
107 if (!selectedPrefix || selectedPrefix.length < prefix.length) | 97 if (!selectedPrefix || selectedPrefix.length < prefix.length) |
108 { | 98 { |
109 selectedItem = subscription; | 99 selectedItem = subscription; |
110 selectedPrefix = prefix; | 100 selectedPrefix = prefix; |
111 matchCount = 1; | 101 matchCount = 1; |
112 } | 102 } |
113 else if (selectedPrefix && selectedPrefix.length == prefix.length) | 103 else if (selectedPrefix && selectedPrefix.length == prefix.length) |
114 { | 104 { |
115 matchCount++; | 105 matchCount++; |
116 | 106 |
117 // If multiple items have a matching prefix of the same length: | 107 // If multiple items have a matching prefix of the same length: |
118 // Select one of the items randomly, probability should be the same | 108 // Select one of the items randomly, probability should be the same |
119 // for all items. So we replace the previous match here with | 109 // for all items. So we replace the previous match here with |
120 // probability 1/N (N being the number of matches). | 110 // probability 1/N (N being the number of matches). |
121 if (Math.random() * matchCount < 1) | 111 if (Math.random() * matchCount < 1) |
122 { | 112 { |
123 selectedItem = subscription; | 113 selectedItem = subscription; |
124 selectedPrefix = prefix; | 114 selectedPrefix = prefix; |
125 } | 115 } |
126 } | 116 } |
127 } | 117 } |
128 } | 118 } |
129 return selectedItem; | 119 return selectedItem; |
130 }, | 120 }, |
131 | 121 |
132 getDocLink: function(linkID) | 122 getDocLink(linkID) |
133 { | 123 { |
134 var Prefs = require("prefs").Prefs; | 124 let docLink = require("prefs").Prefs.documentation_link; |
135 var docLink = Prefs.documentation_link; | 125 return docLink.replace(/%LINK%/g, linkID) |
136 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); | 126 .replace(/%LANG%/g, Utils.appLocale); |
137 }, | 127 }, |
138 | 128 |
139 yield: function() | 129 yield() |
140 { | 130 { |
141 } | 131 } |
142 }; | 132 }; |
LEFT | RIGHT |