【发布时间】:2013-12-24 20:39:12
【问题描述】:
目前我正在编写一个脚本来调用chrome 浏览器的新实例。
我知道如何调用 chrome 在新标签页中打开 *.html 文档。
google-chrome *.html
Chrome 将打开一个新选项卡以显示该文件。
如何在不关闭其他选项卡或关闭浏览器窗口的情况下关闭终端中的选项卡?
【问题讨论】:
标签: bash shell google-chrome sh
目前我正在编写一个脚本来调用chrome 浏览器的新实例。
我知道如何调用 chrome 在新标签页中打开 *.html 文档。
google-chrome *.html
Chrome 将打开一个新选项卡以显示该文件。
如何在不关闭其他选项卡或关闭浏览器窗口的情况下关闭终端中的选项卡?
【问题讨论】:
标签: bash shell google-chrome sh
只有 linux 回答:
也许 wmctrl 可以提供一些帮助。您可以使用 -c 选项优雅地关闭窗口:
wmctrl -c "tab title"
字符串 chrome 与窗口标题匹配。请注意,如果弹出某些消息(例如,当您打开多个选项卡时),窗口可能不会关闭。
【讨论】:
wmctrl -c "https://stackoverflow.com/questions/20441793/how-to-close-a-chrome-browser-tab-via-terminal",并且这个标签保持打开状态。我不认为它有效....
http://example.org 标题:Example page。所以使用wmctrl -c "Example page"
wmctrl??
wmctrl -c "Tab title" 而不是url。它是唯一的 linux 解决方案,因为问题被标记为“bash”
你可以看看chrome远程调试:
chromium --remote-debugging-port=9222
并通过某种客户端连接到它(https://github.com/cyrus-and/chrome-remote-interface 似乎不错)。调试协议用于许多应用程序,但通过一些工作,您可以实现所需的功能。这里有一些文档供您查看:https://chromedevtools.github.io/devtools-protocol/tot/Page
或者也可能是 chromix-too,它基于扩展、守护进程和客户端架构。
https://github.com/smblott-github/chromix-too
这似乎更容易使用,并且可能正是您想要的,虽然扩展有点不方便,而且似乎需要支持更多功能。
编辑您要查找的查询是:
const chrome = require('ox-chrome-remote-interface');
chrome.List().then(tabs=>{
const list = tabs
.filter(t=>t.type == 'page' && t.title.includes(TITLE))
.map(t => t.id)
if(list.length==1){
chrome.Close({id:list[0]})
} else {
console.error(`${list.length} tabs match.`)
}
})
【讨论】: