[英]Javascript execute js on a web page such like chrome console
Hi guys i have a question, if i'm on a external web page such as google.com and i want to click on a "whatever" button i open the chrome console and use : 嗨,大家好,我有一个问题,如果我在google.com之类的外部网页上,并且我想单击“任何”按钮,则打开chrome控制台并使用:
$('input[name="whatever"]').click();
and the button get clicked. 然后单击该按钮。 it works.
有用。 there is a way to execute the same command on for instance google.com but not from the console ?
有没有一种方法可以在例如google.com上执行相同的命令,但不能从控制台执行? like open it in a document like window.open("google.com") and click the button the same way on the web page?
像在window.open(“ google.com”)这样的文档中打开它,然后以相同方式在网页上单击该按钮? thk so much :D
非常感谢:D
No, that is not how Javascript is meant to work in the browser. 不,这不是Javascript在浏览器中的工作方式。 Once the browser starts opening a new document, any code that hasn't been executed yet in the old one stops running and a totally new environment is loaded.
浏览器开始打开新文档后,旧版本中尚未执行的所有代码将停止运行,并加载全新的环境。
You can experience it firsthand by first running this code in the console and waiting: 您可以通过首先在控制台中运行以下代码并等待来直接体验它:
window.setTimeout(console.log, 5000, 'hi');
After 5 seconds, the message 'hi' will display. 5秒钟后,将显示消息“ hi”。 Then try this:
然后试试这个:
function test() {
document.location.href = 'http://www.google.com';
window.setTimeout(console.log, 5000, 'ciao');
}
test();
Gooogle will load and no message will be shown in the console. Gooogle将加载,并且控制台中不会显示任何消息。 Basically, your code was aborted and had no access to the new page.
基本上,您的代码已中止并且无法访问新页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.