forked from philc/vimium
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinjected.js
More file actions
58 lines (51 loc) · 2.37 KB
/
injected.js
File metadata and controls
58 lines (51 loc) · 2.37 KB
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
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* DS209: Avoid top-level return
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
// NOTE(smblott) Disabled pending resolution of #2997.
return;
// The code in `injectedCode()`, below, is injected into the page's own execution context.
//
// This is based on method 2b here: http://stackoverflow.com/a/9517879, and
// @mrmr1993's #1167.
const injectedCode = function() {
// Note the presence of "click" listeners installed with `addEventListener()` (for link hints).
const _addEventListener = EventTarget.prototype.addEventListener;
const _toString = Function.prototype.toString;
// Note some pages may override Element (see https://github.com/gdh1995/vimium-plus/issues/11)
const EL = typeof Element === "function" ? Element : HTMLElement;
const Anchor = HTMLAnchorElement;
const addEventListener = function(type, listener, useCapture) {
if ((type === "click") && this instanceof EL) {
if (!(this instanceof Anchor)) { // Just skip <a>.
try { this.setAttribute("_vimium-has-onclick-listener", ""); } catch (error) {}
}
}
return (_addEventListener != null ? _addEventListener.apply(this, arguments) : undefined);
};
var newToString = function() {
const real = (() => {
if (this === newToString) { return _toString; } else {
if (this === addEventListener) { _addEventListener; } else {}
return this;
}
})();
return _toString.apply(real, arguments);
};
EventTarget.prototype.addEventListener = addEventListener;
// Libraries like Angular/Zone and CKEditor check if element.addEventListener is native,
// so here we hook it to tell outsides it is exactly native.
// This idea is from https://github.com/angular/zone.js/pull/686,
// and see more discussions in https://github.com/ckeditor/ckeditor5-build-classic/issues/34.
return Function.prototype.toString = newToString;
};
const script = document.createElement("script");
script.textContent = `(${injectedCode.toString()})()`;
(document.head || document.documentElement).appendChild(script);
script.remove();