-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDOM - Allow copy paste.js
More file actions
44 lines (44 loc) · 1.3 KB
/
DOM - Allow copy paste.js
File metadata and controls
44 lines (44 loc) · 1.3 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
const blockedEvents = [
"copy",
"paste",
"cut",
"selectstart",
"contextmenu",
"dragstart",
"select",
];
const _addEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (type, fn, options) {
if (blockedEvents.includes(type)) return;
_addEventListener.call(this, type, fn, options);
};
document.querySelectorAll("*").forEach((el) => {
const clone = el.cloneNode(true);
el.parentNode?.replaceChild(clone, el);
blockedEvents.forEach((evt) => {
clone.removeAttribute(`on${evt}`);
clone[`on${evt}`] = null;
});
});
blockedEvents.forEach((evt) => {
document.addEventListener(evt, (e) => e.stopImmediatePropagation(), true);
document[`on${evt}`] = null;
});
const style = document.createElement("style");
style.textContent = `
* {
-webkit-user-select: text !important;
-moz-user-select: text !important;
-ms-user-select: text !important;
user-select: text !important;
pointer-events: auto !important;
}
`;
document.head.appendChild(style);
const _dispatchEvent = EventTarget.prototype.dispatchEvent;
EventTarget.prototype.dispatchEvent = function (e) {
if (blockedEvents.includes(e.type) && e.cancelable) {
Object.defineProperty(e, "defaultPrevented", { get: () => false });
}
return _dispatchEvent.call(this, e);
};