简体   繁体   English

防止Greasemonkey脚本将div插入iframe

[英]Prevent Greasemonkey script from inserting div into iframe

I am creating a userscript that puts a div at the top of websites. 我正在创建一个用户脚本,将div放在网站顶部。 It works for the most part but when a site has an iframe, the div is also inserted at the top of the iframe content. 它在大多数情况下都有效,但是当网站具有iframe时,div也会插入iframe内容的顶部。 I want to remove the div from the iframe. 我想从iframe中删除div。

Here's my code so far: 到目前为止,这是我的代码:

function createDiv(){
    var userBar=document.createElement('DIV');
    userBar.style.height='10px';
    userBar.style.width='100%';
    userBar.style.border='3px solid black';
    userBar.id='userBar'
    document.body.appendChild(userBar);
    document.body.insertBefore(userBar,document.body.childNodes[0]);

    var iframe=document.getElementsByTagName("iframe")[0];
    var content=frame.contentDocument || frame.contentWindow.document;
    content.removeChild(content.userBar);
}

I feel like I am running into a same-origin problem. 我觉得自己遇到了一个同源问题。 If so is there a way to prevent my content from being written into iframes? 如果是这样,是否有一种方法可以防止我的内容被写入iframe?

if ( window.self === window.top ) { not in a frame } else { in a frame } 

check the above condition first before your code of inserting div 在插入div的代码之前,请先检查以上条件

original link: 原始链接:

How to identify if a webpage is being loaded inside an iframe or directly into the browser window? 如何识别网页是在iframe中加载还是直接加载到浏览器窗口中?

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

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