How to view all JS code requested by an iframe?

Asked

Viewed 395 times

6

I want to exhibit a iframe with an external page and then show in a textarea all JS code requested by iframe in question.

I have two problems: the first is how to know all the requisitions that the iframe made, and then take each and show the code.

I tried to make getScript and Ajax in Javascript URL to put in textarea, but I have a problem with cross-Domain, and when I can do the request, no field comes textResponse so that I can pick up the text and display.

How do I do that? It’s possible?

I can only use language frontend, can be any library, jQuery or even pure Javascript.

  • If you worked with PHP, I would teach you a way to get all JS by domdocument ;)

  • 3

    In the browser you will always hit the cross origin problem. The only way I know how to make an external request without this problem is within a browser extension. Both Chrome and Firefox allow you to perform external requests issues. In the case of Chrome you can even capture all requests made by the page with the Extensions API.

  • What code have you made so far?

  • I ended up solving this problem with a yahoo service called YQL

  • 3

    Gabriele can put an answer with the solution you found and with an example?

  • I ended up solving this problem with a yahoo service called YQL

  • I wanted to say an answer down here, in the field of answers with an example. Thank you!

  • If it is the same domain you can get through Document.querySelector('iframe').contentWindow.window.Document if it is not as @Guilhermenagatomo said.

  • @Grabrielle, explain it better? Do you want to capture javascript inside the iframe or outside the iframe? Or you just want to capture the html code of the iframe in the textarea? Because iframe does not have javascript code, necessarily.

Show 4 more comments

1 answer

-3

Well, you can use the fetch to get the code of the requested pages.

Example:

const link = 'https://google.com/';

fetch(link)
.then( result => result.text() )
.then( HTML => {
    // ai você faz o que quiser com o codigo HTML da pagina
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.