chrome.tabs.getSelected未定义在使用清单版本2的最新chrome上? [英] chrome.tabs.getSelected is undefined on latest chrome that uses manifest version 2?

查看:406
本文介绍了chrome.tabs.getSelected未定义在使用清单版本2的最新chrome上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个插件,它使用 chrome.tabs.getSelected 来查找当前URL,

下面我附上一个最简单的例子,当按下任意键时,会弹出一个对话框,但它一直告诉我 chrome.tabs.getSelected 未定义:



清单文件:

  {
content_scripts:[
{
matches:[http:// * / *,https:// * / *],
js:[main.js]

$ b $manifest_version:2,
description:XX,
图标:{
128:图标bng,
16:button.png,
32:button.png,
48:icon.png
} ,
name:XX,
permissions:[tabs,http:// * / *,https:// * / *],
version:1.2
}

和main.js:

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

},false);

Chromium版本:版本21.0.1180.89(154005)

编辑

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

},false);

我现在真的不明白,tabs.query也不行。



解决方案

编辑:抱歉,刚刚意识到你不能在内容脚本中使用 chrome.tabs 。您必须通知该扩展程序的后台页面才能访问该页面。以下是关于消息传递的一些信息: https://developer.chrome.com/extensions/messaging.html


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.

解决方案

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.

这篇关于chrome.tabs.getSelected未定义在使用清单版本2的最新chrome上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆