0

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?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Occasionally, iframes will load after `$(document).ready()` so a `setTimeout` may be a good option. Can you give an example of the issue? Or maybe console errors? – David Starkey Jul 02 '13 at 22:23
  • Oh after a bit of searching i found the answer! – Frankie Dee Jul 02 '13 at 22:26
  • That's good to hear. Feel free to post is as an answer and accept it. You may help someone else with this issue. – David Starkey Jul 02 '13 at 22:27
  • possible duplicate of [How to exclude iframe in Greasemonkey](http://stackoverflow.com/questions/1535404/how-to-exclude-iframe-in-greasemonkey) – Brock Adams Jul 02 '13 at 23:12

1 Answers1

1
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?

Community
  • 1
  • 1
ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40