开发者

Get URL of a specific tab?

In Google Chrome, how would I get the URL of the page being shown on a specif开发者_开发知识库ic tab?


This depends on how you define specific tab. There are numerous functions to get a tab, which in turn give you back a Tab object. This object has a url attribute.

Let's take the current selected tab for example. You get a handle on it with chrome.tabs.getSelected. Where null is a WindowID, and defaults to the current window.

chrome.tabs.getSelected(null, function(tab) { 
    alert(tab.url);
})

For more information I suggest you have a look at the documentation of the API.


According to the Google Chrome Extensions documentation, you can retrieve a tab's url by calling the chrome.tabs.get(integer tabId, function callback) method to get a Tab object containing the following fields:

id ( integer ) The ID of the tab. Tab IDs are unique within a browser session.

index ( integer ) The zero-based index of the tab within its window.

windowId ( integer ) The ID of the window the tab is contained within.

selected ( boolean ) Whether the tab is selected.

pinned ( boolean ) Whether the tab is pinned.

url ( string ) The URL the tab is displaying

title ( optional string ) The title of the tab. This may not be available if the tab is loading.

favIconUrl ( optional string ) The URL of the tab's favicon. This may not be available if the tab is loading.

status ( optional string ) Either loading or complete.

incognito ( boolean ) Whether the tab is in an incognito window.

The "tabs" element also needs to be added to the permissions section of the manifest.

  "permissions": [
    "tabs"
  ],
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜