Frankie Dee
Frankie Dee

Reputation: 61

Prevent Greasemonkey script from inserting div into iframe

I am creating a userscript that puts a div at the top of websites. 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. I want to remove the div from the iframe.

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?

Upvotes: 0

Views: 351

Answers (1)

ManMohan Vyas
ManMohan Vyas

Reputation: 4062

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

check the above condition first before your code of inserting div

original link:

How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

Upvotes: 1

Related Questions