Firefox:以编程方式添加搜索引擎并立即使用

标签 firefox firefox-addon search-engine

我开发了一个 Firefox 插件,该插件在 Firefox 24 中无法使用。

我的插件根据一些用户设置将搜索引擎添加到搜索栏。为了添加搜索引擎,我使用 addEngine()来自nsIBrowserSearchService

直到Firefox 23,该功能还选择了添加的引擎,因此用户可以立即使用它。从 Firefox 24 开始,此行为停止:正在添加引擎,但不再选择引擎。

但是,documentation still says :

... the new engine will be used right away automatically.

我该怎么做才能强制在 Firefox 24 中立即使用新引擎?

最佳答案

一个小解决方法是在添加新搜索引擎后直接更改默认搜索引擎的首选项:

browser.search.defaultenginename

此首选项采用搜索引擎的确切名称。

此外,this MDN tutorial 中有有关添加搜索引擎的更多信息。 :

function startup(data, reason) {
    firstRun = reason == ADDON_INSTALL;
    // Re-select the search engine if this is the first run
    // or we're being re-enabled.
    selectSearch = firstRun || reason == ADDON_ENABLE;

    // Only add the engine if it doesn't already exist.
    if (!Services.search.getEngineByName(ENGINE_DETAILS.name)) {
        Services.search.addEngineWithDetails.apply(Services.search,
            ["name", "iconURL", "alias", "description", "method", "url"].map(
                function (k) ENGINE_DETAILS[k]))
    }

    let engine = Services.search.getEngineByName(ENGINE_DETAILS.name);

    // If the engine is not hidden and this is the first run, move
    // it to the first position in the engine list and select it
    if (selectSearch && !engine.hidden) {
        Services.search.moveEngine(engine, 0);
        Services.search.currentEngine = engine;
    }
}

关于Firefox:以编程方式添加搜索引擎并立即使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171103/

相关文章:

seo - 是否有必要为旧的索引网址生成站点地图?

ruby-on-rails - 重定向大量索引链接

firefox - 在 Firefox 和 Chrome 中查看来自 WCF 数据服务的原始 XML

css - 图像在 firefox 中奇怪地拉伸(stretch)

html - Firefox 4 必需的输入表单红色边框/轮廓

firefox - 如何更改Firebug的字体?

javascript - Firefox 扩展中的 __defineGetter__ 未定义

javascript - TypeError document.querySelector(...) 为空

php - 限制 post sql 结果

c++ - 从 C/C++ 控制 Firefox