-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpgrade.ts
439 lines (405 loc) · 15.5 KB
/
Upgrade.ts
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/// <reference path="./Module.ts" />
declare var __extends;
var exports: any = {};
var __global: {
https: boolean,
supportTemplate: boolean,
useListenerOrMutation: boolean,
ApiServer?: System.basics.Url,
CaseSensitive: boolean;
GetApiAddress(url: string): string;
} = <any>{};
var envirenment: { isBrowser: boolean, isWebWorker: boolean, isOpera: boolean, isChromeApp: boolean, isMobile: boolean };
var clone: (obj) => any;
var $: (selector, context) => void;
var $defineProperty: (o: any, p: string, attributes: PropertyDescriptor & ThisType<any>, onError?: (o: any, p: string, attributes: PropertyDescriptor & ThisType<any>) => any) => any;
var $bench: (f: () => any, timeSpan: number) => number;
declare var w: Window;
((window: any) => {
function detectmob() {
return (navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
);
}
function bench(f, timeSpan) {
var t = performance.now();
var i = 0;
while ((performance.now() - t) < timeSpan) {
i++;
f();
}
return i;
}
$defineProperty = function (o: any, p: string, attributes: PropertyDescriptor & ThisType<any>, onError: (o: any, p: string, attributes: PropertyDescriptor & ThisType<any>) => any): any {
try {
return Object.defineProperty(o, p, attributes);
} catch (e) {
onError && onError(o, p, attributes);
return false;
}
}
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
__global = {
supportTemplate: (function supportsTemplate() { return 'content' in document.createElement('template'); })(),
useListenerOrMutation: true,
GetApiAddress(url: string) {
return __global.ApiServer ? __global.ApiServer.Combine(url).toString() : url;
},
CaseSensitive: false,
https: false,
ApiServer: undefined
};
var __extends1 = function (d, b) {
$defineProperty(d, 'base', {
value: b,
writable: false,
enumerable: true,
configurable: false
});
for (var p in b) {
if (p == 'base' || p.indexOf('_') === 0 || p == 'ctor')
continue;
if (b.hasOwnProperty(p))
d[p] = b[p];
}
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
$defineProperty(window, '__extends', { get: function () { return __extends1 }, set: function () { }, configurable: false, enumerable: false }, (o, p, a) => { __extends = __extends1; });
var isBrowser: boolean = isBrowser = (!!(typeof window !== 'undefined' && typeof navigator !== 'undefined' && window.document));
envirenment = {
isBrowser: isBrowser,
isWebWorker: !isBrowser && typeof importScripts !== 'undefined',
isOpera: typeof (window as any).opera !== 'undefined' && (window as any).opera.toString() === '[object Opera]',
isChromeApp: document.location.protocol === 'chrome-extension:',
isMobile: !!detectmob()
};
if (!__global.supportTemplate) $defineProperty(HTMLUnknownElement.prototype, 'content', { get: function () { return this; } });
clone = function (obj) {
if (!obj) return obj;
if (typeof obj === 'object') {
var copy = obj.constructor();
for (var attr in obj)
if (obj.hasOwnProperty(attr))
copy[attr] = obj[attr];
return copy;
}
else if (obj instanceof Array)
return obj.splice(0);
return obj;
}
//if ((Object.prototype as any).clone === void 0)
// (Object.prototype as any).clone = function (obj) {
// for (var i in obj)
// this[i] = obj[i];
// return this;
// }
function getScrollArea(x: Element) {
var h = []; var t = x; while (t) {
var oy = getComputedStyle(t).overflowY;
if (oy == "auto") return t;
t = t.parentElement;
}
}
(Element.prototype as any).getScrollArea = function () { return getScrollArea(this); };
(Object as any).clone = clone;
if ((Element.prototype as any).scrollIntoViewIfNeeded == void 0)
(Element.prototype as any).scrollIntoViewIfNeeded = function () {
var r = this.getBoundingClientRect();
var v = getScrollArea(this);
if (!v) {
this.scrollIntoView({ inline: 'center' });
return;
}
var t = document.body;
var prRect = (v || t).getBoundingClientRect();
var chRect = this.getBoundingClientRect();
var x = new IDBDatabase();
if (prRect.top - r.top > 0)
v.scrollTop -= prRect.top - r.top;
else if (prRect.bottom < r.bottom)
v.scrollTop -= prRect.bottom - r.bottom;
//var maxTop = prRect.top + prRect.height - chRect.height;
//if (!v) {
// v.scrollIntoView({ inline: 'center' });
// return;
//}
//else if (chRect.top - chRect.height < prRect.top) {
// v.scrollTop += chRect.top - chRect.height;
//}
//else if (chRect.bottom > prRect.bottom)
// v.scrollTop += chRect.top - chRect.height - prRect.top;
}
if ((String.prototype as any).endsWith === void 0)
(String.prototype as any).endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
if ((String.prototype as any).startsWith === void 0)
(String.prototype as any).startsWith = function (suffix) {
return this.indexOf(suffix) === 0;
};
if ((HTMLElement.prototype as any).remove === void 0) {
(HTMLElement.prototype as any).remove = function () {
this.removeNode(true);
};
}
if ((HTMLElement.prototype as any).parent === void 0) {
(HTMLElement.prototype as any).parent = function () {
return this.parentElement || this.parentNode;
};
}
(HTMLElement.prototype as any).insertChildAtIndex = function (v, i) {
var c = this.children.length;
if (i < c)
this.insertBefore(v, this.children.item(i));
else
this.appendChild(v);
}
$ = function (selector, context) {
var b = document.body;
var d = document;
context = context === d ? b : context || b;
//if (!b.contains(context)) throw "";
var o = context === b;
var s = selector[0] || '';
if (s === '#') {
if (o) return document.getElementById(selector.substr(1));
return getElementById(selector.substr(1), context);
}
if (s === '.') {
if (o) return document.getElementsByClassName(selector.substr(1));
return getElementByClassName(selector.substr(1), context);
}
if (s === '[')
return getElementByAttribute(selector.substr(1, selector.length - 2), context);
if (o) return document.getElementsByTagName(selector);
return getElementByTagName(selector, context);
}
function getElementById(id, context) {
if (context instanceof HTMLElement) {
if (id === context.id) return context;
return _getElementById(id, context);
}
if (context instanceof Element)
return context.id == id ? context : undefined;
return undefined;
}
function _getElementById(id, cntxt) {
const t = new Array(1000);
t[0] = cntxt;
var it = 0;
while (it >= 0) {
var root = t[it--];
for (var i = 0, l = root.children.length; i < l; i++) {
var child = root.children[i];
if (child.id === id) return child;
if (child instanceof HTMLElement)
t[++it] = child;
}
}
}
function getElementByTagName(tag, context) {
tag = tag.toUpperCase();
if (context instanceof HTMLElement)
return _getElementByTagName(tag, context);
if (context instanceof Element)
return context.tagName === tag ? [context] : [];
return [];
}
function _getElementByTagName(tag, cntxt) {
var t = new Array(1000);
if (tag === cntxt.tagName) var ret = [cntxt];
else ret = [];
t[0] = cntxt;
var it = 0;
while (it >= 0) {
var root = t[it--];
for (var i = 0, l = root.children.length; i < l; i++) {
var child = root.children[i];
if (child.tagName === tag) ret.push(child);
if (child instanceof HTMLElement)
t[++it] = child;
}
}
return ret;
}
function getElementByClassName(tag, context) {
if (context instanceof HTMLElement)
return _getElementByClassName(tag, context);
if (context instanceof Element)
return context.classList.contains(tag) ? [context] : [];
return [];
}
function _getElementByClassName(tag, cntxt) {
var t = new Array(1000);
t[0] = cntxt;
var it = 0;
if (cntxt.classList.contains(tag)) var ret = [cntxt];
else ret = [];
while (it >= 0) {
var root = t[it--];
for (var i = 0, l = root.children.length; i < l; i++) {
var child = root.children[i];
if (child.classList.contains(tag))
ret.push(child);
if (child instanceof HTMLElement)
t[++it] = child;
}
}
return ret;
}
function getElementByAttribute(tag, context) {
if (context instanceof HTMLElement)
return _getElementByAttribute(tag, context);
if (context instanceof Element)
return context.hasAttribute(tag) ? [context] : [];
return [];
}
function _getElementByAttribute(tag, cntxt) {
var t = new Array(1000);
if (cntxt.hasAttribute(tag)) var ret = [cntxt];
else ret = [];
t[0] = cntxt;
var it = 0;
while (it >= 0) {
var root = t[it--];
for (var i = 0, l = root.children.length; i < l; i++) {
var child = root.children[i];
if (child.hasAttribute(tag)) ret.push(child);
if (child instanceof HTMLElement)
t[++it] = child;
}
}
return ret;
}
if (!DocumentFragment.prototype.hasOwnProperty('firstElementChild'))
$defineProperty(DocumentFragment.prototype, 'firstElementChild', {
get: function () {
var c = this.children || this.childNodes;
for (var i = 0; i < c.length; i++)
if (c[i] instanceof Element)
return c[i];
return null;
},
configurable: false
});
function test(fn, iter) {
var t = Date.now();
iter = iter || 200000;
for (var i = 0; i < iter; i++) {
fn();
}
return Date.now() - t;
}
if (typeof DOMParser === 'undefined')
window.DOMParser = function DOMParser() {
this.parseFromString = function (markup, type) {
if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
var
doc = document.implementation.createHTMLDocument("")
;
if (markup.toLowerCase().indexOf('<!doctype') > -1) {
doc.documentElement.innerHTML = markup;
}
else {
doc.body.innerHTML = markup;
}
return doc;
} else {
return (window as any).DOMParser.prototype.nativeParse.apply(this, arguments);
}
};
(function (DOMParser) {
"use strict";
var proto = DOMParser.prototype,
nativeParse = proto.parseFromString;
try {
if ((new DOMParser()).parseFromString("", "text/html")) {
return;
}
} catch (ex) { }
proto.parseFromString = function (markup, type) {
if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
var
doc = document.implementation.createHTMLDocument("")
;
if (markup.toLowerCase().indexOf('<!doctype') > -1) {
doc.documentElement.innerHTML = markup;
}
else {
doc.body.innerHTML = markup;
}
return doc;
} else {
return nativeParse.apply(this, arguments);
}
};
})(DOMParser)
};
var s;
window.stop = function stop() {
if (s)
debugger;
}
if (envirenment.isChromeApp) {
var str = {};
window.localStorage = <Storage>{
key(i: number) { return Object.keys(str)[i]; },
clear(callback?: (k: string, v: string) => void) {
(chrome.storage.local.clear as any)(callback);
},
getItem(k: string, callback?: (k: string, v: string) => void) {
(chrome.storage.local.get as any)(k, callback);
return str[k];
},
get length() { return Object.keys(str).length; },
setItem(k: string, v: string, callback?: (k: string, v: string) => void) {
str[k] = v;
var x = {}; x[k] = v;
(chrome.storage.local.set as any)(x, callback);
},
removeItem(k: string, callback?: (k: string, v: string) => void) {
delete str[k];
str[k] = void 0;
(chrome.storage.local as any).remove(k, callback);
}
}
}
else if (typeof localStorage === 'undefined') {
var str = {};
window.localStorage = {
setItem: function (a, b) { str[a] = b; },
getItem: function (a) { return str[a]; },
clear: function () { },
key: function (i: number) { return str[i]; },
length: 0,
removeItem: (a) => { }
};
}
})(window);
declare class Map {
new();
get(key): any;
set(key, value);
delete(key);
}
declare var chrome: { storage: { local: { get, set, clear, remove } } };