firefox扩展可以通过sdk工作,但在浏览器兼容性问题中安装时不行

firefox extension works through sdk but not when installed in browser - compatibility issue?

本文关键字:问题 兼容性 安装 浏览器 可以通过 扩展 sdk 工作 firefox      更新时间:2023-09-26

---更新----

在对此进行了更多的实验之后,我确定我编写的contentScript不是问题所在。例如,如果我将扩展简化为:

var buttons = require('sdk/ui/button/action');
var data = require("sdk/self").data;
var self = require("sdk/self");

var button = buttons.ActionButton({
id: "library-link",
label: "External Resource Locator",
icon: self.data.url("icon-16.png"),
  });

当我通过SDK运行扩展时,该按钮仍然会出现,但当我在当前的firefox浏览器(在某些平台上,版本38(中安装xpi时,它不会出现。这个问题似乎发生在他们设计过程中的一个非常基本的层面上。


我正在尝试为firefox编写一个简单的扩展,它将一个表单附加到当前页面并将数据发布到另一个网站。它可以通过操作按钮调用,也可以通过上下文菜单项调用。

我一直在使用插件sdk进行开发,当我使用cfx run测试它时,它运行得很好。然而,在执行cfx xpi并将扩展安装到我的firefox浏览器中后,它根本不起作用。操作按钮和上下文菜单项不会出现,尽管扩展显示在已安装和启用的add-ons->extensions下,但与xpi打包的任何图像都不会显示。

我不确定是什么原因导致了这种情况,而且我的代码相当简短,所以我将添加我的整个main.js:

var buttons = require('sdk/ui/button/action');
var data = require("sdk/self").data;
var contextMenu = require("sdk/context-menu");
var self = require("sdk/self");
var menuItem = contextMenu.Item({
                            label: "Look for selected text in the Library of Babel",
                            context: contextMenu.SelectionContext(),
                             contentScript: 'self.on("click", function () {' +
                            'var text = window.getSelection().toString();' +
                            'var formext = document.createElement("form");' +
                            'formext.setAttribute("method", "POST");' +
                            'formext.setAttribute("action", "https://libraryofbabel.info/resourcelocator.cgi");' +
                            'var hiddenField = document.createElement("input");' +
                            ' hiddenField.setAttribute("type", "hidden");' +
                             'hiddenField.setAttribute("name", "extension");' +
                            ' hiddenField.setAttribute("value", window.getSelection().toString());' +
                            ' formext.appendChild(hiddenField);' +
                            ' document.body.appendChild(formext);' +
                            ' formext.submit();' +
                            '});',
                            image: self.data.url("icon-16.png")
                            });
var button = buttons.ActionButton({
id: "library-link",
label: "External Resource Locator",
icon: {
"16": "./icon-16.png",
"32": "./icon-32.png",
"64": "./icon-64.png"
},
onClick: function() {
 require("sdk/tabs").activeTab.attach({
                              contentScriptFile: data.url("form.js")
                               });
                              }
                              });

我注意到,当我运行cfx xpi时,自动生成的install.rdf文件显示兼容性的最大版本是30.0。然而,我也发现,在一些运行firefox版本(包括38版本(的计算机上,它将完美工作。这段代码中是否有任何内容会阻止与较新版本的firefox的兼容性?我将添加ContentScriptFile,以防可能负责:

function getSelectedText() {
var text = "";
if (window.getSelection) {
    text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
    text = document.selection.createRange().text;
}
return text;
}
var bodytext = document.getElementsByTagName("BODY")[0];
var formext = document.createElement("form");
formext.setAttribute("method", "POST");
formext.setAttribute("action", "https://libraryofbabel.info/resourcelocator.cgi");
//formext.setAttribute("target","_blank");
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "extension");
hiddenField.setAttribute("value", getSelectedText() || document.body.innerHTML); // take selected text OR bodytext
formext.appendChild(hiddenField);
document.body.appendChild(formext);
formext.submit();
  1. 在图标所在的工具栏上打开上下文菜单,选择Customize...。在打开的窗口中,你能在"其他工具和功能"中看到你的图标吗?如果是,那么这意味着firefox在您开发插件时记得图标的缺失。您可以手动将图标放在工具栏上。我相信普通用户不会面临这个问题
  2. install.rdf中手动更改em:maxVersion
  3. 按照设置扩展开发环境中的描述配置您的firefox,即至少以下内容:

    • javascript.options.showInConsole = true在F12控制台中显示插件错误
    • javascript.options.strict = true在控制台中有更多警告
    • extensions.logging.enabled = true在控制台中存在安装/更新问题

    之后,重新启动firefox,看看是否可以从控制台获得任何有用的东西。禁用其他插件以去除噪声。

  4. 尝试备份并删除整个firefox的配置文件文件夹,以使firefox 100%干净。它有帮助吗?如果是的话,那就把问题缩小到侧面的问题
  5. 尝试更改插件的所有标识符(包括插件名称、ID、说明、按钮ID和说明等(,从而创建一个新的重复插件。这有帮助吗?如果是,那很可能意味着firefox在你玩插件时记住了它的一些设置