【问题标题】:Detecting if iframe src is displayable检测 iframe src 是否可显示
【发布时间】:2017-05-23 03:54:10
【问题描述】:

我想在 iframe 中加载外部网站,如果这些网站中的任何一个使用了框架拦截器,那么我想将用户重定向到错误页面。有一些建议的方法可以做到这一点:

  • 等待加载超时
  • 查看 iframe src html 内容在加载后是否为“空”
  • 尝试捕捉错误
  • 维护一个包含“黑名单”网址的数据库

到目前为止,令人沮丧的是,我在最后一项上运气最好。其他方法不起作用,原因如下:

  • 等待加载超时:
    • 即使网站使用框架杀手,也会触发 onload 事件。例如,如果我尝试访问 www.google.com,它只会加载空的 html 结构。
  • 在加载后查看 iframe src html 内容是否为“空”
    • 由于同源策略,您无法访问 iframe 的外部 src 内容。
  • 试图捕捉错误:
    • 据我了解,我只能找到与源自本地 JS 代码的错误相关的错误处理函数,而与 "Refused to display <URL> in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'/'DENY'" 之类的错误无​​关。
  • 维护“黑名单”网址的数据库:
    • 这显然是一个糟糕的解决方案,它不全面,而且清单很大哈哈。

也许我误解了其中一种方法。我在这里缺少解决方案吗?对于上下文,我主要在 JS + jQuery 中执行此操作。

【问题讨论】:

  • 我自己曾经在同一问题上花了很多时间,但从未找到解决方案。好奇是否有人出现
  • 虽然我从未尝试过的一件事是使用服务器端脚本发出头部请求..这可能是值得一试的。
  • 嗯,值得一试
  • 虽然不会捕获的一件事是任何脚本将框架页面强制为top,但可能会帮助您缩小在 iframe 中肯定不起作用的标题

标签: javascript jquery html iframe


【解决方案1】:

我有一个临时修复程序,它使用@charlietfl 建议的标题信息,虽然它并不完美,正如您在测试部分下看到的那样,并非所有网站都在其标题中列出 x-frame 选项。

<?php 

// checkXFO
// checks x-frame options
// $headers: an array of headers
// returns: nothing
function checkXFO($headers){
    if($headers['X-Frame-Options']==""){
        echo "good to embed! <p>";
    }
    else{
        echo "Denied! <p>";
    }
}

//-----------------------
// tests
//-----------------------

// x-frame option: SAMEORIGIN
// should deny
// > passes
$headerArray = get_headers('http://www.google.com',1); 
checkXFO($headerArray);

// x-frame option: DENY
// should deny
// > passes
$headerArray = get_headers('http://www.facebook.com',1); 
checkXFO($headerArray);

//x-frame option: none
// should accept
// > passes
$headerArray = get_headers('http://wikipedia.org',1); 
checkXFO($headerArray);

//x-frame option: none
// should accept
// > passes
$headerArray = get_headers('http://neopets.com',1); 
checkXFO($headerArray);

//x-frame options: DENY
// should deny
// > fails
$headerArray = get_headers('http://www.yahoo.com',1); 
checkXFO($headerArray);

//x-frame option:none. Redirected x-frame options: DENY
// should deny
// > fails
$headerArray = get_headers('http://www.yahoo.ca',1); 
checkXFO($headerArray);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2011-01-26
    • 2010-10-02
    • 2011-06-26
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多