954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Chrome.tabs.getSelected() doesn’t work. What am I doing wrong ?

Hi there ! I'm building my first GC extension, and I am having a problem using this function.

I would like to get the url of the current tab, and to do so (after some google research, of course), I use the chrome.tabs.getSelected() function. Here it is in my code :

...
socket.onopen = function(msg){ log("Welcome - status " + this.readyState); 
chrome.tabs.getSelected(null, function(tab){ sendUrl(tab.url); }); };

function sendUrl(tabUrl)
{
 socket.send("#URL#"+ tabUrl);
}


Thesocket.open is a listener for the Websocket API I use in my extension. The log function print the status of the socket (1, which means it's OPEN or connected and ready to communicate, according to the Websocket API), but the chrome.tabs.getSelected() function do not seems to work, as the server do not receive anything (the socket is not the problem here, I tried to send a test message with socket.send() instead of chrome.tabs.getSelected() and the server received it).

I put the permissions: ["tabs"] into the manifest.json so I can use this function.

So I don't know what I am doing wrong here ? I also tried to put chrome.tab.getSelected() elsewhere in the code, but it is still not working.

Any help is welcome :)

colon3l
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
 
chrome.tabs.getSelected(null, function(tab){ sendUrl(tab.url); } )

You might try replacing sendUrl(tab.url); with var tmp = tab.url;sendUrl(tmp); .

It that doesn't work, at least it will give you a checkpoint to test the return.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

Thx for the answer, I actually managed to resolve this problem by putting all this code into the background page :)

colon3l
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: