1

I am writing a Greasemonkey script; it works great in Chrome, but has issues only in Firefox. I am using the following code to parse a response using xmlHTTPRequest:

var parser      = new DOMParser ();
var responseDoc = parser.parseFromString (response.responseText, "text/html");

So, I can do stuff like responseDoc.getElementById, etc. This works in Chrome, but I always get undefined in Firefox. This thread sounded kind of similar, but I am not too sure the solution works (if there is a solution - not super clear):

DOMParser().parseFromString() not giving response with Firefox

Any help would be great!

Edit, sample added:

GM.xmlHttpRequest({
  method: "GET",
  url: "https://www.google.com/",
  onload: function(response) {
    alert(response.responseText);

    var parser      = new DOMParser ();
    var responseDoc = parser.parseFromString (response.responseText, "text/html");

    alert(responseDoc.innerHTML);
  }
});
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
jimjamian
  • 93
  • 1
  • 10
  • Check if responseText contains the actual HTML. Also, you can make XMLHttpRequest return a document [directly](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType). – wOxxOm Jan 04 '18 at 14:28
  • I think it does have the HTML, check the updated sample in the question. Always comes back undefined after using DomParser (same code works in chrome). Would I be able to parse the document? I want to look at the source code and use stuff like getElementById, which works just fine in chrome. Always issues in Firefox :( Thanks for the help regardless. – jimjamian Jan 04 '18 at 14:44
  • IMO this could not work in Chrome either: what is returned by `parseFromString` method is `document` (which has no `innerHTML` property) not DOM node (which has). Use `documentElement` or `body` property: `(new DOMParser()).parseFromString('

    bah','text/html').documentElement.outerHTML`

    – myf Jan 04 '18 at 15:10
  • oh! my mistake. it doesn't work on chrome (thats what you get when you rush out sample code). i changed it as you suggested and i think it's working now. regarding my main code, let me play around and see if i can get it working now - not entirely sure why it wasn't working to begin with now, but lets see. thanks! – jimjamian Jan 04 '18 at 15:27

0 Answers0