【问题标题】:Check if a Firefox 3.5 add-on is enabled检查是否启用了 Firefox 3.5 附加组件
【发布时间】:2010-11-28 03:24:01
【问题描述】:

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

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 和更早版本),我一生都无法弄清楚如何确定是否确实安装了扩展。

有人知道怎么做吗?

【问题讨论】:

  • 唯一想到的就是运行extensions.ini并寻找扩展的getInstallLocation(),但我一点也不喜欢这种方法。

标签: javascript firefox firefox-addon xpcom


【解决方案1】:

答案是微不足道的;我忽略了extIExtensionenabled 属性。

我是这样解决的:

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
  • 火狐3.6
  • 火狐3.5
  • 火狐3.0
  • 火狐2.0

helping me out 归功于 Atte Kemppilä。

【讨论】:

  • 顺便说一句,我不建议在未经用户同意的情况下禁用加载项并重新启动 Firefox。上面的代码被简化了,在我的插件中我使用通知来引导用户完成这些步骤。
【解决方案2】:

非常翔实的文章。
请注意,您也可以为 Gecko 2 使用“启用”属性。这使得代码更简单一些。

   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);
            if (ext.enabled) disableExtension(id);
        });
    }

为了获得最佳实践,您当然可以将重复的代码移至外部函数。

【讨论】:

  • 我还没有尝试过这个,但它似乎可以工作,因为 Mozilla 非常擅长保持向后兼容性。有机会我会试一试并发布结果。
猜你喜欢
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 2018-09-28
  • 1970-01-01
  • 2019-05-25
  • 2011-10-03
  • 1970-01-01
相关资源
最近更新 更多