通过命令行启动Google Chrome并指定窗口坐标

37

我想要找到一个能够使用特定的x和y坐标打开Google Chrome的shell命令(这样我可以设置窗口打开时的位置)。是否可能使用命令行参数来实现这一点?

为了实现这个目标,我需要修改以下命令:

google-chrome http://www.google.com/


问题现在在Ubuntu 18.04上没有解决。 - Sahin
6个回答

50

当你使用Google Chrome时,有一种更简洁的方式:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 
     --profile-directory="Default"
     --app="data:text/html,<html><body><script>window.moveTo(580,240);window.resizeTo(800,600);window.location='http://www.test.de';</script></body></html>"

优点:

  • 自动打开窗口
  • 避免弹出拦截器
  • 在不同的监视器上打开多个窗口(多屏幕设置,需要两个或更多Chrome配置文件)

缺点:

  • 似乎仅适用于“应用程序”模式
  • 未在其他浏览器中测试
Note: This is the translated version of the original text.

1
另一个“专业版”——它可以使用普通的浏览器配置文件,适用于Chrome 51;而另一个答案显然不能。 - David P. Caldwell
1
太好了,非常感谢!这是唯一对我有效的解决方案。 - cronfy
想要一个地址栏。这个窗口怎么变成弹出窗口的? - Anders Lindén
这个在OSX上能用吗? - avgJoe
@avgJoe,是的,它适用于包括OSX在内的所有操作系统。如何在OSX上调用Chrome的示例可以在其他答案中看到。 - nitram509
如果已经使用相同的配置文件打开了另一个窗口,这将无法工作!屏幕会闪烁,但不会打开新窗口。 - Schwan Abdulkareem

28

http://peter.sh/experiments/chromium-command-line-switches/ 上说,--window-position=x,y 是你要找的。

多年后更新此内容,包括我几年前编写的一个小型 shell 脚本(但是在回答此问题之后),该脚本提供了如何启动具有自定义窗口大小和位置的 Chrome 以及按名称创建“虚假”用户数据目录的示例。

它可能仍然可以工作,而且设置了一些危险选项,但你可以想象一下... 不要直接使用这个脚本,因为一些标志可能已被重命名或完全删除...(像 socks 代理命令一样)。

#!/bin/bash -x

FAKEUSER="${1:-fake-chrome-user}"
CHROMEROOT=$HOME/.chromeroot/

mkdir -p ${CHROMEROOT}

export PROFILE="${CHROMEROOT}/${FAKEUSER}-chromium-profile"
export DISK_CACHEDIR="${CHROMEROOT}/${FAKEUSER}-chromium-profile-cache"
export DISK_CACHESIZE=4096
export MEDIA_CACHESIZE=4096

PARANOID_OPTIONS="\
        --no-displaying-insecure-content \
        --no-referrers \
        --disable-zero-suggest \
        --disable-sync  \
        --cipher-suite-blacklist=0x0004,0x0005,0xc011,0xc007 \
        --enable-sandbox-logging >/dev/null 2>&1
        "


/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
        --remember-cert-error-decisions \
        --ignore-certificate-errors \
        --ignore-urlfetcher-cert-requests \
        --allow-running-insecure-content \
        --window-position=2400,400 \
        --window-size=1500,1000 \
        --no-pings \
        --user-data-dir=${PROFILE} \
        --disk-cache-dir=${DISK_CACHEDIR} \
        --disk-cache-size=${DISK_CACHESIZE} \
        --media-cache-size=${MEDIA_CACHESIZE} \
        2>&1


#--proxy-server="socks4://localhost:30604" \
#--host-resolver-rules="MAP * 0.0.0.0 , EXCLUDE localhost" \

1
它也可以在Firefox和IE上完成吗?我在这里发布了一个问题:http://stackoverflow.com/questions/26626884/chrome-app-equivalent-in-firefox-ie - liorafar
1
遗憾的是,这并不总是有效。请参见https://dev59.com/RGUq5IYBdhLWcg3wT-6D#357knYgBc1ULPQZFhws-。 - Greg Bray
1
它确实可以工作,@GregBray,但你也需要设置--user-data-dir标志。 - Frederik Krautwald

27
为了在@synthesizerpatel的答案基础上进一步,--window-position单独使用将无效。 您需要使用--user-data-dir--chrome-frame将其作为自己的新实例启动,例如:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  --user-data-dir=XXXXXXXXXX --window-size=800,600 --window-position=580,240 --app="http://www.google.com/"
or
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --chrome-frame --window-size=800,600 --window-position=580,240 --app="http://www.google.com/"

对我来说不幸的是,将它作为一个新实例意味着它不会从其他实例中传递会话/cookie等信息,因此我必须正常打开它(只使用--app参数),然后在我打开的页面中使用JavaScript做以下操作:

window.moveTo(580,240);
window.resizeTo(800,600);

我猜,如果你要打开别人拥有的网页,你可以打开自己的网页,该网页包含上述js代码,并导航到他们的网页。


3
选项 "--chrome-frame" 对我无效,但使用 "--user-data-dir" 选项后我解决了问题。谢谢。 - eeezyy
1
看起来它已经被弃用了。 - geotheory
1
它也可以在Firefox和IE上完成吗?我在这里发布了一个问题:http://stackoverflow.com/questions/26626884/chrome-app-equivalent-in-firefox-ie - liorafar
在我的树莓派上,运行着Raspbian和lightdm,这对我来说是可行的。 - Brad
“--user-data-dir”选项在我的MacOS上有效,但是该命令现在会阻塞。为了解决这个问题,命令行必须加上“&”后缀,或者用户在完成操作后按Ctrl+C(关闭窗口不会解除进程的阻塞)。 - raner

4
我曾使用过这个:

google-chrome "data:text/html;charset=ISO-8859-1,<html>
    <head></head><body><script language=\"javascript\">
        window.open('http://perso.f-hauri.ch/~felix/svg/dustin_w_Clock_autonom.svg',
             'clock','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,'
             +'resizable=1,width=600,height=600,top=100,left=120');</script>"

但是谷歌浏览器会阻止弹出窗口,所以这个问题可以这样解决:
google-chrome "data:text/html;charset=ISO-8859-1,<html><head></head><body>
    <button onclick=\"javascript:window.open(
        'http://perso.f-hauri.ch/~felix/svg/dustin_w_Clock_autonom.svg',
        'clock','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,'
        +'resizable=1,width=600,height=600,top=100,left=120');\"> clock </button>"

提供一种不错的方法来完成这个任务。

注意:这个方法在火狐浏览器中同样有效。


如果您将 onclick 替换为 onfocus,则在 Chrome 中只需按 <tab> 键即可正常工作,但在 Firefox 中不行... 可能是安全问题!? - F. Hauri - Give Up GitHub

1

在我最新的Chrome版本中,我只需要以下操作。每次关闭应用程序时,它都会记住我的窗口大小和位置。

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --chrome-frame --app=https://mightytext.net/web8/?exp=1

这对我在版本48.0.2564.48 beta-m (64位)和版本48.0.2564.48 beta-m (64位)中有效。


问题在于它需要手动调整位置和大小;它们不能被纳入启动命令中。 - Synetech

0

这对我有用。

"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\Users\hello\gtemp\123" --window-size=100,1000 --window-position=3000,0 "https://www.openchromeonadifferentwindow.com"
  1. 需要 user-data-dir

为了运行多个独立的Chrome浏览器进程,每个进程都需要自己的配置文件。您可以在每次运行浏览器实例时指定--user-data-dir="path/to/profile/#"(将#更改为不同的数字或使用完全不同的路径)。注意 - 路径不必存在,它只需要是合法的(并且Chrome应该有权限在不存在的情况下创建它)。 Chrome会创建尚不存在的配置文件路径

.. 在这个链接中找到了相关信息。

  1. window-position=3000,0 可以帮助将窗口位置移动到另一个显示器上。
  2. 如果要使用不同的设置打开其他实例,则需要更改 user-data-dir 中的 # 为不同的数字。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接