3

Updated Issue: I can console.log any amount of tabs but no tabs have the url property.

argh

Original Problem that is solved:I cannot seem to get this very simple piece of code to work in background.js. I want to get access to the tabs but they always return as object Object for each tab.

chrome.tabs.query({}, function(tab){ alert(tab); }); 

objects!

I DO have tabs in my manifest file, but even without it doesn't make a difference. Personally I think that it doesn't think it has the permissions to tabs but it does and my other permissions such as notifications & urls work fine. Thanks.

11
  • 3
    alert is not a good debugging tool. It converts everything to strings. Use console.log. Commented Jan 22, 2013 at 21:33
  • My console is empty if I use console.log. Edit: Sorry, after checking the background.js's console it is still undefined. Commented Jan 22, 2013 at 21:35
  • 1
    Do you get any results if you supply some query information (e.g. { active: true })? Commented Jan 22, 2013 at 21:36
  • 1
    Then inspect what exactly tab[0] is, so that you know what properties you can access. Are you sure your extension has the 'tabs' or 'webNavigation' permissions? Commented Jan 22, 2013 at 21:40
  • Console.log was undefined. But now its working =/ Seems your tip about alert was the main issue. Thanks! Commented Jan 22, 2013 at 21:41

3 Answers 3

6

After adding the tabs permission in manifest.json you need to remove and re-add the extension in order for it to take account of the new permission.

Sign up to request clarification or add additional context in comments.

1 Comment

God bless your soul!
5

chrome.tabs.query actually returns an array of Tab objects (which is why you are seeing [object, Object]. Try doing something like:

alert(tab[0].url); 

And see if it displays something that you are expecting.

13 Comments

That gives me undefined, as does tab[0].url (as chrome.tabs.query gives me an array I thought that would give me something)
@Slappybag Sorry, will adjust my answer to reflect the array part. You're getting undefined with tab[0].url?
Yes, I am getting undefined with tab[0].url
@Slappybag And the error is telling you that the object at that location is undefined or the property of that object is undefined?
@Slappybag And just to double check, you definitely do have tabs in your permissions list? And I'm assuming your other objects look the same as that one?
|
1

You have to add "tabs" to the permissions array in your manifest.json, otherwise you will not be able to see the url of the tabs.

1 Comment

Which is easy to screw up if you copy a permissions snippet from the docs and paste it at the end of the JSON, effectively hiding your earlier permissions... :s

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.