I am attempting to launch a new window of Chrome with an exact viewport size. I'm using this command on Windows command line:
chrome --window-size="500,500" http://whatismyviewport.com
This is the result, which is not 500x500 dimensions:
Here is more about this version of Chrome (from chrome://version):
How can I consistently, reliably get the viewport height & width supplied?
The window-size argument has never worked, at least as far back as 2018 when I wrote a script to open Chrome in app mode for taking screenshots. I had a hacky workaround in place but that also broke recently.
Maybe somebody can fix my hack?
If run without the url="data:text/html part commented out Chrome just exits now.
https://gist.github.com/duffyjp/13a8f7a40cb4e43a240811c64235b25b
#!/bin/bash
url=${1:-http://localhost:3000}
# FYI: If your URL has an & you need to pass the argument in quotes.
# Chrome app mode doesn't work without the http/https part.
if [[ $url = *"http"* ]]; then
echo $url
else
url="http://${url}"
echo $url
fi
# Hack to resize the window to 1024x768.
# url="data:text/html,<script>window.moveTo(0,0);window.resizeTo(1024,768);window.location='${url}';</script>"
# Launch Chrome from one of it's usual locations.
if [ -e "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app=$url 2>/dev/null
elif [ -e "~/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
~/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app=$url 2>/dev/null
else
/usr/bin/google-chrome --app=$url 2>/dev/null
fi
Related
I have a shell script that runs chromium-browser headless, using it to print html into a pdf file.
$ chromium-browser --headless --disable-gpu \
--print-to-pdf=/home/www-data/build/out.pdf \
/home/www-data/build/out.html
If there are any errors with the page (anything from Javascript errors to unavailable resources), I would like my shell script to behave accordingly. But how do I even tell if such errors occur?
Below is a screenshot from Chromium, where an environment variable was not set, and therefore the path for the CSS files is incorrect.
I have tried in various ways to get this information out of chrome headless, but without luck.
My best suggestion is to look for it in the log in userdata, but even with the highest logging level, I don't see that there is anything about not being able to log the resources. All I can find about loading those resources is shown below:
$ rm /home/www-data/userdata -rf
$ chromium-browser --headless --disable-gpu \
--enable-logging
--v=4 \
--user-data-dir=/home/www-data/userdata \
--print-to-pdf=/home/www-data/build/out.pdf \
/home/www-data/build/out.html
$ grep css userdata -r
userdata/Default/chrome_debug.log:[0921/163104.096612:VERBOSE1:file_url_loader_factory.cc(451)] FileURLLoader::Start: file:///css/bootstrap.css
userdata/Default/chrome_debug.log:[0921/163104.096746:VERBOSE1:file_url_loader_factory.cc(451)] FileURLLoader::Start: file:///css/email.css
userdata/Default/chrome_debug.log:[0921/163104.096873:VERBOSE1:file_url_loader_factory.cc(451)] FileURLLoader::Start: file:///css/print_common.css
userdata/Default/chrome_debug.log:[0921/163104.097021:VERBOSE1:file_url_loader_factory.cc(451)] FileURLLoader::Start: file:///css/print_A4P.css
I want to feed a virtual webcam device from an application window (under Linux/Xorg). I have so far just maximised the window and then used ffmpeg to grab the whole screen like this:
ffmpeg \
-f x11grab -framerate 15 -video_size 1280x1024 -i :0+0,0 \
-f v4l2 -vcodec rawvideo -pix_fmt yuv420p /dev/video6
where /dev/video6 is my v4l2loopback device. This works and I can use the virtual camera in video calls in chrome. This also indicates that the v4l2loopback module is correctly loaded into the kernel.
Unfortunately, it seems that ffmpeg can only read the whole screen, but not an application window. gstreamer on the other hand can. Playing around with gst-launch-1.0, I was hoping that I could get away with something like this:
gst-launch-1.0 ximagesrc xid=XID_OF_MY_WINDOW \
! "video/x-raw" \
! v4l2sink device=/dev/video6
However, that complains that Device '/dev/video6' is not an output device.
Given that ffmpeg seems happy to write to /dev/video6 I also tried piping the gst output to ffmpeg like this:
gst-launch-1.0 ximagesrc xid=XID_OF_MY_WINDOW \
! "video/x-raw" \
! filesink location=/dev/stdout \
| ffmpeg -i - -codec copy -f v4l2 -vcodec rawvideo -pix_fmt yuv420p /dev/video6
But then ffmpeg complains about Invalid data found when processing input.
This is running inside an xvfb headless environment, so mouse interactions will not work. This rules out obs as far as I can see.
I'm adding the chrome tag, because I see that chrome in principle would also provide a virtual camera via the --use-fake-device-for-media-stream switch. However, it seems that this switch only supports a static file rather than a stream.
Although I don't see why, it might be relevant that the other "application window" window is simply a second browser window. So the setup is google meet (or similar) in one browser window and the virtual camera gets fed vrom a second browser window.
You may try adding identity before v4l2sink:
# Better restart kernel module
sudo rmmod v4l2loopback
sudo modprobe v4l2loopback <your_options>
# Got window id from xwininfo
gst-launch-1.0 ximagesrc xid=0x3000010 ! videoconvert ! video/x-raw,format=YUY2 ! identity drop-allocation=1 ! v4l2sink device=/dev/video6
You should be able to display with:
gst-launch-1.0 v4l2src device=/dev/video6 ! videoconvert ! xvimagesink
No idea for your case, but for some browser on some targets/os/versions you may have to set exclusive_caps=1 into options when loading v4l2loopback kernel module.
Also note that this may not support any source window resizing.
So a service I am using will no longer be allowing Firefox as a default browser on Ubuntu anymore.
Currently I have the following script which will check if Firefox has crashed/closed and it will restart the instance to my desired URL. (Modified for chrome).
#!/bin/bash
COUNTER=0
while true
do
if [ ! `pgrep google-chrome-bin` ] ; then
echo -e '\nOh no, Google Chrome is not running! Restarting it now! \n\n'
echo Google Chrome has been restarted $COUNTER times
let COUNTER=COUNTER+1
google-chrome --private URLHERE
fi
sleep 30
done
The issue is that Chrome doesn't crash like Firefox. Instead you get the "Aw snap!" error message.
I found this: https://stackoverflow.com/a/17938587/5188217
But when I execute that, it will open numerous new tabs.
I would know if it's possible to make a chrome app who could be launched in the terminal without chrome UI. Example:
chrome.app.runtime.onLaunched.addListener(function() {
console.log("Hello world!);
});
imac:bin$ chrome --app-id=<my-app-id>
Hello world!
imac:bin$
I think not, but could you confirm. Thanks
I use this line to start chrome apps from the command line, on OSX:
open -n -a Google\ Chrome\ Canary --args --user-data-dir=/tmp/temp_chrome_data_dir --load-and-launch-app=...
The key flags are --user-data-dir to create an isolated profile, and --load-and-launch-app to start a chrome app automatically. I use the open command to resolve the path to chrome application on OSX, but you can just an explicit path instead:
/path/to/Google\ Chrome\ Canary --user-data-dir=/tmp/temp_chrome_data_dir --load-and-launch-app=...
That said, this will pop open a chrome browser window as well. I'm not yet sure how to get around that, but I will investigate. (My workaround for local development is to just minimize the window once, and it won't pop up any more).
I used to put !open -a Firefox.app % to open current file with Firefox, But I failed to make it with Chrome.
I got my answer:
!open -a Google\ Chrome %
You could also use:
!open -a "Google Chrome" %