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

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

问题描述

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



我正在尝试使用:

  chrome.browserAction.onClicked.addListener 

但我得到


未捕获TypeError:无法读取未定义的属性'onClicked'

这是我的清单文件:

  {
name:first extension,
version:2.2.12,
description:redirct to a link icon,
browser_action:{
default_icon:icontest.png,
default_title:执行操作
},
permissions:[tabs,http:// * / *],
content_scripts:[{
matches:[http://*.twitter.com/*,https://*.twitter.com/* ],
js:[twterland.js]
}],
图标:{
16:icontest.png,
48:icontest.png ,
128:icontest.png
}
}

这是我的js文件:

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


解决方案

好像代码在 twterland.js 文件,这是您的内容脚本。 browserAction 只能在扩展页面中使用,因此您不能在内容脚本中使用它。 $ b Document:< a href =https://developer.chrome.com/extensions/content_scripts =noreferrer> https://developer.chrome.com/extensions/content_scripts


但是,内容脚本有一些限制。 不能

- 使用chrome。* API (除chrome.extension的部分外)

- 使用变量或由其扩展的页面定义的函数

- 使用由网页或其他内容脚本定义的变量或函数

请将它放在后台页面上。


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

I'm trying to use:

chrome.browserAction.onClicked.addListener

but I get

Uncaught TypeError: Cannot read property 'onClicked' of undefined

This is my manifest file:

{
    "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"
    }
}

This is my js file:

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

解决方案

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.

Document: https://developer.chrome.com/extensions/content_scripts

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

Put it on the background page instead.

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

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