1

I am currently trying to build a little chrome extension that needs to get the HTML source code of an external webpage.

To achieve getting the HTML code, I first tried the casual way with the XMLHttpRequest, but I'm facing the Same Origin Policy since it is another domain.

I tried to search for a solution on StackOverflow and found this topic discussing the same subject: Can Javascript read the source of any web page?.

The thing is the YQL is no longer supported and the Jquery solution can grab the page you want but you need to do it via the proxy of your domain, and since I'm trying to do that in a chrome extension, I don't have any server and proxy to use.

How would you do it guys?

EDIT: I did a mistake explaining my problem: In my memory, the problem with the XMLHttpRequest was the Same-Origin-Policy, but I just tried again to do it and it turns out it is the CORS policy :

Error : Access to XMLHttpRequest at 'https://www.mozilla.org/' (redirected from 'http://www.mozilla.org/') from origin 'chrome-extension://mllkahpgmeneacfkpplnapkplfciefjc' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Cyanae
  • 19
  • 2
  • 1
    Use a proxy.... – mplungjan Jun 28 '19 at 13:50
  • 1
    Maybe this will help you: https://stackoverflow.com/questions/13913824/javascript-get-current-page-current-source – kSp Jun 28 '19 at 13:51
  • 1
    Sounds like you're trying to do it in a content script (no longer possible) or something else is wrong in your code. Extensions can perform XMLHttpRequest/fetch directly, provided you added the necessary host permissions in manifest.json, but you need to do it in an internal script like the background script or a browser_action popup script. Yep, it's this simple. – wOxxOm Jun 28 '19 at 14:11

0 Answers0