为什么 chrome.browserAction.onClicked 未定义? [英] Why is chrome.browserAction.onClicked undefined?

查看:45
本文介绍了为什么 chrome.browserAction.onClicked 未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Chrome 扩展程序,当点击浏览器操作图标时,它会将我重定向到一个 URL.

I'm writing a Chrome extension that will redirect me to a URL when clicking on the browser action icon.

我正在尝试使用:

chrome.browserAction.onClicked.addListener

但我明白

未捕获的类型错误:无法读取未定义的属性onClicked"

Uncaught TypeError: Cannot read property 'onClicked' of undefined

这是我的清单文件:

{
    "name": "first extension",
    "version": "2.2.12",
    "description": "redirct to a link icon",
    "browser_action": {
        "default_icon": "icontest.png",
        "default_title": "Do action"
    },
    "permissions": ["tabs", "http://*/*"],
    "content_scripts": [{
        "matches": ["http://*.twitter.com/*", "https://*.twitter.com/*"],
        "js": ["twterland.js"]
    }],
    "icons": {
        "16": "icontest.png",
        "48": "icontest.png",
        "128": "icontest.png"
    }
}

这是我的js文件:

chrome.browserAction.onClicked.addListener(function(tab) { alert("hi"); });

推荐答案

似乎代码在您的 twterland.js 文件中,这是您的内容脚本.browserAction 只能在扩展页面中使用,不能在内容脚本中使用.

It seems like the code is in your twterland.js file, which is your content script. browserAction can only be used in extension pages, so you can not use it in content scripts.

文档:https://developer.chrome.com/extensions/content_scripts

但是,内容脚本有一些限制.他们不能:
- 使用 chrome.* API(除了部分 chrome.extension)
- 使用由其扩展页面定义的变量或函数
- 使用由网页或其他内容脚本定义的变量或函数

However, content scripts have some limitations. They cannot:
- Use chrome.* APIs (except for parts of chrome.extension)
- Use variables or functions defined by their extension's pages
- Use variables or functions defined by web pages or by other content scripts

改为将其放在背景页面上.

这篇关于为什么 chrome.browserAction.onClicked 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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