Skip to content

Commit 16d6509

Browse files
committed
feat(copypaste): improved implementation
1 parent c0beaf6 commit 16d6509

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1-
const e = document.getElementsByTagName("*");
2-
let i;
3-
for (i = 0; i < e.length; i += 1) {
4-
if (e[i].onCopy) {
5-
e[i].onCopy = null;
1+
const blockedEvents = ['copy', 'paste', 'cut', 'selectstart', 'contextmenu', 'dragstart', 'select'];
2+
const _addEventListener = EventTarget.prototype.addEventListener;
3+
EventTarget.prototype.addEventListener = function(type, fn, options) {
4+
if (blockedEvents.includes(type)) return;
5+
_addEventListener.call(this, type, fn, options);
6+
};
7+
document.querySelectorAll('*').forEach(el => {
8+
const clone = el.cloneNode(true);
9+
el.parentNode && el.parentNode.replaceChild(clone, el);
10+
blockedEvents.forEach(evt => {
11+
clone.removeAttribute('on' + evt);
12+
clone[`on${evt}`] = null;
13+
});
14+
});
15+
blockedEvents.forEach(evt => {
16+
document.addEventListener(evt, e => e.stopImmediatePropagation(), true);
17+
document[`on${evt}`] = null;
18+
});
19+
const style = document.createElement('style');
20+
style.textContent = `
21+
* {
22+
-webkit-user-select: text !important;
23+
-moz-user-select: text !important;
24+
-ms-user-select: text !important;
25+
user-select: text !important;
26+
pointer-events: auto !important;
627
}
7-
if (e[i].onPaste) {
8-
e[i].onPaste = null;
28+
`;
29+
document.head.appendChild(style);
30+
const _dispatchEvent = EventTarget.prototype.dispatchEvent;
31+
EventTarget.prototype.dispatchEvent = function(e) {
32+
if (blockedEvents.includes(e.type) && e.cancelable) {
33+
Object.defineProperty(e, 'defaultPrevented', { get: () => false });
934
}
10-
}
35+
return _dispatchEvent.call(this, e);
36+
};

javascripts/DOM - Allow right-click.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

javascripts/index.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
},
1818
{
1919
"name": "DOM - Allow copy paste",
20-
"description": "This bookmarklet reenables copy/paste on all elements in the page (typically a text field or text area)."
21-
},
22-
{
23-
"name": "DOM - Allow right-click",
24-
"description": "This bookmarklet reenables the right-click context menu on a page."
20+
"description": "This bookmarklet reenables copy/paste, selection, right-click, etc. on all elements in the page (typically a text field or text area)."
2521
},
2622
{
2723
"name": "DOM - Allow save password",

0 commit comments

Comments
 (0)