0

I want to have JavaScript to get a user's URL and return the source code of the website. I want to have this to essentially make an iframe, but with the actual code.

E.g. :

let userUrl = prompt("What website do you want displayed?","e.g. https://www.google.com");

function getSource(url){

//some code
return pageSource;
}
document.body.innerHtml=getSource(userUrl);

I tried to scrape a view page source website and turn it into an API that I could inject into JavaScript, but I had no luck.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
henxdl
  • 118
  • 14
  • Does this answer your question? [How to get html source code from external url](https://stackoverflow.com/questions/5289027/how-to-get-html-source-code-from-external-url) – Laurel Apr 05 '22 at 20:45

1 Answers1

1

<html>
<head><meta charset="us-ascii">
    <title></title>
<script>

let userUrl = prompt("What website do you want displayed?","e.g. https://www.google.com");

if (userUrl){

    var srcFrame=""; //complete page code to inject into your iframe or return

    var fetchMe = "https://api.codetabs.com/v1/proxy?quest=" + userUrl;

    fetch(fetchMe).then((response) => response.text()).then((text) => {
        srcFrame = text;

        //if injecting into iframe
        myFrame.document.open();
        myFrame.document.write(srcFrame);
        myFrame.document.close();



        // USE THE FOLLOWING TO ADD THE ORIGINAL URL AS THE baseURI SO RELATIVE 
        //LINKS & SCRIPTS WILL WORK AND ACCESS THE ORIGINAL SOURCE AND NOT YOUR 
        //SERVER
        var addOrigBase= document.createElement('base');
        addOrigBase.setAttribute('href',userUrl);
    
   document.getElementById("myFrame").contentDocument.head.appendChild(addOrigBase);

    });

}


</script>
</head>
<body>
<iframe onload="myFrame.frameElement.height = 
(myFrame.frameElement.contentDocument.body.clientHeight)+10" frameborder="" 
height="25px" id="myFrame" name="myFrame" scrolling="no" src="about:blank"  
width="100%"></iframe>
</body>
</html>
henxdl
  • 118
  • 14
  • This doesn't seem to work for me [here](https://jsfiddle.net/kdpnxt6b); do you have a working link for _me_ like [JSFiddle](https://jsfiddle.net)? – henxdl Nov 28 '22 at 13:52