简体   繁体   English

如何打开网页并在其上运行JavaScript?

[英]How to open a webpage and run your JavaScript on it?

I am trying to load a webpage then insert my own Javascript into it. 我正在尝试加载网页,然后将自己的Javascript插入其中。

I have the current code here: 我在这里有当前代码:

window.location.assign('http://http://79.170.44.75/hostdoctordemo.co.uk/downloads/vpn/index.php');

document.getElementById('address_box').value = prompt("Site Address: ");

document.getElementById('go').click();

and what I am trying to do is: 我想做的是:

  1. Load the webpage 载入网页
  2. Set the address box to a value 将地址框设置为一个值
  3. Simulate a mouse click on the search button 模拟鼠标单击搜索按钮

So it loads the webpage, then searches a value it sets itself. 因此,它会加载网页,然后搜索自己设置的值。

The problem with my current JavaScript is that as soon as the webpage has loaded the JavaScript stops working (as I expected). 我当前的JavaScript的问题在于,网页加载后,JavaScript就会停止工作(正如我所期望的那样)。 I have tried using the iframe tag to load the webpage 'within the webpage' but that did not work when obtaining the id and people said iframe would also not work because of the resolution difference. 我尝试使用iframe标记在“网页内”加载网页,但在获取ID时不起作用,人们说iframe由于分辨率差异也无法正常工作。

**The Question: ** How do I load a webpage and run my own JavaScript code on it? **问题:**如何加载网页并在其上运行自己的JavaScript代码? Thank you! 谢谢!

  • Matthew 马修

I really can't see an easy way to do what you want. 我真的看不到一种简单的方法来做你想要的。 When the browser receives a web page from a server the javascript is interpreted, and only after that, the page is presented on the screen. 当浏览器从服务器接收到网页时,将解释javascript,只有在此之后,页面才会显示在屏幕上。

So you would have to have a web page with a button or other mechanism to make a request to a web server, receive the request, save its contents locally, add your javascript code and only then "give it to the browser". 因此,您将必须具有一个带有按钮或其他机制的网页才能向Web服务器发出请求,接收请求,在本地保存其内容,添加JavaScript代码,然后才“将其提供给浏览器”。

您可能正在寻找类似Greasemonkey的东西。

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

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