chrome.tabs.getSelected is undefined on latest chrome that uses manifest version 2?

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

I'm trying to create a plugin, that uses chrome.tabs.getSelected to find current URL,

Below I attached a minimal example, when any key is pressed, a dialog will popup, but it kept telling me chrome.tabs.getSelected is undefined:

Manifest file:

{     "content_scripts": [     {         "matches": ["http://*/*" , "https://*/*"],         "js" : [ "main.js" ]     }     ],         "manifest_version": 2,         "description": "XX",         "icons": {             "128": "icon.png",             "16": "button.png",             "32": "button.png",             "48": "icon.png"         },         "name": "XX",         "permissions": [ "tabs", "http://*/*", "https://*/*" ],         "version": "1.2" } 

And main.js:

window.addEventListener("keyup", function(e) {     chrome.tabs.getSelected(null, function(tab) { // undefined          alert (tab.url);     });  } , false); 

Chromium version: Version 21.0.1180.89 (154005)

EDIT

window.addEventListener("keyup", function(e) {     chrome.tabs.query( {active:true}, function(tab) {         alert (tab.url);     });  } , false); 

I really don't get it now, tabs.query doesn't work either.

回答1:

EDIT: Sorry, just realized you can't use chrome.tabs in a content script. You'll have to message the extension's background page for access to that. Here's some info about messaging: https://developer.chrome.com/extensions/messaging.html.



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