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?

Lowell Mower
  • 313
  • 4
  • 14
  • 1
    Have you tried to make a js snippet in chrome? You can find the snippets in Chrome Dev Tools -> Sources -> Snippets -> New -> Run. – Stefan Perju Jul 02 '15 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. – Lowell Mower Jul 02 '15 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. – Stefan Perju Jul 02 '15 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. – Lowell Mower Jul 02 '15 at 14:01
  • 2
    Nice thing the snippet, I didn't know. thanks Stefan. – borja gómez Jul 02 '15 at 14:03
  • @borjagómez Glad it helped. – Stefan Perju Jul 03 '15 at 06:54

1 Answers1

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.

marekful
  • 14,986
  • 6
  • 37
  • 59
  • 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. – Lowell Mower Jul 02 '15 at 13:54
  • I really don't think there's a way out of the box. However, you might want to check this http://stackoverflow.com/questions/10611796/is-there-any-way-to-load-a-local-js-file-dynamically?answertab=active#tab-top – marekful Jul 02 '15 at 14:18
  • 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! – Lowell Mower Jul 02 '15 at 14:28