Tabs.url - undefined

I have no idea what I'm doing wrong here! I think this should work. I am writing a chrome extension and this should get the current tab url and set html #current-tab to the url (for testing purposes). The code successfully jumps to the callback, but it says that tab.url is undefined when I put it in the warning field and does not put its value in #current-tab . Here is 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); }; }); }); 
+4
source share
2 answers

chrome.tabs.query actually returns an array of objects Tab , so you will need to refer to the tab within the array (even if it's 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); }; }); }); 
+2
source

I realized what happened! Apparently you need to reload the extension to update the changes to the manifest file! I added permissions later, but did not reload the extensions in the extension manager, so the change did not take effect! We ride now!

+1
source

Source: https://habr.com/ru/post/1447832/


All Articles