You can't do this in the general case
...because of the Same Origin Policy that browsers use to restrict access to Site B from code running on Site A. When you try (with XMLHttpRequest
or fetch
etc.), you'll get an error saying something along the lines of:
No 'Access-Control-Allow-Origin' header is present on the requested resource
More in this question's answers and the SOP link above. But basically: It's not about preventing people seeing Site B's content, it's about not being able to do so from a context (a browser) that may have stored authentication information for the current user that would reveal their private information if you could read Site B's content from that user's browser.
A very small number of websites may serve their content using Cross Origin Resource Sharing headers (the aforementioned Access-Control-Allow-Origin
and others) to allow any site to read their content, but that's very unusual.
If you want to retrieve the content of the vast majority of sites, you'll have to use code running in a non-browser context to do it (for instance, code running on a server somewhere). Over the years there have been many sites that allowed you to query them with a URL and return you the content of that URL queried by their server (and so not subject to the SOP, because the current user's browser-based authentication information for the other site isn't used), but they tend to spring up and then go away again as there's not really a good revenue model to support the bandwidth requirements. After all, why pay someone else for that when you can just run your own server for cheap (or even free) and do it yourself.