Skip to content

Instantly share code, notes, and snippets.

@AngusFu
Last active September 9, 2021 12:59
Show Gist options
  • Save AngusFu/ef2280dcbfa737204f85ae628834bbfb to your computer and use it in GitHub Desktop.
Save AngusFu/ef2280dcbfa737204f85ae628834bbfb to your computer and use it in GitHub Desktop.
只读克星:前端眼里,没有「read only」文档这回事
// 粘贴到 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