Home > Software design >  Given a URL how do I receive the document object from it?
Given a URL how do I receive the document object from it?

Time:01-28

I am performing xpath in the browser with document.evaluate(xpath, document, ..). But now I retrieve some URLS from the page and want to perform document.evaluate(xpath, document, ..) on them too without opening the URL in the browser itself or leaving the current page

Is it even possible to do this? It seems like it's not possible to perform something like markup.evaluate(xpath, document, ..) if I manage to retrieve the markup of a URL. Can I retrieve the document of a URL?

CodePudding user response:

... and want to perform document.evaluate(xpath, document, ..) on them too without opening the URL in the browser itself or leaving the current page

Is it even possible to do this?

Probably not. It's possible only if either A) The URLs are from the same origin as the page you're doing this on, or B) The server(s) for the origin(s) the URLs are on passlist the origin of your page with CORS, relaxing the Same Origin Policy for your origin.

If one of those things is true, though, you can do it by using ajax (e.g., fetch) to fetch the content from the URL and DOMParser to parse it into a document, which you can then query.

But again, if neither of those things is true, you can't do this from browser-hosted JavaScript. You could from Node.js or simlar.

  •  Tags:  
  • Related