简体   繁体   中英

Running a JavaScript file in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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