有没有办法在 JavaScript 中模拟点击警报?

发布于 2024-07-14 01:09:55 字数 141 浏览 5 评论 0原文

我有一个带有 iframe 的页面,其源页面位于单独的域中。 源页面有时会生成警报。 当它这样做时,它会停止正在执行的操作,直到用户单击警报的“确定”。

我想要做的是以编程方式单击此警报上的“确定”,以便源页面可以恢复有用。 这可能吗?

I have a page with an iframe whose source page is in a separate domain. From time to time, the source page generates an alert. When it does so, it stops what it is doing until the user clicks OK to the alert.

What I would like to do is programmatically click OK on this alert so the source page can get back to being useful. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

被你宠の有点坏 2024-07-21 01:09:55

JavaScript 是单线程的,这意味着当您调用函数时,它会阻塞直到返回。 当您调用alert()时,会将控制权传递给浏览器,浏览器决定如何处理它。 弹出 UI 对话框的不是 Javascript,而是浏览器。 在浏览器收到“OK”事件并返回控制之前,alert() 不会返回。 javascript 线程将停止,直到发生这种情况。

因此,出于上一段所述的至少两个不同原因,答案是:)

JavaScript is single-threaded, which means when you call a function, it blocks until it returns. When you call alert(), that passes control to the browser which decides how to handle it. It is not Javascript which is popping the UI dialog, it is the browser. alert() does not return until the browser receives the "OK" event and returns control. The javascript thread is halted until that happens.

So for at least two different reasons stated in the above paragraph, the answer is no :)

烏雲後面有陽光 2024-07-21 01:09:55

我很确定这是不可能的。

I'm pretty sure it's not possible.

万水千山粽是情ミ 2024-07-21 01:09:55

如果您在代码中使用空操作覆盖警报,会发生什么情况?

例如,

<script>
// uncomment next line to swallow alerts
//  function alert(nope) { return; }
<script>
<script>
  $(document).ready(function() { alert("Hello, world!"); });
</script>

当该行被注释掉时,会出现警报。 注释行后,不会出现警报。

What happens if you override alert with a no-op in your code?

e.g.

<script>
// uncomment next line to swallow alerts
//  function alert(nope) { return; }
<script>
<script>
  $(document).ready(function() { alert("Hello, world!"); });
</script>

With the line commented out, alert appears. With line commented in, alert does not appear.

只是偏爱你 2024-07-21 01:09:55

如果您知道警报的文本并且不介意弄脏,那么您应该不会有问题。

覆盖 iframe 窗口的警报,并执行一些操作以达到以下效果。

document.getElementById("InnerFrameName").contentWindow.alert = function(){
    if (arguments[0].toLowerCase() == "innerframe alert text")
        return; //supress
    else
        alert(arguments[0]);
};

编辑:我使用代理处理程序做了一个小测试(它位于 asp.net/c# 中,但您应该明白)...

您可以用自己的方法 我的测试中的页面是一个带有显示警报的按钮的 w3 页面,警报现在通过用户定义的函数传递。

我确信这比你想要的要脏得多。

页面:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Hello From MyPage</title>
    <script type="text/javascript">
        function frameLoaded() {
            document.getElementById("InnerFrameName").contentWindow.alert = function() {
                if (arguments[0].toLowerCase() == "i am an alert box!!") {
                    alert("MESSAGES WOULD BE SUPRESSED: " + arguments[0]);
                    return; //supress
                } else {
                    alert(arguments[0]);
                }
            };
        }
        </script>
</head>
<body onload="alert('Hello from an unsupressed top level frame message!');">
    <form id="form1" runat="server">
            <div>
                <h1>Internal Page (Top Level)</h1>
                <hr />
                <iframe id="InnerFrameName" onload="frameLoaded();" src="PageProxy.ashx?externalURI=http://www.w3schools.com/JS/tryit_view.asp?filename=tryjs_alert"
                    style="width: 800px; height: 600px;">Derp!...</iframe>
            </div>
    </form>
</body>
</html>

代理处理程序:

<%@ WebHandler Language="C#" Class="PageProxy" %>

using System;
using System.Web;

public class PageProxy : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
            byte[] externalPage = new System.Net.WebClient().DownloadData(context.Request["externalURI"]);
            context.Response.OutputStream.Write(externalPage, 0, externalPage.Length);
            context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

You shouldn't have a problem if you know the text of the alert and don't mind getting dirty.

You can override the iframe window's alert with your own and do something to the effect of..

document.getElementById("InnerFrameName").contentWindow.alert = function(){
    if (arguments[0].toLowerCase() == "innerframe alert text")
        return; //supress
    else
        alert(arguments[0]);
};

Edit: I made a little test with a proxy handler (it's in asp.net/c# but you should get the idea)...

The external page in my test is a w3 page with a button that displays an alert, the alert is now passed through the user defined function.

I'm sure this is alot more dirty then you'd like to get.

Page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Hello From MyPage</title>
    <script type="text/javascript">
        function frameLoaded() {
            document.getElementById("InnerFrameName").contentWindow.alert = function() {
                if (arguments[0].toLowerCase() == "i am an alert box!!") {
                    alert("MESSAGES WOULD BE SUPRESSED: " + arguments[0]);
                    return; //supress
                } else {
                    alert(arguments[0]);
                }
            };
        }
        </script>
</head>
<body onload="alert('Hello from an unsupressed top level frame message!');">
    <form id="form1" runat="server">
            <div>
                <h1>Internal Page (Top Level)</h1>
                <hr />
                <iframe id="InnerFrameName" onload="frameLoaded();" src="PageProxy.ashx?externalURI=http://www.w3schools.com/JS/tryit_view.asp?filename=tryjs_alert"
                    style="width: 800px; height: 600px;">Derp!...</iframe>
            </div>
    </form>
</body>
</html>

Proxy Handler:

<%@ WebHandler Language="C#" Class="PageProxy" %>

using System;
using System.Web;

public class PageProxy : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
            byte[] externalPage = new System.Net.WebClient().DownloadData(context.Request["externalURI"]);
            context.Response.OutputStream.Write(externalPage, 0, externalPage.Length);
            context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}
极致的悲 2024-07-21 01:09:55

我非常怀疑。 如果通过单独的域,您是指您无法控制的域吗?

如果您确实有控制权,则可以将警报更改为您可以控制的模态 Javascript 框。

I highly doubt it. If by separate domain, do you mean one which you have no control over?

If you do have control, you could change the alerts to modal Javascript boxes, which you could control.

三生池水覆流年 2024-07-21 01:09:55

解决烦人的 JavaScript 问题的最佳选择是使用客户端脚本工具覆盖它,例如 Greasemonkey

The best bet to solve your problems with annoying JavaScript would be to override it with a client side scripting tool like Greasemonkey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文