如何让我的 Firefox 扩展工具栏按钮自动出现?

发布于 2024-07-14 05:47:42 字数 100 浏览 7 评论 0原文

我创建了一个由工具栏按钮组成的 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.

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

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

发布评论

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

评论(4

猫腻 2024-07-21 05:47:42

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

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

注释

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

只有当您的按钮能够为用户带来真正的价值并且将成为您的扩展程序的频繁入口点时,才请默认添加您的按钮。

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

/**
 * 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");
}

From 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.

Notes

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");
}
高冷爸爸 2024-07-21 05:47:42

我们正在使用以下代码......

function init() {

    // .... 

    var navbar = document.getElementById("nav-bar");
    if ((myExtensionShared.checkMyBtnInstalled() == false) &&
        (navbar != null && document.getElementById("myExtension-button") == null)) {
        var newset;
            if (navbar.getAttribute('currentset') && 
              navbar.getAttribute('currentset').indexOf('myExtension-button') == -1) {

                navbar.insertItem ('myExtension-button', null, null, false);
                newset = navbar.getAttribute('currentset') + ',myExtension-button';
                navbar.setAttribute('currentset', newset);
                document.persist('nav-bar', 'currentset');
            }
            else if (!navbar.getAttribute('currentset')) {

                navbar.insertItem ('myExtension-button', null, null, false);
                newset=navbar.getAttribute('defaultset') + ',myExtension-button';
                navbar.setAttribute('currentset', newset);
                document.persist('nav-bar', 'currentset');
            }

    }

    // .... 

}



myExtensionShared.prototype.checkMyBtnInstalled = function() {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                                       .getService(Components.interfaces.nsIPrefBranch);
    var btnInstalled = false;
    if (prefs.prefHasUserValue("extensions.myExtension.myBtnInstalled")) {
        btnInstalled = prefs.getBoolPref("extensions.myExtension.myBtnInstalled");
    }
    if (!btnInstalled) {
        prefs.setBoolPref("extensions.myExtension.myBtnInstalled", true);
    }
    return btnInstalled;
}

We're using the following code....

function init() {

    // .... 

    var navbar = document.getElementById("nav-bar");
    if ((myExtensionShared.checkMyBtnInstalled() == false) &&
        (navbar != null && document.getElementById("myExtension-button") == null)) {
        var newset;
            if (navbar.getAttribute('currentset') && 
              navbar.getAttribute('currentset').indexOf('myExtension-button') == -1) {

                navbar.insertItem ('myExtension-button', null, null, false);
                newset = navbar.getAttribute('currentset') + ',myExtension-button';
                navbar.setAttribute('currentset', newset);
                document.persist('nav-bar', 'currentset');
            }
            else if (!navbar.getAttribute('currentset')) {

                navbar.insertItem ('myExtension-button', null, null, false);
                newset=navbar.getAttribute('defaultset') + ',myExtension-button';
                navbar.setAttribute('currentset', newset);
                document.persist('nav-bar', 'currentset');
            }

    }

    // .... 

}



myExtensionShared.prototype.checkMyBtnInstalled = function() {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                                       .getService(Components.interfaces.nsIPrefBranch);
    var btnInstalled = false;
    if (prefs.prefHasUserValue("extensions.myExtension.myBtnInstalled")) {
        btnInstalled = prefs.getBoolPref("extensions.myExtension.myBtnInstalled");
    }
    if (!btnInstalled) {
        prefs.setBoolPref("extensions.myExtension.myBtnInstalled", true);
    }
    return btnInstalled;
}
就是爱搞怪 2024-07-21 05:47:42

我们使用以下代码来附加按钮(如果已经存在于栏中的其他位置)。

//...
appendButtonInToolbar:function(buttonId, toolbarId) {
    var toolbar = document.getElementById(toolbarId);
    var button = document.getElementById(buttonId);
    if(button) {
        var parentBar = button.parentNode;          
        if(parentBar && parentBar != toolbar) {
            var newset = this.removeButtonFromToolbarCurrentSet(parentBar,buttonId);              
        }
        toolbar.appendChild(button);
    }else{          
        toolbar.insertItem(buttonId);
    }

    this.appendButtonInToolbarCurrentSet(toolbar,buttonId);
},

appendButtonInToolbarCurrentSet:function(toolbar, buttonId) {
    var oldset = toolbar.getAttribute("currentset");
    var newset = "";
    if(oldset && oldset!="") {
        newset = oldset + ",";
    }        
    newset += buttonId;        
    toolbar.setAttribute("currentset", newset);
    document.persist(toolbar.id,"currentset");
    return newset;
},


removeButtonFromToolbarCurrentSet:function(toolbar, buttonId) {
    var oldset = toolbar.getAttribute("currentset");
    if(!oldset || oldset=="" || oldset.indexOf(buttonId) == -1) return oldset;
    var reg = new RegExp(buttonId+",?", "gi");        
    var newset = oldset.replace(reg,"");
    if (newset.charAt(newset.length-1) == ",") {
       newset = newset.substring(0, newset.length - 1);
    }

    toolbar.setAttribute("currentset", newset);
    document.persist(toolbar.id,"currentset");
    return newset;
}, 
//...

We are using the following code that will append the button (if already exist somewhere else in the bar).

//...
appendButtonInToolbar:function(buttonId, toolbarId) {
    var toolbar = document.getElementById(toolbarId);
    var button = document.getElementById(buttonId);
    if(button) {
        var parentBar = button.parentNode;          
        if(parentBar && parentBar != toolbar) {
            var newset = this.removeButtonFromToolbarCurrentSet(parentBar,buttonId);              
        }
        toolbar.appendChild(button);
    }else{          
        toolbar.insertItem(buttonId);
    }

    this.appendButtonInToolbarCurrentSet(toolbar,buttonId);
},

appendButtonInToolbarCurrentSet:function(toolbar, buttonId) {
    var oldset = toolbar.getAttribute("currentset");
    var newset = "";
    if(oldset && oldset!="") {
        newset = oldset + ",";
    }        
    newset += buttonId;        
    toolbar.setAttribute("currentset", newset);
    document.persist(toolbar.id,"currentset");
    return newset;
},


removeButtonFromToolbarCurrentSet:function(toolbar, buttonId) {
    var oldset = toolbar.getAttribute("currentset");
    if(!oldset || oldset=="" || oldset.indexOf(buttonId) == -1) return oldset;
    var reg = new RegExp(buttonId+",?", "gi");        
    var newset = oldset.replace(reg,"");
    if (newset.charAt(newset.length-1) == ",") {
       newset = newset.substring(0, newset.length - 1);
    }

    toolbar.setAttribute("currentset", newset);
    document.persist(toolbar.id,"currentset");
    return newset;
}, 
//...
━╋う一瞬間旳綻放 2024-07-21 05:47:42

这是我编写的一个小脚本片段,它在扩展程序第一次运行时向 Firefox 工具栏添加一个按钮: 首次运行时将扩展程序的工具栏按钮添加到 Firefox

Here is a small script snippet I write that adds a button to the Firefox toolbar the first time your extension runs: Add your extension’s toolbar button to Firefox at first run

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