|
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; |
6 | 27 | } |
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 }); |
9 | 34 | } |
10 | | -} |
| 35 | + return _dispatchEvent.call(this, e); |
| 36 | +}; |
0 commit comments