簡體   English   中英

防止Greasemonkey腳本將div插入iframe

[英]Prevent Greasemonkey script from inserting div into iframe

我正在創建一個用戶腳本,將div放在網站頂部。 它在大多數情況下都有效,但是當網站具有iframe時,div也會插入iframe內容的頂部。 我想從iframe中刪除div。

到目前為止,這是我的代碼:

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);
}

我覺得自己遇到了一個同源問題。 如果是這樣,是否有一種方法可以防止我的內容被寫入iframe?

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

在插入div的代碼之前,請先檢查以上條件

原始鏈接:

如何識別網頁是在iframe中加載還是直接加載到瀏覽器窗口中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM