Running a JavaScript file in Chrome

只愿长相守 提交于 2020-01-13 10:42:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31186089/running-a-javascript-file-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!