Firefox附加SDK中的Chrome的"setBadgeText"是否等效? [英] Is there an equivalent for Chrome's 'setBadgeText' in Firefox Add-on SDK?

查看:125
本文介绍了Firefox附加SDK中的Chrome的"setBadgeText"是否等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chrome的方法chrome.browserAction.setBadgeText将文本覆盖在扩展程序图标上方.例如,如果要显示未读邮件总数,这将很方便.

在开发 Firefox插件SDK时,是否存在等效的方法

a>扩展程序,还是在运行时更改图标的最佳方法是什么?

从SDK 1.11版开始,我在Firefox附加SDK API中找不到类似的方法.

解决方案

注意:由于删除了小部件模块,此答案在Firefox 38+中不再起作用.使用 ui模块中的按钮之一.


我已将chrome.browserAction API移植到Firefox,并将其作为Jetpack模块发布,请参见 说明)

  • 编辑附件的package.json,并将"browser-action"添加为依赖项.
  • 至此,您已经设置了所有先决条件.剩下的就是创建浏览器操作.

    示例

    此附加组件在位置栏附近添加浏览器操作,该操作在单击时显示弹出窗口.在此弹出窗口的输入字段中键入内容时,选项卡的标题将更改为该字段的值.

    lib/main.js

     // lib/main.js
    var badge = require('browserAction').BrowserAction({
        default_icon: 'images/icon19.png', // optional
        default_title: 'Badge title',      // optional; shown in tooltip
        default_popup: 'popup.html'        // optional
    });
    
    badge.onMessage.addListener(function(message, sender, sendResponse) {
        require('sdk/tabs').activeTab.title = message;
    });
     

    data/popup.html

     <!-- (Example) Please avoid inline JavaScript/CSS in your production code -->
    <body style="width:200px;">
      Demo: This is a popup page.<br>
      Uses <code>extension.sendMessage</code> to notify the background page,
      which in turn changes the current tab's title to below value <br>
      <input onkeyup="extension.sendMessage(this.value);" style="display:block;">
    </body>
     

    这里是结果的简短截屏(注意:工具提示的颜色略有不同; 由拜赞兹录制的):

    Chrome's method chrome.browserAction.setBadgeText overlays text on top of the extensions icon. This is handy if you want to display total unread mails for example.

    Is there an equivalent method when developing a Firefox Addon SDK extension, or what is the best way to change the icon at runtime?

    As of version 1.11 of SDK I can't find similar method in Firefox Add-on SDK API.

    解决方案

    Note: This answer does not work any more in Firefox 38+ due to the removal of the widget module. Use one of the buttons from the ui module instead.


    I've ported the chrome.browserAction API to Firefox, and published it as a Jetpack module, see https://github.com/Rob--W/browser-action-jplib

    If you're familiar with the chrome.browserAction API, using this module can't be easier. Even if you have never created a Firefox add-on before, you can get started within a few minutes:

    1. Install the add-on SDK (instructions from Mozilla).
    2. Activate the SDK (by running addon-sdk) and create the boilerplate files (cfx init).
    3. Download my browser-action Jetpack library (instructions).
    4. Edit your add-on's package.json, and add "browser-action" as a dependency.

    At this point, you've set up all prerequisites. What remains is the creation of the browser action.

    Example

    This add-on adds a browser action near the location bar, which shows a popup on click. When you type something in the input field within this popup, the tab's title is changes to the field's value.

    lib/main.js

    // lib/main.js
    var badge = require('browserAction').BrowserAction({
        default_icon: 'images/icon19.png', // optional
        default_title: 'Badge title',      // optional; shown in tooltip
        default_popup: 'popup.html'        // optional
    });
    
    badge.onMessage.addListener(function(message, sender, sendResponse) {
        require('sdk/tabs').activeTab.title = message;
    });
    

    data/popup.html

    <!-- (Example) Please avoid inline JavaScript/CSS in your production code -->
    <body style="width:200px;">
      Demo: This is a popup page.<br>
      Uses <code>extension.sendMessage</code> to notify the background page,
      which in turn changes the current tab's title to below value <br>
      <input onkeyup="extension.sendMessage(this.value);" style="display:block;">
    </body>
    

    Here's a short screencast of the result (note: tooltip color is slightly off; recorded with byzanz):

    这篇关于Firefox附加SDK中的Chrome的"setBadgeText"是否等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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