7

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?

5
  • 2
    Have you tried to make a js snippet in chrome? You can find the snippets in Chrome Dev Tools -> Sources -> Snippets -> New -> Run. Commented Jul 2, 2015 at 13:49
  • No, I have not. I'm not entirely positive what that is nor is that what I am trying to achieve, I believe. Commented Jul 2, 2015 at 13:55
  • The main thing that you can do with the snippet is to run a random script in a page (whatever page). Give it a try, it may be what you are seeking. Commented Jul 2, 2015 at 13:57
  • I looked into it, and it would be useful, so I appreciate that piece. However, the question still remains as to how to open a .js file from command line and have it run in chrome console/access to it's contents (variables/functions/etc.) in console. Commented Jul 2, 2015 at 14:01
  • 2
    Nice thing the snippet, I didn't know. thanks Stefan. Commented Jul 2, 2015 at 14:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate the suggestion but was consciously avoiding using an HTML doc I am almost entirely positive you can call a javascript file from the command line in such a way that it is opened and run in console.
I really don't think there's a way out of the box. However, you might want to check this stackoverflow.com/questions/10611796/…
I'm afraid you're correct and I must be misremembering my experience of doing so... I will ultimately be wrapping this in an HTML doc and going about it in this way. This is currently the most correct answer. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.