1

The element I am looking for:

<h1 id="itemSummaryPrice" style="color: rgb(255, 255, 255);">42 - 47</h1>

I wanna make a chrome extension that uses XMLHttpRequest GET METHOD for checking a value on another Website, i know i can give the URL, parameters for scaling down the response. So my question is how do I make an URL that gives me only the element in the response that I am looking for ? if its even possible.

Tom ale
  • 45
  • 1
  • 8

1 Answers1

1

You can't request only one part (node) of a document. You'll have to request the whole thing, parse it then select from it what you want:

var request = new XMLHttpRequest();
request.open("GET", yourUrl);

request.onload = function() {
    var doc = new DOMParser().parseFromString(request.responseText, "text/html");
    var el = doc.getElementById("itemSummaryPrice"); // the element you want
}

request.send(null);

Or you can set the responseType to "document" and let the XMLHttpRequest do the parsing internally, then use responseXML:

var request = new XMLHttpRequest();
request.open("GET", yourUrl);
request.responseType = "document";

request.onload = function() {
    var doc = request.responseXML;
    var el = doc.getElementById("itemSummaryPrice"); // the element you want
}

request.send(null);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • The last problem I am having is, I can't get the text, the "42 - 47" of my snippet. Do you have an idea how or what I should look for when asking for var el ? I tried .innerText but no chances. – Tom ale May 05 '18 at 17:19
  • no, your code is correct but the var el gives this element back:

    But I needed the value in the innerText, but it seems gone
    – Tom ale May 05 '18 at 17:25
  • @Tomale Try `el.textContent`. – ibrahim mahrir May 05 '18 at 17:27
  • nop, its also not working it seems as if the element that i get from the getElment is just taking the

    and ignoring the other values in the html element ...
    – Tom ale May 05 '18 at 17:32
  • @Tomale I think the server is sending you that element without any content. Or, worse, the content of the element is populated later by a script (which won't be executed if you'are using a parser). Read more about it [**here**](https://stackoverflow.com/q/28112807/6647153). – ibrahim mahrir May 05 '18 at 17:48