0

I have a Chrome extension that comes with a popup window which, among other things, contains an iframe to load websites. In principle, I can set the src attribute of the iFrame and it works fine. The problem, however, is that many website do not allow to be loaded within frame, yielding a Refused to display '...' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN' error. Fair enough!

After some looking around I found a PHP/cURL solution to uses a simple scrip to fetch the content of a URL using cURL and returning the content, which in turn is then used as value for the src attribute. Here's the script grabber.php:

<?php
  $ch = curl_init($_GET['url']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  $content = curl_exec($ch);
  curl_close($ch); 
  echo $content;
?>

And here's the code I use to set the src attribute of the iFrame:

$('#myIFrame').attr('src', './grabber.php?url='+someURL);

Again, this work in principle, but only reasonably well for rather static / plain HTML websites. As soon as the website is very dynamic, I only get some bits and pieces, i.e., missing content and/or missing style sheets. In short, what the iFrame shows has often almost nothing to do with how the website really looks like.

Is there any chance to get this working properly for any website?

Christian
  • 3,239
  • 5
  • 38
  • 79
  • 2
    Maybe it'd be better to use chrome.webRequest API and [modify the response headers](http://stackoverflow.com/a/15534822/3959875) – wOxxOm Jul 12 '15 at 12:21
  • 1
    The response headers suggested by @wOxxOm is the right solution. For context, this has nothing to do with static vs dynamic, this has to do with what the websites have set for their X-FRAME options in their http header. It is more common in more web application type sets to set these (dynamic), because it prevents a variety of hacks (clickjacking for example). – Brian Jul 12 '15 at 13:12
  • @wOxxOm: Perfect, works pretty neat. Feel free the post this as an answer so I can accept it. I only observed one small annoyance: Some sites, when I load their URL in the iFrame, "take over" the whole popup window, not just the iFrame. Any ideas how I can suppress this? – Christian Jul 13 '15 at 01:18

0 Answers0