为了通知用户可能的冲突,我想让我的插件检查是否已安装并启用了另一个插件。如果是这样,我可以根据用户的要求禁用它或自己禁用它:

function disableExtension(id) {
    var man = Components.classes["@mozilla.org/extensions/manager;1"];
    if (man) {
        man = man.getService(Components.interfaces.nsIExtensionManager);
    }
    if (man) {
        man.disableItem(id);
    } else {
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            addon.userDisabled = true;
        });
    }
}

但是我首先当然要检查是否安装了某些其他加载项。目前,我这样做如下:
if (Application.extensions) {
    // Gecko 1.9.2 and older
    ext = Application.extensions.get(id);
    if (ext) {
        // TODO check if extension is also enabled
        disableExtension(id);
    }
} else {
    // Gecko 2.0.0 and newer
    Application.getExtensions(function(extensions) {
        ext = extensions.get(id);
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            if (!addon.userDisabled) {
                disableExtension(id);
            }
        });
    })
}

Firefox 4的代码(else -statement)可以正常工作。对于Firefox的旧版本(3.5和更早的版本),我一生都无法弄清楚如何确定该扩展程序是否已安装。

有人知道怎么做这个吗?

最佳答案

答案很简单。我忽略了enabledextIExtension属性。

我解决如下:

var ext;
if (typeof Application != 'undefined') {
    if (Application.extensions) {
        // Gecko 1.9.0 - 1.9.2
        ext = Application.extensions.get(id);
        if (ext) {
            if (ext.enabled) disableExtension(id);
        }
    } else {
        // Gecko 2.0.0 and newer
        Application.getExtensions(function(extensions) {
            ext = extensions.get(id);
            Components.utils.import("resource://gre/modules/AddonManager.jsm");
            AddonManager.getAddonByID(id, function(addon) {
                if (!addon.userDisabled) {
                    disableExtension(id);
                }
            });
        })
    }
} else {
    // Gecko 1.8.0
    var extMgr = Cc["@mozilla.org/extensions/manager;1"].getService(Ci.nsIExtensionManager);
    if (extMgr) {
        ext = extMgr.getItemForID(id);
    }
    var extMgrDs = extMgr.datasource;
    if (extMgrDs) {
        var rdfSvc = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
        if (rdfSvc && ext) {
            var source = rdfSvc.GetResource("urn:mozilla:item:" + ext.id);
            var property = rdfSvc.GetResource("http://www.mozilla.org/2004/em-rdf#isDisabled");
            var target = rdfSvc.GetLiteral("true");
            var disabled = extMgrDs.HasAssertion(source, property, target, true);
            if (!disabled) {
                disableExtension(id);
            }
        }
    } else if (typeof className != "undefined") {
        // Opens the add-on window
        BrowserOpenAddonsMgr();
    }
}

其中disableExtension()是:
disableExtension: function(id) {
    var man = Components.classes["@mozilla.org/extensions/manager;1"];
    if (man) {
        man = man.getService(Components.interfaces.nsIExtensionManager);
    }
    if (man) {
        man.disableItem(id);
        restart();
    } else {
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            addon.userDisabled = true;
            restart();
        });
    }
}
restart()是:
restart: function() {
    var appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"];
    if (appStartup) {
        appStartup = appStartup.getService(Components.interfaces.nsIAppStartup);
    }
    if (appStartup) {
        appStartup.quit(appStartup.eAttemptQuit | appStartup.eRestart);
    } else if (typeof Application != 'undefined') {
        if (Application.restart) Application.restart();
    }
}

尚未在Firefox 1.0-1.5上进行过测试,但可以在以下环境中使用:
  • Firefox 4.0b7
  • Firefox 3.6
  • Firefox 3.5
  • Firefox 3.0
  • Firefox 2.0

  • helping me out的版权归AtteKemppilä所有。

    08-18 22:07
    查看更多