简体   繁体   English

单击图标后如何更改Chrome扩展名的标志文字?

[英]How to change badge text for chrome extension when the icon is clicked?

I'm new to developing chrome extensions, and wanted to start with some simple code. 我刚开始开发chrome扩展,并想从一些简单的代码开始。 I've tried to change the badge text when the user clicks the icon. 当用户单击图标时,我试图更改徽章文本。 However, this doesn't seem to work. 但是,这似乎不起作用。 Can anyone suggest what am I doing wrong here? 有人可以建议我在这里做错什么吗?

manifest.json manifest.json

  {
  "name": "Hello World",
  "description": "Just displays hello world",
  "version": "1.2",
  "manifest_version": 2,
  "background": 
    {
        "scripts": ["background.js"],
        "persistent": false
    },
  "browser_action":
    {
        "name": "Click to display",
        "default_popup": "popup.html"

    }

 }

background.js background.js

function updatebadge()
{
chrome.browserAction.setBadgeText({text: "Hello!"});    
};

chrome.browserAction.onClicked.addListener(updatebadge);

popup.html popup.html

<body>
    Hello, World!
</body>

According to the chrome.browserAction 根据chrome.browserAction

onClicked onClicked

Fired when a browser action icon is clicked. 单击浏览器操作图标时触发。 This event will not fire if the browser action has a popup. 如果浏览器操作弹出,则不会触发此事件。

So you can remove your popup, or use a work-around... 因此,您可以删除弹出窗口,或使用替代方法...

chrome.browserAction.onClicked.addListener() with popup 带有弹出窗口的chrome.browserAction.onClicked.addListener()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM