Firefox Addon SDK:将插件文件加载到iframe中

Pat*_*een 6 javascript iframe firefox firefox-addon firefox-addon-sdk

我想将一个resource://链接,分别是我的Firefox插件中的本地文件加载到iframe网页中.

原因是,出于安全原因,资源应该可视地嵌入到网页中,而不是让网站访问它的DOM.

这个问题在过去的各个地方已经讨论过了,例如这里(没有解决方案):https: //bugzilla.mozilla.org/show_bug.cgi?id = 792479

由于大多数帖子都比较陈旧,我想问一下,如果在此期间有任何新的解决方案或解决方法.

ZER*_*ER0 11

我认为我在错误或在喷气背包的ML中建议了一个糟糕的解决方法,基本上是resource://在你的data:URL中转换(使用data.load加载HTML内容,然后编码并附加为前缀,所以类似的东西应该有效:

/* main.js */
const { data } = require('sdk/self');

// just an example, you can use `tab.attach` too
require('sdk/page-mod').PageMod({
  include: '*',
  contentScriptFile: data.url('content.js'),
  contentScriptOptions: {
    content: encodeURIComponent(data.load('index.html'))
  }
});

/* content.js */
let iframe = document.body.appendChild(document.createElement('iframe'));

iframe.setAttribute('sandbox', 'allow-scripts');
// maybe you want also use the seamless attribute, see:
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe

iframe.contentWindow.location.href = 'data:text/html;charset=utf-8,' + self.options.content;
Run Code Online (Sandbox Code Playgroud)

这当然是一种解决方法,我希望你提到的错误很快就会解决.请注意,通过这种方式,您无法直接从iframe与父文档进行通信,但这也意味着您无法绕过,这就是您要阻止的.

当然,您仍然可以使用附加代码在iframe和父文档之间进行通信(您需要附加内容脚本并使用portpostMessage).

编辑:更改了网址设置的方式,否则仍然可以src从父文档中获取属性,并包含完整的HTML.