Firefox加载项按钮代码不工作-Javascript

Firefox Addon Button Code Not Working - Javascript

本文关键字:工作 -Javascript 代码 加载项 按钮 Firefox      更新时间:2023-09-26

我正在创建一个Firefox扩展,该扩展旨在自动在工具栏上放置一个按钮,当单击该按钮时,将在新选项卡中打开一个网页。我使用了Mozilla开发网站上的代码片段,但当两者放在一起时,只有按钮的位置有效。单击该按钮时将不起任何作用。

我对javascript不太了解,所以我不知道这里出了什么问题。整个扩展已通过所有Mozilla验证检查,没有任何错误和警告。

这是代码。如能提供任何帮助,我们将不胜感激。

CustomButton = {
1: 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 (firstRun) {
    installButton("nav-bar", "my-extension-navbar-button");
}
},
2: function () {
const url = "http://www.mysite.com/"
document
    .getElementById("content")
.webNavigation
.loadURI(url, 0, null, null, null)
}
}

我对此不是很清楚,这就是为什么我在这里问这个问题,而不是搜索其他对我来说毫无意义的例子。如果有人能告诉我如何修改这个特定的代码来完成我需要它做的事情,我将不胜感激。

这将从2014年4月发布的FF 29开始工作它向工具栏添加了一个操作按钮,一旦您单击它,它将加载http://www.example.com新选项卡中的站点;如果您喜欢加载一个新窗口,请使用require("sdk/windows")。

使用插件sdk

var ui = require("sdk/ui");
var action_button = ui.ActionButton({
    id: "my-button",
    label: "Open example page!",
    icon: "./icon.png",
    onClick: function() {
        require("sdk/tabs").open("http://www.example.com");
    }
});