简体   繁体   English

Javascript在chrome控制台等网页上执行js

[英]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.

相关问题 从chrome扩展内容脚本执行网页js - Execute web page js from chrome extension content script 如何在Visual Studio Code预览侧打开Web控制台(例如Chrome控制台)并动态查看JavaScript更改? - How to open web console like Chrome console in Visual Studio Code preview side and see JavaScript changes dynamicaly? 无法从Chrome控制台执行Javascript - Cannot execute Javascript from Chrome console 如何在Chrome控制台中执行Javascript代码? - How to execute Javascript code in Chrome console? 当浏览器进入特定网页时,是否可以通过浏览器的JavaScript控制台自动执行预定义的JavaScript? - is there a way to auto execute a predefined JavaScript through the JavaScript console of a browser when it enters a specific web page? 在Chrome等浏览器中停止控制台javascript命令 - Stop Console javascript commands in browser like chrome 在Javascript控制台中控制Chrome Web Inspector? - Control Chrome Web Inspector in Javascript console? 用于控制台的Javascript资源,如Web应用程序? - Javascript resources for console like web applications? 提交页面后,Chrome JavaScript控制台为空 - Chrome JavaScript console is null after submitting the page 如何访问网页源代码(包括chrome的CSS和JavaScript)检查元素选项呢? - How to access web page source code including CSS and JavaScript like chrome inspect element option does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM