有没有办法在 JavaScript 中模拟点击警报?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
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 :)
我很确定这是不可能的。
I'm pretty sure it's not possible.
如果您在代码中使用空操作覆盖警报,会发生什么情况?
例如,
当该行被注释掉时,会出现警报。 注释行后,不会出现警报。
What happens if you override alert with a no-op in your code?
e.g.
With the line commented out, alert appears. With line commented in, alert does not appear.
如果您知道警报的文本并且不介意弄脏,那么您应该不会有问题。
覆盖 iframe 窗口的警报,并执行一些操作以达到以下效果。
编辑:我使用代理处理程序做了一个小测试(它位于 asp.net/c# 中,但您应该明白)...
您可以用自己的方法 我的测试中的页面是一个带有显示警报的按钮的 w3 页面,警报现在通过用户定义的函数传递。
我确信这比你想要的要脏得多。
页面:
代理处理程序:
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..
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:
Proxy Handler:
我非常怀疑。 如果通过单独的域,您是指您无法控制的域吗?
如果您确实有控制权,则可以将警报更改为您可以控制的模态 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.
解决烦人的 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.