从Chrome扩展中Iframe访问不安全Javascript访问

从Chrome扩展中Iframe访问不安全Javascript访问

问题描述:

我正在构建一个Chrome扩展,它将使用加载Flash内容的Iframe。在Iframe中使用Mootools,我创建一个.swf对象,然后将它注入到自身中。此iframe,main.html中,被插入Flash对象,像这样:从Chrome扩展中Iframe访问不安全Javascript访问

window.addEvent("domready", function() { 
    var flashContainer = $("flash-container"); 

    new Swiff("http://www.example.com/content.swf", { 
     "width":"100%", 
     "height":"90%", 
     "id":"flash-content" 
    }).inject(flashContainer); 
}); 

这将产生以下错误:

不安全的JavaScript尝试与URL铬扩展访问框架:// ... ...../index.html与网址http://www.example.com/main.html。域,协议和端口必须匹配。

当我删除代码的注入部分,我没有得到错误。另外,当我在其他非Flash元素(例如div)上使用注入时,我不会收到错误。我也尝试使用纯ol'appendChild javascript方法来获得相同的错误。

任何想法,为什么我会只在Flash对象上得到这个错误?有没有办法添加这个Flash元素而没有得到这个错误?

+0

任何跨域调用的资源,是不是JSONP将提高这个安全错误。您需要找到一种方法将SWF内容注入到与swf文件相同的域中的页面上,或者将SWF(如果它是静态的)添加到数据URL(data:application/x-swf ; base64,'或其他)。 – MischaNix

+0

我从example.com创建SWF并将其注入到example.com。那如何不构成跨域? Chrome扩展本身就是加载iframe,将所有这一切都完成了。 –

+0

什么是swf,'chrome-extension://./ index.html'的iframe url? – serg

@MishcaNix和@serg是对的。

Any cross-domain call to a resource that isn't JSONP will raise this security error. You'll need to either find a way to inject the SWF content into a page on the same domain as the swf file, or take the SWF (if it's static) and make it a data URL (data:application/x-swf;base64, or somesuch). – MischaNix

每当Flash对象嵌入到跨域iframe中时,它将引发跨域异常。 Youtube和Vimeo都是如此。

例如,看看这个的jsfiddle嵌入一个iFrame:http://jsfiddle.net/bkCdB/

<object width="420" height="315"><param name="movie" value="http://www.youtube.com/v/6V_DsL1x1uY?version=3&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/6V_DsL1x1uY?version=3&amp;hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>