如何在 Firefox 插件的工具栏按钮上显示文本?

发布于 2024-11-14 09:14:59 字数 804 浏览 1 评论 0原文

我试图在工具栏按钮的图标上显示一个小文本(新消息计数),就像 Chrome 和 Opera 扩展程序在工具栏图标上带有徽章一样。文本不应覆盖整个图标,它应该位于底部,以便用户仍然可以看到图标的主要部分并识别它是什么扩展名。我该怎么做?

您可以在 Chrome 的此示例图像上看到我想要的内容:

toolbar button text overlay on chrome


我尝试使用说明、 span 和 div 在堆栈元素内,但我无法将它们放置在底部。 Description 和 div 始终在中心显示文本,而 span 将其显示在图标的右侧,使整个按钮更宽。

<toolbarpalette id="BrowserToolbarPalette">
    <toolbarbutton class="toolbarbutton-1">
        <stack>
            <image></image>
            <description right="0" bottom="0" value="4"></description>
            <!-- <html:div right="0" bottom="0">5</html:div> -->
        </stack>
    </toolbarbutton>
</toolbarpalette>

I'm trying to display a small text (new messages count) over icon in toolbar button, something like Chrome and Opera extensions have with badge over the toolbar icons. Text should not cover the whole icon, it should be at the bottom so the user can still see main part of the icon and recognize what extension it is. How can I do this?

You can see what I want on this example image from Chrome:

toolbar button text overlay on chrome

I tried with description, span and div inside stack element but I couldn't get none of them to position at the bottom. Description and div always display text at the center, while span displays it on the right side of the icon making the entire button wider.

<toolbarpalette id="BrowserToolbarPalette">
    <toolbarbutton class="toolbarbutton-1">
        <stack>
            <image></image>
            <description right="0" bottom="0" value="4"></description>
            <!-- <html:div right="0" bottom="0">5</html:div> -->
        </stack>
    </toolbarbutton>
</toolbarpalette>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呢古 2024-11-21 09:14:59

在一级方程式赛车中,我在等待雨停时浪费了一些时间,因此我使用画布进行了这项工作:

更新:当用户从工具栏中删除图标时,以前的方法存在缺陷,更新的方法是更健壮并且代码更少。

XUL

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
    <hbox hidden="true">
        <html:canvas id="toolbar_button_canvas" width="24" height="24"></html:canvas>
    </hbox>
    <toolbarpalette id="BrowserToolbarPalette">
        <toolbarbutton id="toolbar_button" class="toolbarbutton-1" />
    </toolbarpalette>
</overlay>

CSS

#toolbar_button {
    list-style-image:url("chrome://your_extension/skin/icon_024.png");
}

toolbar[iconsize="small"] #toolbar_button {
    list-style-image:url("chrome://your_extension/skin/icon_016.png");
}

JavaScript

show_status: function(display_text)
{
    var btn = document.getElementById("toolbar_button");
    var canvas = document.getElementById("toolbar_button_canvas");
    var img_src = window.getComputedStyle(btn).listStyleImage.replace(/^url\("(chrome:\/\/your_extension\/skin\/icon_0\d{2}.png)"\)$/, "$1");   // get image path
    var csize = img_src.replace(/^chrome:\/\/your_extension\/skin\/icon_0(\d{2})\.png$/, "$1"); // get image size

    canvas.setAttribute("width", csize);
    canvas.setAttribute("height", csize);
    var ctx = canvas.getContext("2d");

    var img = new Image();
    img.onload = function()
    {
        ctx.textBaseline = "top";
        ctx.font = "bold 9px sans-serif";       // has to go before measureText

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0);

        if (display_text !== "")
        {
            var w = ctx.measureText(display_text).width;
            var h = 7;                  // 9 = font height

            var rp = ((canvas.width - 4) > w) ? 2 : 1;  // right padding = 2, or 1 if text is wider
            var x = canvas.width - w - rp;
            var y = canvas.height - h - 1;          // 1 = bottom padding

            ctx.fillStyle = "#f00";             // background color
            ctx.fillRect(x-rp, y-1, w+rp+rp, h+2);
            ctx.fillStyle = "#fff";             // text color
            ctx.fillText(display_text, x, y);
            //ctx.strokeText(display_text, x, y);
        }
        btn.image = canvas.toDataURL("image/png", "");  // set new toolbar image
    };
    img.src = img_src;
}

I had some time to waste while waiting for rain to stop in Formula 1, so I made this work with canvas:

UPDATED: Previous method was flawed when user would remove the icon from the toolbar, updated method is more robust and with less code.

XUL

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
    <hbox hidden="true">
        <html:canvas id="toolbar_button_canvas" width="24" height="24"></html:canvas>
    </hbox>
    <toolbarpalette id="BrowserToolbarPalette">
        <toolbarbutton id="toolbar_button" class="toolbarbutton-1" />
    </toolbarpalette>
</overlay>

CSS

#toolbar_button {
    list-style-image:url("chrome://your_extension/skin/icon_024.png");
}

toolbar[iconsize="small"] #toolbar_button {
    list-style-image:url("chrome://your_extension/skin/icon_016.png");
}

JavaScript

show_status: function(display_text)
{
    var btn = document.getElementById("toolbar_button");
    var canvas = document.getElementById("toolbar_button_canvas");
    var img_src = window.getComputedStyle(btn).listStyleImage.replace(/^url\("(chrome:\/\/your_extension\/skin\/icon_0\d{2}.png)"\)$/, "$1");   // get image path
    var csize = img_src.replace(/^chrome:\/\/your_extension\/skin\/icon_0(\d{2})\.png$/, "$1"); // get image size

    canvas.setAttribute("width", csize);
    canvas.setAttribute("height", csize);
    var ctx = canvas.getContext("2d");

    var img = new Image();
    img.onload = function()
    {
        ctx.textBaseline = "top";
        ctx.font = "bold 9px sans-serif";       // has to go before measureText

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0);

        if (display_text !== "")
        {
            var w = ctx.measureText(display_text).width;
            var h = 7;                  // 9 = font height

            var rp = ((canvas.width - 4) > w) ? 2 : 1;  // right padding = 2, or 1 if text is wider
            var x = canvas.width - w - rp;
            var y = canvas.height - h - 1;          // 1 = bottom padding

            ctx.fillStyle = "#f00";             // background color
            ctx.fillRect(x-rp, y-1, w+rp+rp, h+2);
            ctx.fillStyle = "#fff";             // text color
            ctx.fillText(display_text, x, y);
            //ctx.strokeText(display_text, x, y);
        }
        btn.image = canvas.toDataURL("image/png", "");  // set new toolbar image
    };
    img.src = img_src;
}
_畞蕅 2024-11-21 09:14:59

我的 html:div 变

体你也可以尝试使用 z-index

看起来如何 http://f3.s.qip.ru /fTUvr2Cj.jpg

XUL

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
    <toolbar id="nav-bar">
    <hbox>
    <html:div style="color: #000000;
        font-size: 10px;
        font-weight: bold;
        margin-right: 10px;
        margin-top: 16px;
        position: fixed;
        right: 0;">5</html:div>
    <toolbarbutton  insertafter="search-container" type="menu">
    <menupopup>
    <menuitem   class="menuitem-iconic" label="&count;"></menuitem>

    <menuitem   class="menuitem-iconic" label="&size;"></menuitem>

    <menuitem  class="menuitem-iconic" label="&time;"></menuitem>
    <menuseparator></menuseparator>
    <menuitem  class="menuitem-iconic" label="&settings;"></menuitem>
    </menupopup>
    </toolbarbutton>
    </hbox>
    </toolbar>
</overlay>

my variant with html:div

P.S. you can also try play with z-index

How it's looks http://f3.s.qip.ru/fTUvr2Cj.jpg

XUL

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
    <toolbar id="nav-bar">
    <hbox>
    <html:div style="color: #000000;
        font-size: 10px;
        font-weight: bold;
        margin-right: 10px;
        margin-top: 16px;
        position: fixed;
        right: 0;">5</html:div>
    <toolbarbutton  insertafter="search-container" type="menu">
    <menupopup>
    <menuitem   class="menuitem-iconic" label="&count;"></menuitem>

    <menuitem   class="menuitem-iconic" label="&size;"></menuitem>

    <menuitem  class="menuitem-iconic" label="&time;"></menuitem>
    <menuseparator></menuseparator>
    <menuitem  class="menuitem-iconic" label="&settings;"></menuitem>
    </menupopup>
    </toolbarbutton>
    </hbox>
    </toolbar>
</overlay>
成熟的代价 2024-11-21 09:14:59

我刚刚使用了 browser-action-jplib

它在我的简单插件上运行良好>

如果您使用图书馆,您可以AMO-review 遇到一些问题

可以通过 解决问题

I just used the libraries from browser-action-jplib

And it worked fine on my simple addons

If you use the library, you may got some problems with AMO-review

It can be solved by the issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文