Search code examples
javascriptjqueryfirefox-addonfirefox-addon-sdk

how to parse a html string with Firefox add-on SDK without opening any window


I downloaded jquery module from github, only to find that a lot of functions and objects are undefined.

  var Request = require("sdk/request").Request;
  var Jquery = require("jquery");
  var latestTweetRequest = Request({
  url: "https://example.com",
  onComplete: function (response) {
    var abc = Jquery.jquery(response);
  }
});

Is there any light-weight lib can help me


Solution

  • You can use built-in DOMParser awesome topic and solution by MJ Saedy:

    https://stackoverflow.com/a/23207820/1828637

    documenation on it here: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser?redirectlocale=en-US&redirectslug=DOM%2FDOMParser

    I'm not sure how to do this from XPCOM though but there has to be a way.

    edit: found out how to do it from XPCOM and addon-sdk.

    do it like this:

    var DOMParser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
    

    see here for addon sdk details: https://stackoverflow.com/a/9172947/1828637

    111 /**
    112  * Parse the given string into a DOM tree
    113  *
    114  * @param str       The string to parse
    115  * @param docUri    (optional) The document URI to use
    116  * @param baseUri   (optional) The base URI to use
    117  * @return          The parsed DOM Document
    118  */
    119 cal.xml.parseString = function(str, docUri, baseUri) {
    120     let parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
    121                            .createInstance(Components.interfaces.nsIDOMParser);
    122 
    123     parser.init(null, docUri, baseUri);
    124     return parser.parseFromString(str, "application/xml");
    125 };
    

    http://mxr.mozilla.org/comm-central/source/calendar/base/modules/calXMLUtils.jsm#120