Chrome应用:从父窗口访问沙盒iframe [英] Chrome app: accessing sandboxed iframe from parent window

查看:143
本文介绍了Chrome应用:从父窗口访问沙盒iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Google chrome应用程序中使用了ockoutoutjs.为了能够使用剔除功能,我必须将real.application.html定义为sandox页面,并将其作为iframe包含在虚拟容器中.应用程序结构如下:

I'm using knockoutjs in my google chrome app. To be able to use knockout, I have to define the real application.html as sandox page and include it as an iframe in a dummy container. Application structure is as follows:

- container.html
|
+-- application.html as iframe 
   |
   +-knockout and application.js

iframe的定义如下:

Iframe is defined as follows:

  <iframe src="application.html" frameborder="0"  
            sandbox="allow-same-origin allow-scripts" ></iframe>

运行

document.getElementsByTagName("iframe")[0]

container.html上检查工具中的

引发以下错误.

in inspect tool on container.html throws following error.

Sandbox access violation: Blocked a frame at "chrome-extension://hllbklabnppjkmnngfanldbllljfeaia" 
from accessing a frame at "chrome-extension://hllbklabnppjkmnngfanldbllljfeaia".  
The frame being accessed is sandboxed and lacks the "allow-same-origin" flag.

如何从其父级访问iframed文档?

How can i access the iframed document from it's parent?

推荐答案

找出了罪魁祸首.这是我的proxy.js,包含在container.html中,用作在应用程序iframe和background.js之间传递消息的桥梁.接下来的部分是侦听来自iframe的消息的部分.

Found out the culprit. This is my proxy.js, which is included by the container.html, used as a bridge to transfer the messages between the application iframe and the background.js. Following part is the one that listens for the messages originated from the iframe.

window.addEventListener("message",
    function(evt){
        console.log(evt); <= this is the problem
        var iframe = document.getElementById("application").contentWindow; <= not this one
        if (evt.source == iframe) {
            return chrome.runtime.sendMessage(null, evt.data);
        }
    }
);

我不认为console.log是导致问题的原因.相反,我从document.getElem ..行怀疑.因为尝试在应用程序的检查窗口中运行该代码会引发相同的错误.

I didn't think that console.log would be the causing the problem. Instead i was suspecting from the document.getElem.. line. Because trying to run that code in inspect window of the application was throwing the same error.

但是,似乎console.log(控制台似乎属于container.html的范围)访问了一些事件对象的内部内容,这些内容不应该从iframe的范围之外访问(这解释了为什么我在检阅中遇到相同的错误)安慰).删除console.log行为我解决了这个问题.

But it seems that console.log (console seems to belong to container.html's scope) accesses some internals of event object that are not meant to be accessible out of the iframe's scope(which explains why i get the same error in inspect console). Removing the console.log line solved this problem for me.

这篇关于Chrome应用:从父窗口访问沙盒iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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