Get current page URL in Chrome extension

只愿长相守 提交于 2019-12-23 10:55:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!