如何知道通过iframe加载的页面是否在沙盒中? [英] How to know if a page loaded via iframe is within sandbox?

查看:625
本文介绍了如何知道通过iframe加载的页面是否在沙盒中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测是否通过沙盒iframe加载了一个页面。这可能吗?



例如,我们提供自定义可嵌入小部件,有些人认为通过在iframe中对它们进行沙盒化是聪明的,但这会打破某些事情......比如as window.top.location



显然,他们可以启用我们需要的功能,但理想情况下,我应该是能够像这样做:

  window.top中的沙盒

我也尝试过这样做

 试试{
//做一些在沙盒中不起作用的东西
} catch(e){

}

但这不起作用,因为它是浏览器安全性错误,与javascript无关。



JSFiddle实际上沙箱他们的iframe以防止window.top.location导航,所以这将是一个很好的例子向您展示。
如果你看这个例子:



解决方案

sandblaster 项目可以帮助您检测是否正在运行沙盒。



在iframe中测试它是否为沙盒,打开脚本标记并粘贴 https://raw.githubusercontent.com/JamesMGreene/sandblaster的内容/master/dist/sandblaster.js 。这是由于安全问题。



在此之后,它就像以下一样简单。

  var result = sandblaster.detect(); 
if(result.sandboxed === true){
// sandboxed
}

这是我为另一个答案制作的演示,但表明该解决方案有效。


I'm trying to detect if a page is loaded via a sandboxed iframe. Is this possible?

For example,we provide custom embeddable widgets and some people think they are being smart by sandboxing them in their iframe, but this breaks certain things.. such as window.top.location

Obviously, they could enable the features we need, but ideally, I should be able to just do something like:

"sandbox" in window.top

I have also tried doing

try {
    // do something that would not work if within sandbox
} catch(e) {

}

But this doesn't work because it's a browser security error, and not related to javascript.

JSFiddle actually sandbox their iframes to prevent window.top.location navigation, so this would be a good example to show you. If you look at this example here:

http://jsfiddle.net/mwsb8geL/show/

You can see the error when you press the Instant Book Online button.

解决方案

A project sandblaster can help you detect if you running being sandboxed.

Inside the iframe where you are testing if it is sandbox, open up your script tag and paste the contents of https://raw.githubusercontent.com/JamesMGreene/sandblaster/master/dist/sandblaster.js. This is due to the security issue.

After this, its as simple as the following.

var result = sandblaster.detect();
if(result.sandboxed === true) {
    //sandboxed
}

Here is a demo I made for another answer but shows that the solution works.

这篇关于如何知道通过iframe加载的页面是否在沙盒中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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