如何将一个Chrome扩展(Chrome浏览器专用的API),以一个Firefox插件? [英] How to convert a Chrome extension (with Chrome-specific APIs) to a Firefox add-on?

查看:905
本文介绍了如何将一个Chrome扩展(Chrome浏览器专用的API),以一个Firefox插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦复杂的Chrome扩展转换到Firefox插件。在Chrome扩展,笔者采用了一些镀铬的API,如

I'm having trouble converting a complex Chrome extension to a Firefox add-on. In the Chrome extension, the author uses some Chrome APIs such as


  • chrome.extension.sendMessage

  • chrome.browserAction.setIcon

  • webkitNotifications.createNotification

  • chrome.tabs.sendMessage

  • chrome.extension.onMessage.addListener

  • chrome.browserAction.onClicked.addListener

  • chrome.privacy.services.autofillEnabled.get

  • chrome.runtime.onInstalled.addListener

  • chrome.tabs.query

  • 的localStorage

  • chrome.extension.sendMessage
  • chrome.browserAction.setIcon
  • webkitNotifications.createNotification
  • chrome.tabs.sendMessage
  • chrome.extension.onMessage.addListener
  • chrome.browserAction.onClicked.addListener
  • chrome.privacy.services.autofillEnabled.get
  • chrome.runtime.onInstalled.addListener
  • chrome.tabs.query
  • localStorage

等。

我不知道如何来实现这些方法在Firefox插件code。一些这些API可以用一个回调函数被调用。例如:

I don't know how to implement these methods in Firefox add-on code. Some of these APIs can be called with a call-back function. For example:

chrome.extension.sendMessage ( {
        type: "get.identitieswithurl",
        originUrl: lgmIdentitySave.originUrl,
        actionUrl: lgmIdentitySave.actionUrl
    }, 
    function (resultIdentities) {
        if(resultIdentities != null && resultIdentities.length > 0) {
            lgmIdentitiesPage = resultIdentities;
            fillFormsWithCredential(resultIdentities[0]);
            if(resultIdentities.length > 1 && lgmSuggestionSelectIdDismissed == false) {
                showSelectIdentitySuggestionBar();
            }
        }
    } 
);    

从<一个href=\"http://stackoverflow.com/questions/11107921/communicating-with-a-content-script-in-a-firefox-extension\">this问题,我知道火狐code可以这样写

From this question, I know that the Firefox code can be written like

// main add-on script
pageMod.PageMod({
    include: "*.org",
    contentScriptFile: self.data.url("my-script.js"),
    // Send the content script a message inside onAttach
    onAttach: function (worker) {
        worker.port.emit("replacePage", "Page matches ruleset");
    }
});    

但我仍然不知道如何添加回调函数。

But I still don't know how to add the callback function.

推荐答案

让我们通过您的清单。整个答案,我会请参阅相关的<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/index.html\">documentation.如果你在开始使用Firefox插件开发complelely毫无章法,阅读<一href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/index.html#getting-started\">Getting开始教程。

Let's go through your list. Throughout the answer, I will refer to the relevant documentation. If you're complelely clueless on getting started with Firefox add-on development, read the Getting started tutorial.

开始之前,也看过关于<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/modules.html\">modules和<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/index.html#sdk-idioms\">SDK成语(最后一个是非常重要的,因为它给了Firefox插件SDK世界上重要的概念的解释)。

Before starting, also read about modules and the SDK Idioms (the last one is very important, because it gives an explanation on important concepts in the Firefox add-on SDK world).


  • chrome.extension.sendMessage / chrome.tabs.sendMessage /
    chrome.extension.onMessage.addListener 结果
    见<一href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/index.html#content-scripts\">Content脚本,了解如何与<一个工作href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/page-mod.html\"><$c$c>sdk/page-mod模块并通过围绕着消息。

  • chrome.extension.sendMessage / chrome.tabs.sendMessage / chrome.extension.onMessage.addListener
    See Content scripts to learn how to work with the sdk/page-mod module and pass around messages.

chrome.browserAction.setIcon chrome.extension.onMessage.addListener 结果
我写了一个SDK模块,这是一个全面落实Chrome的的<一个href=\"https://developer.chrome.com/extensions/browserAction.html\"><$c$c>chrome.browserAction API的Firefox浏览器。安装说明和文件在<一个被发现href=\"https://github.com/Rob--W/browser-action-jplib\">https://github.com/Rob--W/browser-action-jplib.

webkitNotifications.createNotification 结果
请参阅<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/notifications.html\"><$c$c>sdk/notifications模块。

chrome.privacy.services.autofillEnabled.get 结果
参见 SDK / preferences /服务模块。配置项文章:preference标识符的列表可以在有关被发现。为了您的具体的例子:

chrome.privacy.services.autofillEnabled.get
See sdk/preferences/service module. A list of preference identifiers can be found at the about:config entries article. For your specific example:

var prefName = 'browser.formfill.enable';
var prefService = require("sdk/preferences/service");
var isAutoFillEnabled = prefService.get(prefName); // true or false


  • chrome.runtime.onInstalled.addListener 结果
    是否已安装分机可以通过阅读来确定<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/self.html#loadReason\"><$c$c>loadReason模块
    ,财产例如

  • chrome.runtime.onInstalled.addListener
    Whether your extension was installed can be determined by reading the loadReason property of the self module, e.g.

    if (require('sdk/self').loadReason == 'install') {
        // Do something on the very first install
    }
    


  • chrome.tabs.query 结果
    在<一个href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/tabs.html\"><$c$c>sdk/tabs模块是一个迭代器。您可以通过对象循环和过滤出来的结果自己。

  • chrome.tabs.query
    The sdk/tabs module is an iterator. You can loop through the object and filter out the results yourself.

    的localStorage 结果
    见<一href=\"https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/simple-storage.html\"><$c$c>sdk/simple-storage

    这篇关于如何将一个Chrome扩展(Chrome浏览器专用的API),以一个Firefox插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆