How to create DOM object from html page received over XMLHttpRequest?

前端 未结 1 1261
旧巷少年郎
旧巷少年郎 2021-01-03 10:30

I\'m developing a chromium extension so I have cross-host permissions for XMLHttpRequests for the domains I\'m asking permissions for.

I have used X

相关标签:
1条回答
  • 2021-01-03 10:57

    The DOMParser is choking on the DOCTYPE definition. It would also error on any other non-xhtml markup such as a <link> without a closing /. Do you have control over the document being sent? If not, your best bet is to parse it as a string. Use regular expressions to find what you are looking for.

    Edit: You can get the browser to parse the contents of the body for you by injecting it into a hidden div:

    var hidden = document.body.appendChild(document.createElement("div"));
    hidden.style.display = "none";
    hidden.innerHTML = /<body[^>]*>([\s\S]+)<\/body>/i(xhr.responseText)[1];
    

    Now search inside hidden to find what you're looking for:

    var myEl = hidden.querySelector("table.foo > tr > td.bar > span.fu");
    var myVal = myEl.innerHTML;
    
    0 讨论(0)
提交回复
热议问题