This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgyfy.js
540 lines (474 loc) · 18.6 KB
/
edgyfy.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// ----------------------------------------------------------------------------------------------------------------- //
// MIT License
//
// Copyright (c) 2018-2019 Hugo Xu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ----------------------------------------------------------------------------------------------------------------- //
// Edgyfy - Shim to make Chromium extensions to run on Edge
//
// https://docs.microsoft.com/en-us/microsoft-edge/extensions/api-support/supported-apis
// ----------------------------------------------------------------------------------------------------------------- //
"use strict";
// ----------------------------------------------------------------------------------------------------------------- //
window.edge = window.chrome || {};
window.chrome = window.browser;
// ----------------------------------------------------------------------------------------------------------------- //
window.elib = {};
window.elib.hardAssert = (test, err) => {
if (!test) {
throw err;
}
};
window.elib.tricheck = (prop) => {
return (
typeof Element.prototype[prop] === "function" &&
typeof Document.prototype[prop] === "function" &&
typeof DocumentFragment.prototype[prop] === "function"
);
};
window.elib.tripatch = (patcher) => {
patcher(Element.prototype);
patcher(Document.prototype);
patcher(DocumentFragment.prototype);
};
// For patching the chrome namespace
window.elib.cpatch = (parent, name, value) => {
Object.defineProperty(parent, name, {
configurable: true,
enumerable: true,
writable: false,
value: value,
});
};
window.elib.unbreak_popup = (extra) => {
const style = document.createElement("style");
style.innerHTML = "body { width: 100%; }";
if (typeof extra === "string")
style.innerHTML = style.innerHTML + " " + extra;
document.documentElement.prepend(style);
};
// ----------------------------------------------------------------------------------------------------------------- //
window.ecfg = {};
window.ecfg.dateStripMarks = true;
window.ecfg.fetchAware = false;
// ----------------------------------------------------------------------------------------------------------------- //
{
const reMarks = /\u200E|\u200F/g;
const _toLocaleString = Date.prototype.toLocaleString;
Date.prototype.toLocaleString = function (...args) {
// "fullwide" throws as of 41
if (args[0] === "fullwide")
args.shift();
let temp = _toLocaleString.apply(this, args);
// Chromium does not insert those marks
if (ecfg.dateStripMarks)
temp = temp.replace(reMarks, "");
return temp;
};
}
try {
const nodes = document.querySelectorAll("html");
// Throws until 40, fixed in 41
for (const node of nodes)
void node;
} catch (err) {
elib.tripatch((ptype) => {
const _querySelectorAll = ptype.querySelectorAll;
ptype.querySelectorAll = function () {
const result = _querySelectorAll.apply(this, arguments);
return Array.from(result);
};
});
}
// Missing as of 41
if (!elib.tricheck("prepend")) {
elib.tripatch((ptype) => {
ptype.prepend = function () {
let docFrag = document.createDocumentFragment();
for (const arg of arguments) {
if (arg instanceof Node) {
docFrag.appendChild(arg);
} else {
docFrag.appendChild(document.createTextNode(String(argItem)));
}
}
this.insertBefore(docFrag, this.firstChild);
};
});
}
// Missing as of 41
if (!elib.tricheck("append")) {
elib.tripatch((ptype) => {
ptype.append = function () {
let docFrag = document.createDocumentFragment();
for (const arg of arguments) {
if (arg instanceof Node) {
docFrag.appendChild(arg);
} else {
docFrag.appendChild(document.createTextNode(String(argItem)));
}
}
this.appendChild(docFrag);
};
});
}
// ----------------------------------------------------------------------------------------------------------------- //
try {
// From health report, this seems to be crashing on Edge 42
// localStorage is not functional for extensions as of 41
const _localStorage = localStorage;
let newLocalStorage = {};
const wrap = (name) => {
return (...args) => {
try {
return _localStorage[name](...args);
} catch (err) {
console.warn("localStorage." + name + ": Crash prevented\n", err);
debugger;
}
};
};
const keys = ["clear", "getItem", "key", "removeItem", "setItem"];
for (const key of keys) {
newLocalStorage[key] = wrap(key);
}
Object.defineProperty(window, "localStorage", {
configurable: true,
enumerable: true,
writable: false,
value: newLocalStorage,
});
} catch (err) {
console.error(err);
debugger;
}
// ----------------------------------------------------------------------------------------------------------------- //
if (!window.requestIdleCallback) {
window.IdleDeadline = class {
constructor(noTimeout) {
this.didTimeout = false;
if (!noTimeout)
this.didTimeout = Math.random() > 0.9;
}
timeRemaining() {
return Math.random() * 10 + 45;
}
};
window.requestIdleCallback = (callback, options) => {
let timeout = 0;
if (options && options.timeout)
timeout = options.timeout;
return setTimeout(callback, timeout, new IdleDeadline(timeout === 0));
};
window.cancelIdleCallback = (handle) => {
clearTimeout(handle);
};
}
// ----------------------------------------------------------------------------------------------------------------- //
if (chrome.tabs && typeof chrome.tabs.reload !== "function") {
const _reload = (tabId, reloadProperties, callback) => {
elib.hardAssert(
typeof tabId === "undefined" || typeof tabId === "number",
"Uncaught TypeError: Invalid type for tabId",
);
elib.hardAssert(
typeof reloadProperties === "undefined" ||
typeof reloadProperties === "object",
"Uncaught TypeError: Invalid type for reloadProperties",
);
elib.hardAssert(
reloadProperties !== null,
"Uncaught TypeError: Invalid type for reloadProperties",
);
if (reloadProperties) {
for (const key in reloadProperties) {
if (reloadProperties.hasOwnProperty(key)) {
switch (key) {
case "bypassCache":
elib.hardAssert(
typeof reloadProperties.bypassCache === "boolean",
"Uncaught TypeError: Invalid type for reloadProperties.bypassCache",
);
break;
default:
elib.hardAssert(
false,
"Uncaught TypeError: Unexpected key in reloadProperties",
);
break;
}
}
}
}
elib.hardAssert(
typeof callback === "undefined" || typeof callback === "function",
"Uncaught TypeError: Invalid type for callback",
);
let bypassCache = reloadProperties && reloadProperties.bypassCache;
bypassCache = String(Boolean(bypassCache));
const details = {
code: ";location.reload(" + bypassCache + ");",
runAt: "document_start",
};
const _callback = (...args) => {
if (args.length) {
console.log("chrome.tabs.reload: Workaround callback arguments discarded\n", args);
}
if (typeof callback === "function") {
return callback();
}
};
if (typeof tabId === "number") {
chrome.tabs.executeScript(tabId, details, _callback);
} else {
chrome.tabs.executeScript(details, _callback);
}
};
elib.cpatch(chrome.tabs, "reload", (...args) => { // Available starting 42
try {
return _reload(...args);
} catch (err) {
console.warn("chrome.tabs.reload: Crash prevented\n", err);
debugger;
}
});
}
if (chrome.tabs && typeof chrome.tabs.executeScript === "function") {
const _executeScript = chrome.tabs.executeScript;
elib.cpatch(chrome.tabs, "executeScript", (tabId, details, callback) => {
try {
_executeScript(tabId, details, callback);
} catch (err) {
console.log("chrome.tabs.executeScript: Ignoring 'frameId'\n", err);
if (typeof tabId === "object") {
delete tabId.frameId;
} else if (typeof details === "object") {
delete details.frameId;
}
try {
_executeScript(tabId, details, callback);
} catch (err) {
// Give up
console.warn("chrome.tabs.executeScript: Crash prevented\n", err);
debugger;
}
}
});
}
if (chrome.browserAction) {
const reIsNumber = /^\d+$/;
const _setIcon = chrome.browserAction.setIcon;
elib.cpatch(chrome.browserAction, "setIcon", (details, callback) => {
let largest = -1;
for (const key in details.path) {
if (reIsNumber.test(key)) {
const current = parseInt(key);
if (isFinite(current) && current > largest) {
largest = current;
}
}
}
if (largest === -1) {
throw new Error("chrome.browserAction.setIcon: No reasonable icon path");
}
const pathToLargest = details.path[String(largest)];
// Edge modifies this object
details.path = {
// Edge does not care if the size is actually right but do care if
// the key name is right
"38": pathToLargest,
};
try {
return _setIcon(details, callback);
} catch (err) {
console.warn("chrome.browserAction.setIcon: Crash prevented\n", err);
debugger;
}
});
}
if (chrome.webRequest) {
elib.cpatch(chrome.webRequest, "ResourceType", {
"MAIN_FRAME": "main_frame",
"SUB_FRAME": "sub_frame",
"STYLESHEET": "stylesheet",
"SCRIPT": "script",
"IMAGE": "image",
// "FONT": "font", // Not available as of 41
"OBJECT": "object",
"XMLHTTPREQUEST": "xmlhttprequest",
"FETCH": "fetch", // Available starting in 41, but not Chromium
"PING": "ping",
// "CSP_REPORT": "csp_report", // Not available as of 41
// "MEDIA": "media", // Not available as of 41
// "WEBSOCKET": "websocket", // Not available as of 41
"OTHER": "other",
});
{
let canFilterFetch = null;
let failCount = 0;
const _addListener = chrome.webRequest.onBeforeRequest.addListener;
elib.cpatch(
chrome.webRequest.onBeforeRequest, "addListener",
(callback, filter, opt_extraInfoSpec) => {
if (canFilterFetch === null) {
const noopfn = () => { };
try {
_addListener(noopfn, {
urls: filter.urls,
types: ["fetch"],
}, opt_extraInfoSpec);
chrome.webRequest.onBeforeRequest.removeListener(noopfn);
canFilterFetch = true;
} catch (err) {
failCount++;
if (failCount > 10) {
canFilterFetch = false;
}
}
}
if (!ecfg.fetchAware) {
if (
canFilterFetch && filter.types &&
filter.types.includes("xmlhttprequest")
) {
filter.types.push("fetch");
}
if (
!filter.types ||
filter.types.includes("xmlhttprequest") ||
filter.types.includes("fetch")
) {
const _callback = callback;
callback = (details) => {
if (details.type === "fetch") {
details.type = "xmlhttprequest";
}
return _callback(details);
};
}
}
try {
return _addListener(callback, filter, opt_extraInfoSpec);
} catch (err) {
console.warn("chrome.webRequest.onBeforeRequest: Crash prevented\n", err);
debugger;
}
},
);
}
{
const _addListener = chrome.webRequest.onBeforeSendHeaders.addListener;
elib.cpatch(
chrome.webRequest.onBeforeSendHeaders, "addListener",
(callback, filter, opt_extraInfoSpec) => {
try {
return _addListener(callback, filter, opt_extraInfoSpec);
} catch (err) {
console.warn("chrome.webRequest.onBeforeSendHeaders: Crash prevented\n", err);
debugger;
}
},
);
}
{
const _addListener = chrome.webRequest.onHeadersReceived.addListener;
const acceptedTypes = new Set(Object.values(chrome.webRequest.ResourceType));
elib.cpatch(
chrome.webRequest.onHeadersReceived, "addListener",
(callback, filter, opt_extraInfoSpec) => {
const types = filter.types;
if (types) {
let i = types.length;
while (i--) {
if (!acceptedTypes.has(types[i])) {
console.warn("chrome.webRequest.onHeadersReceived: Type '" + types[i] + "' Discarded");
types.splice(i, 1);
}
}
}
try {
return _addListener(callback, filter, opt_extraInfoSpec);
} catch (err) {
console.warn("chrome.webRequest.onHeadersReceived: Crash prevented\n", err);
debugger;
}
},
);
}
}
// ----------------------------------------------------------------------------------------------------------------- //
// From: https://github.com/jonathantneal/element-qsa-scope/blob/master/index.js (public domain)
try {
// test for scope support
document.querySelector(':scope *');
} catch (error) {
(function (ElementPrototype) {
// scope regex
var scope = /:scope(?![\w-])/gi;
// polyfill Element#querySelector
var querySelectorWithScope = polyfill(ElementPrototype.querySelector);
ElementPrototype.querySelector = function querySelector(selectors) {
return querySelectorWithScope.apply(this, arguments);
};
// polyfill Element#querySelectorAll
var querySelectorAllWithScope = polyfill(ElementPrototype.querySelectorAll);
ElementPrototype.querySelectorAll = function querySelectorAll(selectors) {
return querySelectorAllWithScope.apply(this, arguments);
};
// polyfill Element#matches
if (ElementPrototype.matches) {
var matchesWithScope = polyfill(ElementPrototype.matches);
ElementPrototype.matches = function matches(selectors) {
return matchesWithScope.apply(this, arguments);
};
}
// polyfill Element#closest
if (ElementPrototype.closest) {
var closestWithScope = polyfill(ElementPrototype.closest);
ElementPrototype.closest = function closest(selectors) {
return closestWithScope.apply(this, arguments);
};
}
function polyfill(qsa) {
return function (selectors) {
// whether the selectors contain :scope
var hasScope = selectors && scope.test(selectors);
if (hasScope) {
// fallback attribute
var attr = 'q' + Math.floor(Math.random() * 9000000) + 1000000;
// replace :scope with the fallback attribute
arguments[0] = selectors.replace(scope, '[' + attr + ']');
// add the fallback attribute
this.setAttribute(attr, '');
// results of the qsa
var elementOrNodeList = qsa.apply(this, arguments);
// remove the fallback attribute
this.removeAttribute(attr);
// return the results of the qsa
return elementOrNodeList;
} else {
// return the results of the qsa
return qsa.apply(this, arguments);
}
};
}
})(Element.prototype);
}
// ----------------------------------------------------------------------------------------------------------------- //