“无法读取未定义的属性"在内容脚本中使用 chrome.tabs 或其他 chrome API 时 [英] "Cannot read property of undefined" when using chrome.tabs or other chrome API in content script

查看:32
本文介绍了“无法读取未定义的属性"在内容脚本中使用 chrome.tabs 或其他 chrome API 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

chrome.tabs 返回 undefined 尽管我在权限块中设置了选项卡.

chrome.tabs returns undefined despite the fact I set tabs in the permissions block.

"permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
],
"content_scripts": [
    {
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "js": [
            "js/myScript.js"
        ],
        "all_frames": true
    }
],

但在 myScript.js 中,以下返回 undefined.

But in myScript.js the following returns undefined.

chrome.tabs   

推荐答案

由于内容脚本有其自身的局限性,

As content script has its own limitations,

chrome.tabs 仅在后台脚本和弹出脚本中可用.

chrome.tabs is only available in background scripts and popup scripts.

如果您想使用 chrome.tabs,那么将消息从 content_script 传递到后台脚本并使用 chrome.tabs.

If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs.

这篇关于“无法读取未定义的属性"在内容脚本中使用 chrome.tabs 或其他 chrome API 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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