简体   繁体   English

如何使用javascript阅读警报消息?

[英]How to read alert message with javascript?

There is website that alerts you with a text according to what you have done in the page. 有一个网站会根据您在页面中所做的操作用文本提醒您。

I want to read that message with JavaScript so I can write some code according to what the page have shown in the popup text. 我想用JavaScript阅读该消息,因此我可以根据页面在弹出文本中显示的内容编写一些代码。

alert("message");

I just need to know what the "message" is! 我只需要知道“消息”是什么!

The website that I'm trying to get message from is coded with asp.net.What should I do with that if it's impossible to read the message with JS. 我试图从中获取消息的网站是用asp.net编码的。如果无法用JS读取消息,该怎么办。

alert() is a global function, ie window.alert() so can be overwritten. alert()是一个全局函数,即window.alert()因此可以覆盖。

Most likely, you'll still want the alert, so you can keep a record of it before overwriting, giving: 最有可能的是,您仍然需要警报,因此您可以在覆盖之前对其进行记录,并给出:

window.old_alert = window.alert;
window.alert = function(msg) { 
    // Process the msg here
    console.log(msg); 

    // still show the original alert
    old_alert(msg); 
};

alert() function when executed is passed to the Browser to execute. alert()函数在执行时传递给浏览器以执行。 Each browser executes it in its own way. 每个浏览器都以自己的方式执行它。 So a way around is to override the alert() function itself. 因此,一种解决方法是重写alert()函数本身。

Some javascript code on the page might be calling the alert() function. 页面上的某些javascript代码可能正在调用alert()函数。 Maybe you can try to find the place in the code where it is called. 也许您可以尝试在代码中找到该位置。 The argument to alert() is what you need. 您需要的是alert()的参数。 You can override the default alert function using your own as described in: JavaScript: Overriding alert() . 您可以使用自己的方法来覆盖默认警报功能,如下所述: JavaScript:覆盖alert() So you can do (as taken from the above answer): 因此,您可以执行以下操作(从以上答案中获取):

(function(proxied) {
  window.alert = function() {
    // do something here
    // arguments is what holds what you want.
    return proxied.apply(this, arguments);
  };
})(window.alert);

@freedomn-m's answer is more relevant and apt. @freedomn -m的答案更相关且更恰当。 But you can use the answer for overriding alert() for more examples on how to do it. 但是您可以使用答案来覆盖alert()以获得有关如何执行此操作的更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM