如何使Firefox扩展工具栏按钮自动出现? [英] How can I make my Firefox extension toolbar button appear automatically?

查看:320
本文介绍了如何使Firefox扩展工具栏按钮自动出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含工具栏按钮的firefox扩展.如何设置它,以便在安装扩展程序后,该按钮会自动出现在主工具栏中.我不希望我的用户必须转到自定义工具栏菜单并将我的按钮拖到上方.

I've created a firefox extension that consists of a toolbar button. How can I set it up so that when my extension is installed, the button automatically appears in the main toolbar. I don't want my users to have to go to the customize toolbar menu and drag my button over.

推荐答案

来自 https: //developer.mozilla.org/En/Code_snippets:Toolbar#Adding_button_by_default -

创建和部署扩展并包含工具栏按钮时 通过覆盖自定义"工具栏调色板来实现此功能,该功能不可用 默认情况下.用户必须将其拖动到工具栏上.以下 代码默认情况下会将您的按钮放在工具栏上.这应该 仅在安装后第一次运行附件时执行,因此 如果用户决定删除您的按钮,则不会显示 他们每次启动应用程序时都会再次输入.

When you create and deploy your extension and include a toolbar button for it by overlaying the Customize toolbarpalette, it is not available by default. The user has to drag it onto the toolbar. The following code will place your button on the toolbar by default. This should only be done on the first run of your add-on after installation so that if the user decides to remove your button, it doesn't show up again every time they start the application.

注释

默认情况下,在首次运行时或扩展程序更新添加新按钮时,默认情况下仅插入一次按钮.

Insert your button by default only once, at first run, or when an extension update adds a new button.

请仅在默认情况下添加按钮,如果该按钮可以为用户增加实际价值,并且会经常成为您扩展程序的入口点.

Please only add your button by default if it adds real value to the user and will be a frequent entry point to your extension.

您不得在以下任何元素之间插入工具栏按钮:组合的后退/前进按钮,位置 条,停止按钮或重新加载按钮.这些元素有 并排放置时的特殊行为,如果 用另一个元素分隔.

You must not insert your toolbar button between any of the following elements: the combined back/forward button, the location bar, the stop botton, or the reload button. These elements have special behaviors when placed next to eachother, and will break if separated by another element.

/**
 * Installs the toolbar button with the given ID into the given
 * toolbar, if it is not already present in the document.
 *
 * @param {string} toolbarId The ID of the toolbar to install to.
 * @param {string} id The ID of the button to install.
 * @param {string} afterId The ID of the element to insert after. @optional
 */
function installButton(toolbarId, id, afterId) {
    if (!document.getElementById(id)) {
        var toolbar = document.getElementById(toolbarId);

        // If no afterId is given, then append the item to the toolbar
        var before = null;
        if (afterId) {
            let elem = document.getElementById(afterId);
            if (elem && elem.parentNode == toolbar)
                before = elem.nextElementSibling;
        }

        toolbar.insertItem(id, before);
        toolbar.setAttribute("currentset", toolbar.currentSet);
        document.persist(toolbar.id, "currentset");

        if (toolbarId == "addon-bar")
            toolbar.collapsed = false;
    }
}

if (firstRun) {
    installButton("nav-bar", "my-extension-navbar-button");
    // The "addon-bar" is available since Firefox 4
    installButton("addon-bar", "my-extension-addon-bar-button");
}

这篇关于如何使Firefox扩展工具栏按钮自动出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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