从Google Chrome中获取标签页信息的方法(C/Go)

huangapple go评论158阅读模式
英文:

Get tab information from Google Chrome in C/Go

问题

我想收集一些关于Google Chrome运行时的信息。目前我是使用一些AppleScript(https://gist.github.com/jcla1/6525572)来实现的,但我想用C或Go来重写这个脚本。

有人知道Chrome是否提供了API来获取以下信息吗:

  • 打开的标签数
  • 当前活动的URL

到目前为止,我只发现可以使用CGWindowListCopyWindowInfo(Carbon)来获取当前标签的标题,这个方法效果很好,但显然无法提供URL(以及打开的标签数等)。

理想情况下,Chrome应该有一个事件架构,我可以连接到其中并接收与输入新URL相关的所有事件。

P.S. 只需要在OSX上工作!

英文:

I'd like to gather some info on Google Chrome when it's running. Currently I'm doing this using some applescript (https://gist.github.com/jcla1/6525572), but I'd like to rewrite this in C or Go.

Does anyone know of an API Chrome exposes to gather information like:

  • open tabs

  • current active URL

So far I've only found out that I can get the current tab's title using CGWindowListCopyWindowInfo (Carbon), which works well but obviously doesn't provide the URL (and not the # open tabs, etc.).

Ideally Chrome would have a event architecture which I could hook into and receive all the events to do with entering a new URL.

P.S. Only required to work on OSX!

答案1

得分: 3

我已经编写了Go程序,使用远程调试协议来远程控制/检查Chrome。它的工作原理是首先从特定的URL获取JSON数据,该数据提供了打开的标签页和基本信息,如标签页的当前URL。然后,您可以使用JSON对象中的WebSocket链接来控制/检查各个标签页。

由于您只需要基本信息,可以忽略大部分调试API,只需下载JSON索引即可。首先,使用chrome --remote-debugging-port=9222启动Chrome。请注意,为了使其正常工作,所有Chrome窗口都需要关闭。您还可以使用--user-data-dir=<some dir>以全新的配置文件启动Chrome,这样您就可以保留其他Chrome窗口。

一旦端口打开,获取数据就很容易。只需访问http://localhost:9222/json并解组数据即可。以下是我使用的代码:

type Tab struct {
    Description          string `json:"description"`
    DevtoolsFrontendUrl  string `json:"devtoolsFrontendUrl"`
    FaviconUrl           string `json:"faviconUrl"`
    Id                   string `json:"id"`
    ThumbnailUrl         string `json:"thumbnailUrl"`
    Title                string `json:"title"`
    Type                 string `json:"type"`
    Url                  string `json:"url"`
    WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"`
}

func GetTabs() ([]Tab, error) {
    resp, err := http.Get("http://localhost:9222/json")
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    var tabs []Tab
    err = json.NewDecoder(resp.Body).Decode(&tabs)
    if err != nil {
        return nil, err
    }

    return tabs, nil
}

您可以在这里获取更多信息。

不幸的是,使用此API无法在打开新标签页时接收到事件。但是,如果您连接到的标签页以任何方式发生更改,您可以通过Websockets获得通知。如果您愿意,您可以构建一个扩展程序来监视诸如新标签页和加载的URL等更改。

英文:

I have written Go programs to remotely control/inspect chrome using the Remote Debugging Protocol. The way it works is you first fetch JSON data from a specific url and it gives the open tabs and basic information such as the tab's current url. You can then use the websocket links in the JSON object to contorl/inspect individual tabs.

Since you only want basic information, you can ignore most of the debugging api and just download the JSON index. Frist, start chrome using chrome --remote-debugging-port=9222. Note that all chrome windows need to be closed for this to work. You may also use --user-data-dir=&lt;some dir&gt; to launch with a fresh profile so you can leave your other chrome windows open.

Once you have the port open, obtaining the data is easy. Just go to http://localhost:9222/json and unmarshal the data. Here is the code I used :

type Tab struct {
	Description          string `json:&quot;description&quot;`
	DevtoolsFrontendUrl  string `json:&quot;devtoolsFrontendUrl&quot;`
	FaviconUrl           string `json:&quot;faviconUrl&quot;`
	Id                   string `json:&quot;id&quot;`
	ThumbnailUrl         string `json:&quot;thumbnailUrl&quot;`
	Title                string `json:&quot;title&quot;`
	Type                 string `json:&quot;type&quot;`
	Url                  string `json:&quot;url&quot;`
	WebSocketDebuggerUrl string `json:&quot;webSocketDebuggerUrl&quot;`
}

func GetTabs() ([]Tab, error) {
	resp, err := http.Get(&quot;http://localhost:9222/json&quot;)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var tabs []Tab
	err = json.NewDecoder(resp.Body).Decode(&amp;tabs)
	if err != nil {
		return nil, err
	}

	return tabs, nil
}

You can obtain more information here.


There is unfortunately no way to receive an event when a new tab is opened using this API. However, you can get notified when a tab you are connected to with websockets changes in any way. If you are willing to go crazy, you can build an extension to monitor for changes such as new tabs and loaded urls.

huangapple
  • 本文由 发表于 2013年11月2日 20:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/19741977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定