Greasy Fork is available in English.
Block alert/confirm/prompt and neutralize common DevTools detection
// ==UserScript==
// @license MIT
// @name alert强力禁用
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Block alert/confirm/prompt and neutralize common DevTools detection
// @author You
// @match *://*/*
// @exclude *://localhost/*
// @exclude *://127.0.0.1/*
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
(function () {
'use strict';
const w = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
const noop = function () {};
const returnTrue = function () { return true; };
const returnNull = function () { return null; };
function defineSafe(obj, key, value) {
try {
Object.defineProperty(obj, key, {
configurable: false,
enumerable: false,
writable: false,
value
});
} catch (e) {
try {
obj[key] = value;
} catch (_) {}
}
}
function patchWin(target) {
if (!target) return;
defineSafe(target, 'alert', noop);
defineSafe(target, 'confirm', returnTrue);
defineSafe(target, 'prompt', returnNull);
try { target.alert = noop; } catch (e) {}
try { target.confirm = returnTrue; } catch (e) {}
try { target.prompt = returnNull; } catch (e) {}
try { target.open = noop; } catch (e) {}
try {
target.onbeforeunload = null;
} catch (e) {}
}
patchWin(window);
patchWin(w);
try {
if (window.top) patchWin(window.top);
} catch (e) {}
try {
if (window.parent) patchWin(window.parent);
} catch (e) {}
const stopEvt = (e) => {
e.stopImmediatePropagation();
};
window.addEventListener('keydown', stopEvt, true);
window.addEventListener('keyup', stopEvt, true);
window.addEventListener('keypress', stopEvt, true);
window.addEventListener('contextmenu', stopEvt, true);
// 干掉常见反调试:debugger 死循环、定时检测
try {
const rawSetInterval = window.setInterval;
const rawSetTimeout = window.setTimeout;
window.setInterval = function (fn, delay, ...args) {
if (typeof fn === 'function') {
const txt = Function.prototype.toString.call(fn);
if (/debugger|devtools|toString|outerWidth|outerHeight/i.test(txt)) {
return 0;
}
}
if (typeof fn === 'string' && /debugger|devtools/i.test(fn)) {
return 0;
}
return rawSetInterval.call(this, fn, delay, ...args);
};
window.setTimeout = function (fn, delay, ...args) {
if (typeof fn === 'function') {
const txt = Function.prototype.toString.call(fn);
if (/debugger|devtools|toString|outerWidth|outerHeight/i.test(txt)) {
return 0;
}
}
if (typeof fn === 'string' && /debugger|devtools/i.test(fn)) {
return 0;
}
return rawSetTimeout.call(this, fn, delay, ...args);
};
} catch (e) {}
// 持续补丁,防止页面恢复 alert
const reinforce = () => {
patchWin(window);
patchWin(w);
try { if (window.top) patchWin(window.top); } catch (e) {}
try { if (window.parent) patchWin(window.parent); } catch (e) {}
try {
document.querySelectorAll('iframe').forEach((iframe) => {
try {
patchWin(iframe.contentWindow);
} catch (e) {}
});
} catch (e) {}
};
reinforce();
setInterval(reinforce, 500);
// 页面加载后再补一刀
document.addEventListener('DOMContentLoaded', reinforce, true);
window.addEventListener('load', reinforce, true);
})();