问题
I want to get the current page URL from my default_popup
page like this:
chrome.tabs.query({active:true},function(tab){
url = tab.url;
});
And I have registered this popup.html
page in the manifest.json
file. Yet
I am getting the error message:
Uncaught Type Error: Cannot call method 'query' of undefined
What am I doing wrong?
回答1:
The callback parameter should specify a function that looks like this:
function(array of Tab result){...}
Maybe you should write like this
url = tab[0].url;
回答2:
Actually the error
Uncaught Type Error: Cannot call method 'query' of undefined
was because i was running popup.html page separately (separate from extension ) means i was explicitly opening popup.html page in browser to find the error but i forgot that popup.html can use chrome api if it is an extension page and my extension was not showing url because i was usinf tab.url instead of tab[0].url so Tom suggested right ans.
来源:https://stackoverflow.com/questions/11010396/get-current-page-url-in-chrome-extension