Last active
March 23, 2025 02:44
-
-
Save AngusFu/ef2280dcbfa737204f85ae628834bbfb to your computer and use it in GitHub Desktop.
只读克星:前端眼里,没有「read only」文档这回事
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 粘贴到 chrome devtool console 中执行 | |
// 使用场景 | |
// 1. 想复制只读文档的内容 | |
// 2. 担心复制内容的行为被公司监听了 | |
function removeAllEvents(event) { | |
function removeEvent(el, type) { | |
const listeners = (getEventListeners(el)[type] || []); | |
el[`on${type}`.toLowerCase()] = null; | |
listeners.forEach(({ type, listener, ...opts }) => { | |
el.removeEventListener(type, listener); | |
el.removeEventListener(type, listener, true); | |
el.removeEventListener(type, listener, opts); | |
}); | |
} | |
const events = event.split(/\s+/); | |
const remove = (el) => events.forEach(e => removeEvent(el, e)); | |
remove(window); | |
remove(document); | |
document.querySelectorAll('*').forEach(remove); | |
document.querySelectorAll('iframe').forEach(el => { | |
try { | |
remove(el.contentWindow); | |
remove(el.contentDocument); | |
el.contentDocument.querySelectorAll('*').forEach(remove); | |
} catch (e) {} | |
}); | |
} | |
removeAllEvents('copy keyup keydown keypress select selectstart selectend selectionchange') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment