admin管理员组文章数量:1313800
I'm trying to add some html elements inside a iframe, to personalize a site. I'm building a browser extension, so there is no cross-domain problem. I have the iframe:
<iframe id="container" src="">
<p>Your browser does not support iframes.</p>
</iframe>
and then, when the document is already loaded, I create the element and insert it... But it is never shown in the iframe. But I am allowed to get an iframe-existent element and to change it's properties. So, I have access to the elements but not for create new ones? Why?
var ifr = document.querySelector("#container");
var ifrDoc = ifr.contentWindow || ifr.contentDocument;
if (ifrDoc.document) ifrDoc = ifrDoc.document;
var elem = ifrDoc.createElement("div");
elem.innerHTML = "Demo Box";
elem.style.width = "50px";
elem.style.height = "50px";
elem.style.position = "absolute";
elem.style.background = "red";
ifrDoc.body.appendChild(elem);
Here a JSFiddle demo to understand the problem (remember extensions have another privileges): /
Any idea?
I'm trying to add some html elements inside a iframe, to personalize a site. I'm building a browser extension, so there is no cross-domain problem. I have the iframe:
<iframe id="container" src="http://www.google.">
<p>Your browser does not support iframes.</p>
</iframe>
and then, when the document is already loaded, I create the element and insert it... But it is never shown in the iframe. But I am allowed to get an iframe-existent element and to change it's properties. So, I have access to the elements but not for create new ones? Why?
var ifr = document.querySelector("#container");
var ifrDoc = ifr.contentWindow || ifr.contentDocument;
if (ifrDoc.document) ifrDoc = ifrDoc.document;
var elem = ifrDoc.createElement("div");
elem.innerHTML = "Demo Box";
elem.style.width = "50px";
elem.style.height = "50px";
elem.style.position = "absolute";
elem.style.background = "red";
ifrDoc.body.appendChild(elem);
Here a JSFiddle demo to understand the problem (remember extensions have another privileges): http://jsfiddle/TH48e/40/
Any idea?
Share Improve this question edited Nov 26, 2014 at 16:50 erikvold 16.6k11 gold badges58 silver badges101 bronze badges asked Nov 26, 2014 at 1:59 gal007gal007 7,2129 gold badges50 silver badges73 bronze badges 5- Why do you think that HTML content can be inserted in Google.? Can you link somewhere where it says that? I don't see why the Same Origin Policy would be any different when creating Browser extensions. – istos Commented Nov 26, 2014 at 2:46
- Extensions can do that. E.g. Greasemonkey – gal007 Commented Nov 26, 2014 at 2:52
- Are you trying to create a Browser extension or User Script? – istos Commented Nov 26, 2014 at 2:55
- What scope are you running this code from? – Noitidart Commented Nov 26, 2014 at 3:09
- The chrome window (Firefox) – gal007 Commented Nov 26, 2014 at 3:12
1 Answer
Reset to default 6After your clarification in ments:
var ifr = gBrowser.selectedBrowser.contentDocument.getElementById('container')
Dont do that .contentWindow
then get the document
from that, its useless redundant etc.
Also dont do querySelector
if you want to just get something by id
anyways, huge speed difference, getElementById
is much faster.
Also don't do .innerHTML
the addon approvers won't accept it because it leads to security issues. Do .textContent
.
Your fiddle will not work because:
- You can't do cross frame munication
- I'm not sure but I think:
createElement
is a "native" function in javascript so reserved in away although it isdocument.createElement
but ya
This fiddle works, the rawr
function is undefined in body, I'm not sure why so i moved the script to the document:
http://jsfiddle/TH48e/43/
<script>
function createElementMY(){
var doc = document.getElementById('container');
var ifrDoc = doc.contentDocument;
var elem = ifrDoc.createElement("div");
elem.textContent = 'Test';
ifrDoc.body.appendChild(elem);
}
</script>
<iframe id="container" src="data:text/html,rawr"></iframe>
<button onclick="createElementMY()">Click me!</button>
本文标签: javascriptAdding new elements to existing Iframe by JSStack Overflow
版权声明:本文标题:javascript - Adding new elements to existing Iframe by JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741940691a2406124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论