1

I built a chrome extension and everything worked well. Now i need to put it on firefox, and it's a f*** mess.

The problem is with dom parsing.

Her's the code that doesn't work on FF :

var parser = new DOMParser();
SOURCE_DOM = parser.parseFromString(data.url, "text/html");

SOURCE_DOM always return an object empty :

Object : {location : null}

On chrome there's no problem with that, it gives me the document object and i can properly work with it. But Firefox is a pain in the ass compared to chrome when it comes to extension building.

Someone would know how to get the document ?

  • 2
    is `data.url` a string containing HTML? – Jaromanda X Sep 29 '15 at 10:06
  • I've run that code snippet in chrome and firefox - both work identically - as long as `data.url` is a string containing something that could be considered even close to HTML – Jaromanda X Sep 29 '15 at 10:22
  • Yeah, the only way I could imagine this failing is if `data.url` were some object other than a string. Because HTML parsers in browsers (regardless of if they’re called from `.parseFromString(…)` whatever other means) are pretty much going to parse any string into a HTML DOM (with at least `html` add `head` and `body` elements) without failing. – sideshowbarker Sep 29 '15 at 10:29
  • Are you doing this from bootstrap.js? Or are you making an addon-sdk addon? If you are using from bootstrap.js or jsm modules then you need the privelaged parser: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMParser firefox is more powerful, you can't get away with copy pasting code, you need to understand it most of the time. but you'll get the hang of it. chrome puts you in a one scope above content scope. in firefox you are in the most privelaged scope and can go into any other scope you need. the usual problem for newcomers is scope – Noitidart Sep 29 '15 at 16:29
  • data.url contains the current page html, as a string. I've run it in firefox and it works too, but here i'm in firefox addon. – Félix Marchenay Sep 29 '15 at 18:09
  • Noitidart : yeah i think this is all about scopes. Chrome is way better when it comes to scopes. i tried to load nsIDOMParser but "Components.classes" is undefined .. And the method parser.parseFromString() is a defined function, i checked it. So why can't i parse it ... my whole extension is about getting information from the current webpage :( – Félix Marchenay Sep 29 '15 at 18:13
  • Ahhh yeah I know what you're doing now. You're in SDK, You have to use `Cc` and you have to get `Cc` by putting at top `var {Cc, Cu, Ci} = require('chrome')`. Then you can import the `nsIDOMParser` with `var parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);`. The word is not Chrome is "better", but rather Chrome is "dumber" and "close-source" and "hiding", they don't share with you everything that is going on. They just give you one of the limited scopes to play with. I replied late because I wasn't notified by Stackove, you have to put a `@` before my name. :) – Noitidart Oct 01 '15 at 23:14
  • Thanks for everything @Noitidart !! I'll try today ! Your explanations are very clear even for a french coder ! :) – Félix Marchenay Oct 02 '15 at 10:59
  • I don't understand. I put `var {Cc, Cu, Ci} = require('chrome');` in the index.js, but in the popup.js (the contentURL of the panel) i can't reach Cc. So i can't get a parser object. WHY DOMParser doesn't work ?! There is something mysterious ... I just want to get the dom (as a document object) from the current tab, IN the panel. Why is it so hard @Noitidart ?? – Félix Marchenay Oct 02 '15 at 22:11

1 Answers1

0

Use the code below

if (window.DOMParser) {
    var parser=new window.DOMParser();
    var parsererrorNS = null;
    // IE9+ now is here
    if(!isIEParser) {
        try {
            parsererrorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0].namespaceURI;
        }
        catch(err) {
            parsererrorNS = null;
        }
    }
    try {
        xmlDoc = parser.parseFromString( xmlDocStr, "text/xml" );
        if( parsererrorNS!= null && xmlDoc.getElementsByTagNameNS(parsererrorNS, "parsererror").length > 0) {
            //throw new Error('Error parsing XML: '+xmlDocStr);
            xmlDoc = null;
        }
    }
    catch(err) {
        xmlDoc = null;
    }
} else {
    // IE :(
    if(xmlDocStr.indexOf("<?")==0) {
        xmlDocStr = xmlDocStr.substr( xmlDocStr.indexOf("?>") + 2 );
    }
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(xmlDocStr);
}
Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
Habib Adıbelli
  • 1,181
  • 7
  • 14