开发者

Get Complete URL of Tab in Chrome Extension

I'm trying to modify a chrome extension which uploads images to Picasa and labels them with the complete URL e.g. www.domain.com/whatever.jpg, of where they were uploaded from.

I'm having problems with getting the complete URL. When i try the following bit of code, it gives me an incomplete URL with only the whatever.jpg, and not the www.domain.com/

开发者_如何学C
...
chrome.tabs.getSelected(null, function(tab) {
var yourell = tab.url;
...

Anyone have any ideas?

Thanks,

Dave


Answer

Use

chrome.tabs.query

chrome.tabs.getSelected has been deprecated. To get the full URL of the currently active tab you will want to use the chrome.tabs.query function and do something like this

Example

manifest.json

{
    "name": "Get Current Open Tab Info Example",
    "manifest_version": 2,
    "version": "0.1",
    "description": "How to get info on the current tab on the active window in chrome.",
    "background": {
        "scripts": ["background.js"]
    },
    "browser_action": {
        "default_title": "test"
    },
    "permissions": [
        "tabs"
    ]
}

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.query({
        active: true,
        currentWindow: true
    }, function(tab) {
        console.log(tab[0]);
        console.log(tab[0].url);
    });
});

Running Example Screenshots

Load example extension

Get Complete URL of Tab in Chrome Extension

Clicking extension button with browser window opened to exampley.com
Console log of extension popup

Get Complete URL of Tab in Chrome Extension

Example files http://mikegrace.s3.amazonaws.com/stackoverflow/current-tab.zip

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜