2

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/

...
chrome.tabs.getSelected(null, function(tab) {
var yourell = tab.url;
...

Anyone have any ideas?

Thanks,

Dave

hippietrail
  • 15,848
  • 18
  • 99
  • 158
dave
  • 29
  • 2
  • 1
    It always returns full url, not sure how you managed to get filename only... Output that url into a console.log, maybe you are looking at something different. – serg Mar 17 '11 at 18:17
  • You are correct Serg, apologies for the timewasting. It appears that it's picasa chopping the URL up. Thanks for your help – dave Mar 18 '11 at 08:20

1 Answers1

0

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

enter image description here

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

enter image description here

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

Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • Should this still be working? I've installed your plugin but when I click on the icon I get nothing in my console. – Jack Apr 23 '15 at 10:44
  • @JackNicholson, Thanks for checking it out and testing it. It is still working, but you need to be looking at the console for the background page since that is where the javascript is running that has the console output. – Mike Grace Apr 23 '15 at 14:12