Home > Back-end >  Bash (MacOS): Redirect input/output from Chrome app
Bash (MacOS): Redirect input/output from Chrome app

Time:01-16

I need redirect (for example to file) input and output from terminal Chrome app, and standard methods are not working (>, >>, $>) or I use them wrong?

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--headless --disable-gpu --repl https://www.google.com > ./output.txt

(When I succeed, I will be able to continue working in the Node.js app. Please don't recommend me test frameworks :-) )

Thanks in advance for all the solutions.

Edit:

Disposable answer:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--headless --disable-gpu --repl https://www.google.com \
<<< $( echo 123 ) > stdout.txt

or

echo 123 \
| /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--headless --disable-gpu --repl https://www.google.com \
| tee stdout.txt

Stream answer:

mkfifo std.in
mkfifo std.out
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--headless --disable-gpu --repl https://www.google.com \
< std.in > std.out

CodePudding user response:

I think you are just missing a switch (or more). On windows - cygwin I have to do the following:

$ chrome --headless --disable-gpu --print-to-pdf="c:\temp\out.pdf" --enable-logging=stderr  https://www.google.com
[0115/183433.009:INFO:headless_shell.cc(223)] 121086 bytes written to file c:\temp\out.pdf

At least now I get console logging on stdout (with another page: google.com doesn't use console.log), and an "out.pdf" dump of the requested page.
To do input one can do the following:

 chrome --headless --disable-gpu --print-to-pdf="c:\temp\out.pdf" --enable-logging=stderr --repl https://www.google.com <<< $(echo -e "i=1;\ni=i 1;\nquit")
[0115/184655.533:INFO:headless_shell.cc(476)] Type a Javascript expression to evaluate or "quit" to exit.
>>> {"id":2,"result":{"result":{"description":"1","type":"number","value":1}}}
>>> {"id":3,"result":{"result":{"description":"2","type":"number","value":2}}}
>>>

To read input from a file replace "<<< $(...)" at the end of the line with "< yourfile". "yourfile" should contain some javascript.

Mind you, just to be sure, that --headless doesn't make it a terminal app, it just makes it invisible. So no web output is to be expected on the terminal. But if you just want to capture the above '{"id":3 ...}' lines just combine the two. So (see the end of the line):

 chrome --headless --disable-gpu --print-to-pdf="c:\temp\out.pdf" --enable-logging=stderr --repl https://www.google.com <<< $(echo -e "i=1;\nprint(i);\nquit") > c:/temp/out.log

p.s. You have to replace the windows file paths, with Mac ones i.e. real unix paths.

Hope this helps!

  • Related