最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get current page URL in Chrome extension - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jun 13, 2012 at 9:47 user2428118 8,1244 gold badges46 silver badges73 bronze badges asked Jun 13, 2012 at 7:51 r.bhardwajr.bhardwaj 1,6236 gold badges31 silver badges54 bronze badges 2
  • The error you're getting means that the function query doesn't exist on the chrome.tabs object. Does the part of the extension you're calling this function from have the privileges to access this function? And does your extension have the tabs privilege? – user2428118 Commented Jun 13, 2012 at 8:22
  • yes i my extension have tabs privilege and i am using above chrome.tabs.query() method inside popup.html page which is registered as default_popup in browser_action: field in manifest.json file. – r.bhardwaj Commented Jun 13, 2012 at 8:32
Add a ment  | 

2 Answers 2

Reset to default 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.

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;
发布评论

评论列表(0)

  1. 暂无评论