4

I have no clue what I'm doing wrong here! This should be working I believe. I'm writing a chrome extension and this should get the current tab's url and set the html of #current-tab to the url (for testing purposes). The code successfully gets to the callback, but it says that tab.url is undefined when I put it in an alert box, and it does not put it's value in #current-tab. Here's the code I have:

$('#get-tab').click(function(){ chrome.tabs.query({"active" : true}, function(tab){ for (var i = 0; i < tab.length; i++) { alert(tab[i].url); $('#current-tab').append(tab[i].url); }; }); }); 
3
  • Shouldn't that be tab[i].url? Commented Nov 22, 2012 at 22:56
  • @Kiyura Ha, looks like we were thinking the same thing :) Commented Nov 22, 2012 at 22:57
  • I initially did that (tab[i].url) as I knew that it returned an array, but that didn't seem to be working either, so I posted the script that I was trying instead, which didn't make much sense to me, but I was experimenting... See my edit above with my original code. Commented Nov 22, 2012 at 23:38

2 Answers 2

3

I figured out what was wrong! Apparently you need to reload the extension to refresh changes to the manifest file! I had added the permissions later, but had not reloaded the extensions in the extension manager, so the change had not taken effect! We're rolling now!

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

2 Comments

Haha, so it was the manifest, that's good! Was it the tabs part?
Same issue here. this show be on the front page of the google extension docs.
2

chrome.tabs.query actually returns an array of Tab objects, so you would have to reference the tab inside of the array (even if it is only one Tab):

$('#get-tab').click(function(){ chrome.tabs.query({"active" : true}, function(tab){ for (var i = 0; i < tab.length; i++) { alert(tab[i].url); $('#current-tab').append(tab[i].url); }; }); }); 

4 Comments

I initially did that (tab[i].url) as I knew that it returned an array, but that didn't seem to be working either, so I posted the script that I was trying instead, which didn't make much sense to me, but I was experimenting... See my edit above with my original code.
@Notionwork We'll go for the easy way first (hopefully :) ) - do you have tabs in your permissions in manifest.json?
Yup! Here's my manifest: { "name" : "Notionwork Site Manager", "version" : "1.0", "manifest_version" : 2, "description" : "The manager organizes sites that are of interest and adds corrisponding bookmarks to the Chrome bookmark manager", "browser_action" : { "default_icon" : "icon.png", "default_popup" : "popup.html" }, "permissions" : [ "tabs" ], "icons" : { "128" : "icon.png" } }
@Notionwork Alright, let's figure this thing out. Where are you defining your JavaScript? Is it directly inside of popup.html? One issue that comes up with manifest v2 is that you can no longer use in-line scripts, so if it is inside of there, try moving it into a popup.js as a background script that you call from popup.html.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.