I'm trying to make a tampermonkey script using firefox in which i use an xmlhttpRequest to retrieve external information, but I keep getting the same error in the console: "Uncaught DOMException: Node cannot be used in a document other than the one in which it was created". I don't want to use the responseText to look for the information I need, I want to turn it into a DOM on which I can apply evaluate and refer to elements. But the way I'm doing it doesn't seem to work. Anybody knows how I can handle this?
GM_xmlhttpRequest({
method: "get",
url: someurl,
onload: function(responseDetails) {
if(responseDetails.readyState == 4 && responseDetails.status == 200){
var parser = new DOMParser()
var to_DOM = parser.parseFromString(responseDetails.responseText, "text/html")
alert(to_DOM.evaluate("//tr[@bgcolor]/b[1]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null).snapshotItem(0).innerText)
}
}
})
You are referencing the document that is loaded in the browser, not the document of what you created.
const parser = new DOMParser()
const responseText = "<html><body><p><strong>Hello World</strong><p></body></html>";
const doc = parser.parseFromString(responseText, "text/html");
console.log(document.evaluate("//p/strong", doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0).innerText)