chrome.tabs.getSelected is undefined on latest chr

2019-08-24 04:08发布

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.

enter image description here

1条回答
Animai°情兽
2楼-- · 2019-08-24 05:00

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.

查看更多
登录 后发表回答