Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
Lowell Mower Avatar asked Nov 09 '22 10:11

Lowell Mower


1 Answers

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.

like image 99
marekful Avatar answered Nov 14 '22 22:11

marekful