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

﹥>﹥吖頭↗ 提交于 2019-11-28 00:12:27

问题


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

回答1:


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).




回答2:


If you use XMLHttpRequest (available via the xhr module) you can easily avoid the use of DOMParser. Bellow I provide an example supposing request is an XMLHttpRequest object which request is successfully completed:

Instead of:

var parser = new DOMParser(); 
var xmlDoc = parser.parseFromString(request.responseText, "application/xml");  

Use:

var xmlDoc = request.responseXML;  

An then you can:

var packs = xmlDoc.getElementsByTagName("packs");
console.log(packs);

Or whatever.



来源:https://stackoverflow.com/questions/9171590/how-to-parse-a-xml-string-in-a-firefox-addon-using-add-on-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!