Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing cURL to retrieve website and bypass same origin restriction, inserting javascript
    primarykey
    data
    text
    <p>I need to load several websites in iframes whilst also adding a google translate plugin into each page so they can be translated. Here's my code for the insertion part:</p> <pre><code>&lt;iframe onload="googleJS1(); googleJS2(); googleJS3();" class=iframe2 src=http://localhost:8888/mysitep&gt;&lt;/iframe&gt; &lt;script&gt; function googleJS1() { var iframe = document.getElementsByTagName('iframe')[0]; var doc = iframe.contentWindow.document; var newScript = doc.createElement('div'); newScript.setAttribute("id", "google_translate_element"); var bodyClass = doc.getElementsByTagName('body')[0]; bodyClass.insertBefore(newScript, bodyClass.childNodes[0]); } function googleJS2() { var iframe = document.getElementsByTagName('iframe')[0]; var doc = iframe.contentWindow.document; var newScript = doc.createElement('script'); newScript.setAttribute("src", "http://translate.google.com/translate_a/element.js? cb=googleTranslateElementInit"); var bodyClass = doc.getElementsByTagName('head')[0]; bodyClass.insertBefore(newScript, bodyClass.childNodes[1]); } function googleJS3() { var iframe = document.getElementsByTagName('iframe')[0]; var doc = iframe.contentWindow.document; var newScript = doc.createElement('script'); newScript.setAttribute("src", "http://localhost:8888/mysite/google.js"); var bodyClass = doc.getElementsByTagName('head')[0]; bodyClass.insertBefore(newScript, bodyClass.childNodes[2]); } } &lt;/script&gt; </code></pre> <p>This works as long as the iframe target URL is on the same server. I read to bypass the same origin constraint I should set up a proxy server and pass all URL requests via the proxy. So I read up on cURL and tried this as a test: </p> <pre><code>&lt;?php function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_USERAGENT, $userAgent); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); $data = curl_exec($ch); curl_close($ch); return $data; } $test = get_data("http://www.selfridges.com"); echo $test; ?&gt; </code></pre> <p>The basic HTML elements are loaded yet no CSS and images are loaded. Also the links still point to the original URL. I need some suggestions on how I can also pull the CSS, images and js off the target URL into a proxy and load the pages from there, making it look like it came from the same domain and ports and by passing the same origin policy. I also need the links to work in this fashion. </p> <p>e.g: </p> <pre><code>main page - http://localhost:8888/proxy.php links - http://localhost:8888/proxy.php/products/2012/shoes </code></pre> <p>Any other methods or alternatives are also welcome.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload