Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a XML string in a Firefox addon using Add-on SDK

I am trying to create a FF AddOn that brings some XML data from a website. But I can't find a way to parse my RESPONSE. First I used DOMParser but I get this error:

ReferenceError: DOMParser is not defined.

Someone suggested to use XMLHttpRequest, because the parsing is done automatically but then I get this other error:

Error: An exception occurred. Traceback (most recent call last):
File "resource://jid0-a23vmnhgidl8wlymvolsst4ca98-at-jetpack/api-utils/lib/cuddlefish.js", line 208, in require let module, manifest = this.manifest[base], requirer = this.modules[base]; TypeError: this.manifest is undefined

I really don't know what else to do. I must note that I am using the AddOn Builder to achieve this.

Below the code that doesn't seem to work.

Option 1:

exports.main = function() {

require("widget").Widget({
    id: "widgetID1",
    label: "My Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(event) {

    var Request = require("request").Request;
    var goblecontent = Request({
      url: "http://www.myexperiment.org/search.xml?query=goble",
      onComplete: function (response) {
        var parser = new DOMParser(); 
        var xml = parser.parseFromString(response.text, "application/xml");  

        var packs = xml.getElementsByTagName("packs");
        console.log(packs);
      }
    });

    goblecontent.get();

    }
});

};

Option 2:

exports.main = function() {
    require("widget").Widget({
        id: "widgetID1",
        label: "My Mozilla Widget",
        contentURL: "http://www.mozilla.org/favicon.ico",
        onClick: function(event) {

            var request = new require("xhr").XMLHttpRequest();
            request.open("GET", "http://www.myexperiment.org/search.xml?query=goble", false);
            request.send(null);  

            if (request.status === 200) {  
              console.log(request.responseText);  
            }  
        }
    });
};
like image 573
kriztean Avatar asked Feb 07 '12 05:02

kriztean


1 Answers

DOMParser constructor isn't defined in the context of SDK modules. You can still get it using chrome authority however:

var {Cc, Ci} = require("chrome");
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);

nsIDOMParser documentation.

That said, your approach with XMLHttpRequest should work as well. You used the new operator incorrectly however, the way you wrote it a new "require object" is being created. This way it should work however:

var {XMLHttpRequest} = require("xhr");
var request = new XMLHttpRequest();

Please consider using an asynchronous XMLHttpRequest object however, use request.onreadystatechange to attach your listener (the xhr module currently doesn't support other types of listeners or addEventListener).

like image 84
Wladimir Palant Avatar answered Oct 06 '22 04:10

Wladimir Palant