سؤال

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

and so on.

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();
            }
        }
    } 
);    

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.

هل كانت مفيدة؟

المحلول

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.

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
    See Content scripts to learn how to work with the sdk/page-mod module and pass around messages.

  • chrome.browserAction.setIcon and chrome.extension.onMessage.addListener
    I've written a SDK module which is a full implementation of Chrome's chrome.browserAction API for Firefox. Installation instructions and documentation is found at https://github.com/Rob--W/browser-action-jplib.

  • webkitNotifications.createNotification
    See the sdk/notifications module.

  • 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
    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
    The sdk/tabs module is an iterator. You can loop through the object and filter out the results yourself.

  • localStorage
    See sdk/simple-storage

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top