Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:49:45+00:00 2026-06-08T16:49:45+00:00

I’m trying to use a Chrome userscript or a Tampermonkey script to modify a

  • 0

I’m trying to use a Chrome userscript or a Tampermonkey script to modify a page with this structure:

<body>
content up here

<iframe id="main" src="foo.dat"></iframe>
</body>

The iframe is same-origin.

I need to access a function that’s in iframe#main. I thought I could use unsafeWindow to get it but I keep getting nothing or undefined returned.

I’ve tried a slew of things:

  • Tried creating a new script element in the iframe, but it attaches to the parent even with $('frame#main').contents().append(script) or $('frame#main').contents()[0].createElement('script')

  • window.frames["#main"].contentWindow returns undefined.

I have tried many other things I can’t recall at the moment, but I have exhausted all my ideas and feel that I’m typing rubbish more than anything that counts.
I can’t figure out how to play with the unsafeWindow of the iFrame.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:49:47+00:00Added an answer on June 8, 2026 at 4:49 pm
    1. unsafeWindow doesn’t play nice with frames/iframes on Chrome, Tampermonkey, or Firefox.
    2. Trying to access global (to the frame) JS with jQuery, like that, will not work.
    3. userscripts will run on iframes that meet the @include, @exclude, and/or @match requirements.

    So, you need to account for the multiple script runs and then you have two basic approaches, depending on what you are trying to accomplish. You can:

    (A) Tailor the script to specific frame(s), as in this answer.

    or (B) inject your JS and use the special frames object to grab the specific function you want.

    The following script demonstrates both. Install it in Tampermonkey1 (or Firefox Greasemonkey), and then visit this test page at jsBin.

    // ==UserScript==
    // @name        _Calling iframe functions
    // @namespace   _pc
    // @include     http://jsbin.com/ugoruz/*
    // @include     http://jsbin.com/okequw/*
    // ==/UserScript==
    
    console.log ("Script start...");
    
    /*--- This next function call will work in Firefox or Tampermonkey ONLY,
        not pure Chrome userscript.
    */
    console.log ("calling functionOfInterest ()...");
    unsafeWindow.functionOfInterest ();
    
    
    if (window.top === window.self) {
        //--- Code to run when page is the main site...
        console.log ("Userscript is in the MAIN page.");
    
        //--- The frames object does not play nice with unsafeWindow.
        /*--- These next three work in Firefox, but not Tampermonkey, nor pure Chrome.
        console.log ("1", frames[1].variableOfInterest);                // undefined
        console.log ("2", unsafeWindow.frames[1].variableOfInterest);   // undefined
        console.log ("3", frames[1].unsafeWindow);                      // undefined
        */
        /*--- This next would cause a silent crash, all browsers...
        console.log ("4", unsafeWindow.frames[1].unsafeWindow.variableOfInterest);
        */
    
        //--- To get at iFramed JS, we must inject our JS.
        withPages_jQuery (demoAccessToFramedJS);
    }
    else {
        //--- Code to run when page is in an iframe...
        console.log ("Userscript is in the FRAMED page.");
        console.log ("The frame's ID is:", window.self.frameElement.id);
    }
    
    
    function demoAccessToFramedJS ($) {
        $("body").prepend (
              '<button id="gmMain">Run JS on main window</button>'
            + '<button id="gmFrame">Run JS on iframe</button>'
        );
    
        $("#gmMain, #gmFrame").click ( function () {
            if (this.id === "gmMain") {
                functionOfInterest ();
            }
            else {
                frames[1].functionOfInterest ();
            }
            console.log (this.id + "was clicked.");
        } );
    }
    
    function withPages_jQuery (NAMED_FunctionToRun) {
        //--- Use named functions for clarity and debugging...
        var funcText        = NAMED_FunctionToRun.toString ();
        var funcName        = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
        var script          = document.createElement ("script");
        script.textContent  = funcText + "\n\n";
        script.textContent += 'jQuery(document).ready(function() {'+funcName+'(jQuery);});';
        document.body.appendChild (script);
    };
    
    console.log ("Script end");
    


    You will see that the script runs a function from both the main page and from the iframe. The console output (Tampermonkey) will be:

    Tampermonkey started
    Script start...
    calling functionOfInterest ()...
    Userscript is in the MAIN page.
    Script end
    Tampermonkey started
    Script start...
    calling functionOfInterest ()...
    Userscript is in the FRAMED page.
    The frame's ID is: iframe2
    Script end
    

    1 It will also work as a straight-up Chrome userscript if you remove the unsafeWindow line(s).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.