简体   繁体   English

有没有办法从浏览器外运行的程​​序触发 DOM 事件?

[英]Is there a way to trigger DOM events from a program running outside the browser?

I have my own web app running in a browser.我在浏览器中运行了自己的网络应用程序。 I want to trigger DOM events from a program running outside the browser.我想从在浏览器外运行的程​​序触发 DOM 事件。 What is the best way to setup this interaction?设置此交互的最佳方法是什么? Is my best bet to send keystrokes to the browser and handling these in my web app?将击键发送到浏览器并在我的网络应用程序中处理这些是我最好的选择吗?

I prefer a generic solution, but if certain browsers (eg Chrome) have a specific facilities for this, I'm also interested in finding out.我更喜欢通用的解决方案,但如果某些浏览器(例如 Chrome)对此有特定的功能,我也有兴趣找出答案。

  1. To simulate events from javascript use eg the Event.simulate library .要模拟来自 javascript 的事件,请使用例如 Event.simulate 库
  2. To trigger the simulation from outside the browser generically have your javascript use AJAX or COMET to poll/be notified by your external application .要从浏览器外部触发模拟,通常让您的 javascript 使用 AJAX 或 COMET 轮询/由您的外部应用程序通知

You will run into tons of hassle, cross-browser problems, and security issues trying to send keystrokes from an app into the browser.尝试将击键从应用程序发送到浏览器时,您会遇到大量麻烦、跨浏览器问题和安全问题。 I would not suggest trying it.我不建议尝试。

Your best bet would be to use something like AJAX polling or, better yet(if possible), sockets and setup your two apps to communicate with eachother via some type of service layer/API.你最好的选择是使用 AJAX 轮询之类的东西,或者更好(如果可能),套接字并设置你的两个应用程序通过某种类型的服务层/API 相互通信。

Check out socket.io for a nice way to do it on the web app side, the setup for your other app will depend on the language it is written in.查看 socket.io 以获得在 Web 应用程序端执行此操作的好方法,其他应用程序的设置将取决于它所用的语言。

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

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