简体   繁体   English

如何在类似页面上运行javascript?

[英]How can I run javascript on similar pages?

I want to use chrome snippet to run a javascript code to autofill a series of pages. 我想使用chrome片段来运行javascript代码以自动填充一系列页面。

The problem is after another page was opened, the code seems stopped. 问题是打开另一个页面后,代码似乎停止了。

Instead, I need it run continually. 相反,我需要它连续运行。 Like this: 像这样:

 //Page 1 form
 document.getElementById('page1Radiobutton').click()
 //Click Next
 document.getElementById('page1Next').click()
 //Page 2 form (need run continually after page 1 was submitted, but now stop at here...)
 document.getElementById('page2Radiobutton').click()
 document.getElementById('page2Next').click()

Your JavaScript can't really run across pages. 您的JavaScript不能真正页面运行。 Once the user navigates to a new page, all JavaScript is thrown out by the browser for security (among other) reasons. 一旦用户导航到新页面,出于安全性(以及其他原因),浏览器将抛出所有JavaScript。

I would look into browser extensions such as Tampermonkey (for Chrome) or Greasemonkey (for Firefox) 我会研究浏览器扩展程序,例如Tampermonkey(对于Chrome)Greasemonkey(对于Firefox)

With either of these monkeys, you can create snippets of arbitrary javascript that will run on any page on the internet automatically. 使用这些猴子中的任何一个,您都可以创建任意javascript的代码段,这些代码段将自动在互联网上的任何页面上运行。

If you are going to create a script that runs on every page, make sure it exits gracefully if it can't find the elements you're looking for. 如果要创建在每个页面上运行的脚本,请确保在找不到所需元素的情况下正常退出。 Otherwise you'll end up with javascript errors when you go to other pages. 否则,当您转到其他页面时,将最终遇到javascript错误。

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

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