【发布时间】:2018-03-08 22:51:59
【问题描述】:
我正在开发一个 Chrome 扩展程序,为了向我的用户显示一些信息,我创建了一个 IFrame 并将其添加到页面的 DOM。
Iframe 包含在我的扩展中定义的模板。
到目前为止,在大多数页面上都没有问题,除非我尝试在特定页面上打开我的扩展程序:
- 像 RSS Feeds 这样的 XML 文档 ==> 我真的需要解决这个问题。
- 特定的 Chrome 页面 ==> 我并不真正关心这个用例。
当用户打开我的扩展程序时,我通过以下方式创建和管理 iframe:
var iframe = document.createElement('iframe');
iframe.id = "my_awesome_iframe";
// Then I specify the source: I ask chrome to look for it.
// I have checked the correct URL is returned. No problem so far here.
iframe.src = chrome.runtime.getURL("templates/page.html');
// Here the problems start:
// with a normal page: No problem
// with an XML document: I have the error: "style is undefined, can't set cssText"
iframe.style.cssText = 'height: 100% !important; width: 100% !important;';
// I can solve this doing the following:
iframe.setAttribute("style", 'height: 100% !important; width: 100% !important;');
// And finally
document.body.appendChild(iframe);
但是现在,一切都已正确设置。每当我遇到上面提到的一种情况时,src url 只是未加载。我在 DOM 中有 iframe 元素,但它只是空的。
只是为了检查,我尝试了以下方法:
document.getElementById("my_awesome_iframe").contentWindow
// As expected, I obtain the following only when the iframe refuses to load.
>> undefined
知道如何解决这个问题吗?
当用户使用 XML 文件(如 RSS Feed)时,我真的需要能够加载我的扩展程序。这是我扩展的重点之一。
附加信息:
我的扩展与 Firefox 兼容,实现完全相同。行为有点不同。当 XML 文档是 RSS 提要时,我可以打开 mmy 扩展,当它是别的东西时:这是不可能的。
示例链接:
- RSS 源:https://www.feedcrunch.io/@dataradar/rss/(仅适用于 FF)
- XML 文档:http://www.httrack.com/httrack.xml(在 FF 和 Chrome 上失败)
一定有一些 XMLViewer 特有的东西,但我不知道在哪里寻找......
【问题讨论】:
-
试试
iframe = document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')和iframe.setAttributeNS(null, 'style', '/* your CSS here */') -
@wOxxOm:你摇滚!那行得通。我不能将您的 awswer 标记为好。您能否将其复制/粘贴为正常答案;)非常感谢。
标签: javascript html google-chrome iframe google-chrome-extension