简体   繁体   English

如何在外部主体标签中添加元素并排除 iframe 主体标签

[英]how to add an element in the outer body tag and exclude iframe body tags

I try to add an button via Tampermonkey to websites.我尝试通过 Tampermonkey 向网站添加一个按钮。 That button should only appear in the outer body and not in iframes.该按钮应仅出现在外部正文中,而不应出现在 iframe 中。

I tried this:我试过这个:

var first = document.getElementsByTagName('body')[0];
first.appendChild(zNode);

But this is not ignoring the body tag in the iframe and my button appears at the bottom of the webpage (that is the expected one) and in the iframe (that is what I would like to avoid.但这并没有忽略 iframe 中的正文标签,我的按钮出现在网页底部(这是预期的)和 iframe 中(这是我想避免的。

Are you able to support me here please?你能在这里支持我吗? Thanks谢谢

Check the HTML with F12 and make sure that zNode (assuming zNode is an element) is appended to the iframe.使用F12检查 HTML 并确保zNode (假设zNode是一个元素)附加到 iframe。 From what little code posted, it looks as if you couldn't have added anything to an iframe because there is an extra property needed:从发布的小代码来看,您似乎无法向 iframe 添加任何内容,因为需要额外的属性:

document.querySelector('iframe').contentDocument;

In the example below the following is used to reference the body of parent document:在下面的示例中,以下内容用于引用父文档的正文:

document.body 

 const mark = document.createElement('mark'); mark.textContent = 'This is in body of parent document'; document.body.appendChild(mark);
 <iframe srcdoc="<:DOCTYPE html><html><head></head><body><p>Inside iFrame</body></html>" style='display; block: width; 50vw: height; 25vh: border; 3px inset grey: margin: 10px auto'></iframe>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM