无法访问iframe内容(同源政策) [英] Unable to access iframe content (same-origin policy)

查看:199
本文介绍了无法访问iframe内容(同源政策)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下页面

<!DOCTYPE html> 
<html>
<script type="text/javascript">
    function loopLink(i) 
    {
        window.open($('#iframe_a').contents().find('.image-navigator-mid a').attr('href'),'iframe_a');
        setTimeout(function()
        {
            if (i < 3) loopLink(i+1);
        }, 5000);
    }
    // Wait for the page to load first
    window.onload = function() {

      var a = document.getElementById("mylink");

      a.onclick = function() {
        loopLink(0);
        return false;
      }
    }
</script>
<iframe src="http://nanofate.us/content/fate-new-hair-style#node-inner" width="500" height="500" name="iframe_a" id="iframe_a"></iframe>
<br />
<a id="mylink" href="">Execute</a>

想法是,在当前形式下,单击执行"时,javascript将使iframe使用"previous"链接4次,每次等待5秒,但是,当我单击该链接时,它将重新加载页面甚至在等待10秒钟后,iframe仍无反应

the idea is that in it's current form, when you click Execute, the javascript will cause the iframe to use the "previous" link 4 times, waiting 5 second each time, however when i click the link it just reloads the page and even after waiting 10 seconds the iframe is doing nothing

我想知道我做错了什么

推荐答案

由于相同原始政策限制,如果iframe正在运行来自另一个域的页面,则无法访问其内容.有一些针对相同域策略的解决方案,例如

Due to Same Origin Policy restrictions, you can not access contents of iframe if it is running a page from another domain. There are solutions to same domain policy like

  • 通过代理打开页面

查看 Tomodo .这只是给您一个提示,他们是如何使用代理绕过相同的原始策略约束并访问iframe内容的.所以实现的想法是这样的

Check out Tomodo. It is just to give you a hint how they used proxy to bypass same origin policy constraint and access iframe content. So the implementation idea goes like this

  • 创建代理并将其托管在a.com/proxy
  • a.com/index.html
  • 托管您的主页
  • 现在,请求您的代理为您提供iframe_url的内容,例如a.com/proxy?url=iframe_url.com
  • Create a proxy and host it at a.com/proxy
  • Host your main page at a.com/index.html
  • Now, request your proxy to give you content of iframe_url something like this a.com/proxy?url=iframe_url.com

请注意,这不是一件容易的事,您可能需要在代理处处理很多情况,例如处理相对网址,通过iframe_url读取 Cookie 等 因此,只有在您迫切需要它时,才去购买它.

Please note this is not a trivial task and you may have to handle a lot of cases at your proxy like handling relative URLs, cookie reading by iframe_url etc etc. So go for it only if you need it desperately.

如果要下载特定域的某些图像,只需向您询问服务器端代码即可.您的后端代码将获取页面的html并使用

If you want to download some images for a particular domain, just ask your server side code to it for you. Your backend code will fetch the html of page and use some HTML parser like

  • BeautifulSoup for python (Documentation Link)
  • Jsoup for Java (Documentation Link)

解析img标签并提取源代码并获取图像并下载.

to parse img tags and extract the source and fetch the images and download them.

PS :仅提供一些有用的信息,请阅读如何规避相同的原产地政策

PS: Just for some good information, please read Ways to circumvent same origin policy

这篇关于无法访问iframe内容(同源政策)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆