【发布时间】:2017-03-16 05:59:55
【问题描述】:
我想在后台页面中动态注入和加载 iframe。但是每次请求都会被取消。
http://i.imgur.com/Puto33c.png
这曾经在一周前起作用。我不知道我错在哪里。
为了重现这个问题,我创建了一个小扩展:
manifest.js:
{
"name": "iframe background",
"version": "1.0.0",
"manifest_version": 2,
"browser_action": {
"default_title": "iframe"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
}
}
background.js:
chrome.browserAction.onClicked.addListener(function() {
var iframe = document.createElement('iframe');
iframe.src = 'http://localhost:3000/';
iframe.onload = function() {
console.log(iframe.contentDocument); // return null
};
document.body.appendChild(iframe);
});
要加载的页面未被 X-Frame-Options SAMEORIGIN 阻止。
我尝试将 iframe 直接放在 HTML 背景页面中,但没有成功。
我还尝试添加 content_security_policy :
"content_security_policy": "script-src 'self'; object-src 'self'; frame-src 'self' http://localhost:3000/"
但 iframe 仍然无法加载。
有人对此问题有解决方法或解决方案吗?
谢谢!
【问题讨论】:
标签: iframe google-chrome-extension