在Chrome中运行JavaScript文件 [英] Running a JavaScript file in Chrome

查看:292
本文介绍了在Chrome中运行JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在chrome控制台中运行本地javascript文件,但不想复制并粘贴代码。我知道有一个命令可以从终端运行,以在新的chrome窗口/标签页中打开文件,但无法调用它,也无法在线找到它的示例。



我可以从终端调用什么命令以在新的Chrome标签中打开.js文件,然后可以在控制台中运行它?

解决方案

我认为您无法做到这一点。要在可以访问控制台的情况下运行JS,可以将其源代码复制到控制台或地址/位置栏( javascript :(此处为js代码))。 / p>

我能想到的最简单的方法是将JS包装在HTML文件中,并使用 chrome -u文件调用chrome:/ //path/to/file.html 。在 src 属性的< script> 标记中还包含file:///协议的JS,指向本地文件或使用相对路径。



打开chrome时,您应该可以访问代码中定义为全局变量的函数/变量。


I would like to run a local javascript file in chrome console but do not want to copy and paste the code. I know there is a command I can run from terminal to open the file in a new chrome window/tab but cannot recall it nor find example of it on-line.

What command can I call from the terminal to open a .js file in a new chrome tab where I can then run it in console?

解决方案

I don't think you can do exactly that. To run JS with having access to the console, you can either copy its source to the console or to the address/location bar (javascript:(js code here)).

The simplest way I can think of to achieve your goal is to wrap the JS in an HTML file and call chrome with chrome -u file:///path/to/file.html. Include the JS in a <script> tag also with the file:/// protocol in the src attribute to point to the local file or using a relative path.

When chrome opens, you should have access to functions/variables defined in your code as global variables.

这篇关于在Chrome中运行JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆