检查网站是否不允许 iframe 嵌入
我正在为我的应用程序编写一个简单的类似灯箱的插件,并且我需要嵌入一个链接到任意页面的 iframe。问题是,许多网站(例如,facebook、nytimes 甚至 stackoverflow)将检查是否嵌入到框架中,如果是,则将刷新页面并将其自身作为父页面。这是一个已知问题,我认为对此无能为力。但是,我希望能够事先知道网站是否支持嵌入。如果没有,我想在新选项卡/窗口中打开页面,而不是使用 iframe。
有什么技巧可以让我在 javascript 中检查这个吗?
也许有一个服务器端脚本可以检查链接以查看它们是否允许 iframe 嵌入?
我正在开发一个浏览器扩展,因此有机会做一些非常有创意的事情。我的扩展程序会加载到每个页面上,因此我认为有一种方法可以在 iframe url 中传递一个参数,如果扩展程序破坏了 iframe,则扩展程序可以获取该参数。然后我可以将该域添加到不支持 iframe 嵌入的网站列表中。这可能会起作用,因为扩展不会加载到 iframe 中。我会努力解决这个问题,但与此同时......
澄清:
我愿意接受没有办法“破坏”“框架破坏者”,即我知道我不能在 iframe 中显示不想出现在 iframe 中的页面。但我希望我的应用程序能够正常失败,这意味着如果不支持 iframe 嵌入,则在新窗口中打开链接。理想情况下,我想在运行时检查 iframe 嵌入支持(javascript),但我可以看到使用上面评论中建议的代理的潜在服务器端解决方案。希望我可以建立一个不允许嵌入 iframe 的网站数据库。
I am writing a simple lightbox-like plugin for my app, and I need to embed an iframe that is linked to an arbitrary page. The problem is, many web sites (for example, facebook, nytimes, and even stackoverflow) will check to see if is being embedded within a frame and if so, will refresh the page with itself as the parent page. This is a known issue, and I don't think there's anything that can be done about this. However, I would like the ability to know before hand if a site supports embed or not. If it doesn't, I'd like to open the page in a new tab/window instead of using an iframe.
Is there a trick that allows me to check this in javascript?
Maybe there is a server-side script that can check links to see if they permit an iframe embed?
I am developing a browser extension, so there is an opportunity to do something very creative. My extension is loaded on every page, so I'm thinking there's a way to pass a parameter in the iframe url that can be picked up by the extension if it destroys the iframe. Then I can add the domain to a list of sites that don't support iframe embed. This may work since extensions aren't loaded within iframes. I will work on this, but in the meantime....
Clarification:
I am willing to accept that there's no way to "bust" the "frame buster," i.e. I know that I can't display a page in an iframe that doesn't want to be in one. But I'd like for my app to fail gracefully, which means opening the link in a new window if iframe embed is not supported. Ideally, I'd like to check iframe embed support at runtime (javascript), but I can see a potential server-side solution using a proxy like suggested in the comments above. Hopefully, I can build a database of sites that don't allow iframe embed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下代码检查 x-frame-options 标头
如果返回值 DENY、SAMEORIGIN 或 ALLOW-FROM 那么您不能将 iframe 与该 url 一起使用。
Check x-frame-options header by using following code
If return value DENY, SAMEORIGIN or ALLOW-FROM then you can't use iframe with that url.
可能已经很晚了,但您需要做的是发出请求(可能来自您的服务器)并查找 x-frame-options 标头。如果它存在,您只需打开一个新选项卡,因为如果它存在,则存在以下选项之一:DENY、SAMEORIGIN、ALLOW-FROM。在任何一种情况下,您都可能无权在 iframe 中打开它。
Probably pretty late but what you need to do is make a request, likely from your server and look for the x-frame-options header. If it's there at all you can just open a new tab because if it is there is is one of the following: DENY, SAMEORIGIN, ALLOW-FROM. In any of these cases it's likely that you don't have access to open it in an iframe.
这个主题已经在网络上永远讨论过,这里有一个特别有趣(失败)的尝试:
Frame Buster Buster ... 需要 buster 代码
最重要的是,即使您能够构建一个代理来解析您想要在 iframe 中出现的页面内容,并在将其提供给 iframe 之前删除有问题的代码。内嵌框架如果他们听说您这样做,您可能仍会受到该网站的“停止和终止”的处罚。
如果您不希望您的开发被广泛使用,您可能会侥幸逃脱。如果你希望你的开发变得流行,那就忘掉它,并建立一种不那么卑鄙的方式来处理它。
或者仅为移动设备开发它...;)
更新: 好的,根据您的评论,这里有一些品味:
链接服务器端的点击
在 javascript 中捕获PHP 中
This subject has been discussed forever on the web with a particularly interesting (failed) attempt here:
Frame Buster Buster ... buster code needed
The bottom line is that even if you are able to construct a proxy that parses the contents of the page that you want in your iframe and removes the offending code before it is served to the iframe you may still come under "cease and desist" from the site if they get to hear about you doing it.
If you don't want your development to be widely available, you could probably get away with it. If you want your development to become popular, forget about it, and build a less underhand way of dealing with it.
Or develop it for mobile only... ;)
UPDATE: OK following on from your comment here's a bit of taster:
in javascript capture the click on the link
server side in PHP