有人有在JavaScript中重写alert()函数的经验吗?
哪些浏览器支持这个功能? 哪些浏览器版本支持此功能? 重写函数的危险是什么?
有人有在JavaScript中重写alert()函数的经验吗?
哪些浏览器支持这个功能? 哪些浏览器版本支持此功能? 重写函数的危险是什么?
当前回答
我做的一个快速破解来找到警报来自哪里,是去控制台,然后输入这个
function alert(message) {
console.info(message);
debugger;
}
其他回答
我重写alert()函数的经验是,我们曾经使用它来“破解”JavaScript库的试用版,该库在每次提醒时都显示“请注册!”的提示屏幕。
我们刚刚定义了自己的alert()函数,瞧。
这只是为了测试,我们后来买了完整版,所以这里没有什么不道德的事情;-)
它绝对是“支持的”。这是你的网页,你想做什么就做什么。
我已经这样做了,在不修改库的情况下跟踪分析事件,但通过潜入事件。
使用代理模式:
(function(proxied) {
window.alert = function() {
// do something here
return proxied.apply(this, arguments);
};
})(window.alert);
如果需要,还可以绕过对原始函数的调用(代理)
更多信息:JQuery类型#代理模式
我做的一个快速破解来找到警报来自哪里,是去控制台,然后输入这个
function alert(message) {
console.info(message);
debugger;
}
我遇到过一个要求,即在显示实际警报消息的同时显示默认消息。这就是我如何做到的。
const actualAlertFunc = window.alert;
window.alert = function(msg) {
actualAlertFunc('some default message '+ msg);
}
我已经在chrome上测试过了。 我不确定这是否是一种好的做法,但它达到了目的。
Ladislav。 对于IE8,你可以像这样重新定义alert()
/**
* Definition of global attached to window properties <br/>
*/
(function() {
nalert = window.alert;
Type = {
native: 'native',
custom: 'custom'
};
})();
/**
* Factory method for calling alert().
* It will be call a native alert() or a custom redefined alert() by a Type param.
* This defeinition need for IE
*/
(function(proxy) {
proxy.alert = function () {
var message = (!arguments[0]) ? 'null': arguments[0];
var type = (!arguments[1]) ? '': arguments[1];
if(type && type == 'native') {
nalert(message);
}
else {
document.write('<h1>I am redefiend alert()<br/>Alert say: '+message+'</h1>');
}
};
})(this);
并调用as
alert('Hello, hacker!');
nalert('I am native alert');
alert('Hello, user!', Type.custom);