forked from ScratchAddons/ScratchAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype-handler.js
More file actions
30 lines (28 loc) · 1.15 KB
/
Copy pathprototype-handler.js
File metadata and controls
30 lines (28 loc) · 1.15 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
function injectPrototype() {
const oldBind = Function.prototype.bind;
// Use custom event target
window.__scratchAddonsTraps = new EventTarget();
const onceMap = (__scratchAddonsTraps._onceMap = Object.create(null));
Function.prototype.bind = function (...args) {
if (Function.prototype.bind === oldBind) {
// Just in case some code stores the bind function once on startup, then always uses it.
return oldBind.apply(this, args);
} else if (
args[0] &&
Object.prototype.hasOwnProperty.call(args[0], "editingTarget") &&
Object.prototype.hasOwnProperty.call(args[0], "runtime")
) {
onceMap.vm = args[0];
// After finding the VM, return to previous Function.prototype.bind
Function.prototype.bind = oldBind;
return oldBind.apply(this, args);
} else {
return oldBind.apply(this, args);
}
};
}
if (location.pathname.split("/")[1] === "projects") {
const injectPrototypeScript = document.createElement("script");
injectPrototypeScript.append(document.createTextNode("(" + injectPrototype + ")()"));
(document.head || document.documentElement).appendChild(injectPrototypeScript);
}