-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduolingo.user.js
230 lines (222 loc) · 6.81 KB
/
duolingo.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// ==UserScript==
// @name duolingo
// @namespace https://github.com/lilydjwg/userscripts
// @description duolingo tweaks
// @include https://*.duolingo.com/*
// @version 0.1
// @grant none
// @run-at document-start
// ==/UserScript==
// License: Mozilla Public License Version 2.0
// Modified from https://addons.mozilla.org/firefox/addon/spoof-timezone/
(function() {
'use strict'
const prefs = `
Date.prefs = Date.prefs || [
'Asia/Singapore', -480, new Date().getTimezoneOffset(), 'Singapore Standard Time'
];
try { // get preferences for subframes synchronously
Date.prefs = window.parent.Date.prefs;
}
catch (e) {}
`;
const intl = `
const ODateTimeFormat = Intl.DateTimeFormat;
Intl.DateTimeFormat = function(locales, options = {}) {
Object.assign(options, {
timeZone: Date.prefs[0]
});
return ODateTimeFormat(locales, options);
};
Intl.DateTimeFormat.prototype = Object.create(ODateTimeFormat.prototype);
Intl.DateTimeFormat.supportedLocalesOf = ODateTimeFormat.supportedLocalesOf;
`;
const shiftedDate = `
const clean = str => {
const toGMT = offset => {
const z = n => (n < 10 ? '0' : '') + n;
const sign = offset <= 0 ? '+' : '-';
offset = Math.abs(offset);
return sign + z(offset / 60 | 0) + z(offset % 60);
};
str = str.replace(/([T\\(])[\\+-]\\d+/g, '$1' + toGMT(Date.prefs[1]));
if (str.indexOf(' (') !== -1) {
str = str.split(' (')[0] + ' (' + Date.prefs[3] + ')';
}
return str;
}
const ODate = Date;
const {
getTime, getDate, getDay, getFullYear, getHours, getMilliseconds, getMinutes, getMonth, getSeconds, getYear,
toDateString, toLocaleString, toString, toTimeString, toLocaleTimeString, toLocaleDateString,
setYear, setHours, setTime, setFullYear, setMilliseconds, setMinutes, setMonth, setSeconds, setDate,
setUTCDate, setUTCFullYear, setUTCHours, setUTCMilliseconds, setUTCMinutes, setUTCMonth, setUTCSeconds
} = ODate.prototype;
class ShiftedDate extends ODate {
constructor(...args) {
super(...args);
this.nd = new ODate(
getTime.apply(this) + (Date.prefs[2] - Date.prefs[1]) * 60 * 1000
);
}
// get
toLocaleString(...args) {
return toLocaleString.apply(this.nd, args);
}
toLocaleTimeString(...args) {
return toLocaleTimeString.apply(this.nd, args);
}
toLocaleDateString(...args) {
return toLocaleDateString.apply(this.nd, args);
}
toDateString(...args) {
return toDateString.apply(this.nd, args);
}
getDate(...args) {
return getDate.apply(this.nd, args);
}
getDay(...args) {
return getDay.apply(this.nd, args);
}
getFullYear(...args) {
return getFullYear.apply(this.nd, args);
}
getHours(...args) {
return getHours.apply(this.nd, args);
}
getMilliseconds(...args) {
return getMilliseconds.apply(this.nd, args);
}
getMinutes(...args) {
return getMinutes.apply(this.nd, args);
}
getMonth(...args) {
return getMonth.apply(this.nd, args);
}
getSeconds(...args) {
return getSeconds.apply(this.nd, args);
}
getYear(...args) {
return getYear.apply(this.nd, args);
}
// set
setHours(...args) {
const a = getTime.call(this.nd);
const b = setHours.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setFullYear(...args) {
const a = getTime.call(this.nd);
const b = setFullYear.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setMilliseconds(...args) {
const a = getTime.call(this.nd);
const b = setMilliseconds.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setMinutes(...args) {
const a = getTime.call(this.nd);
const b = setMinutes.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setMonth(...args) {
const a = getTime.call(this.nd);
const b = setMonth.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setSeconds(...args) {
const a = getTime.call(this.nd);
const b = setSeconds.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setDate(...args) {
const a = getTime.call(this.nd);
const b = setDate.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setYear(...args) {
const a = getTime.call(this.nd);
const b = setYear.apply(this.nd, args);
setTime.call(this, getTime.call(this) + b - a);
return b;
}
setTime(...args) {
const a = getTime.call(this);
const b = setTime.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCDate(...args) {
const a = getTime.call(this);
const b = setUTCDate.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCFullYear(...args) {
const a = getTime.call(this);
const b = setUTCFullYear.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCHours(...args) {
const a = getTime.call(this);
const b = setUTCHours.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCMilliseconds(...args) {
const a = getTime.call(this);
const b = setUTCMilliseconds.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCMinutes(...args) {
const a = getTime.call(this);
const b = setUTCMinutes.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCMonth(...args) {
const a = getTime.call(this);
const b = setUTCMonth.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
setUTCSeconds(...args) {
const a = getTime.call(this);
const b = setUTCSeconds.apply(this, args);
setTime.call(this.nd, getTime.call(this.nd) + b - a);
return b;
}
// toString
toString(...args) {
return clean(toString.apply(this.nd, args));
}
toTimeString(...args) {
return clean(toTimeString.apply(this.nd, args));
}
// offset
getTimezoneOffset() {
return Date.prefs[1];
}
}
`;
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#constructors_from_the_page_context
const script = document.createElement('script');
script.textContent = `{
${prefs}
${intl}
${shiftedDate}
Date = ShiftedDate;
}`;
document.documentElement.appendChild(script);
script.remove();
})()