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 8798711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:09:19+00:00 2026-06-14T00:09:19+00:00

I’m developing a chrome extension and bumped into a big problem. I’m using content

  • 0

I’m developing a chrome extension and bumped into a big problem.

I’m using content scripts to inject my javascript code on a web site. The web site has an iframe.
I can change the source code of the iframe but don’t seem to get any access to the iframe’s contentWindow property. I need it to insert text at the current carret position.

So basically this code works perfectly in the context of the page:

$("#iframe1").contentWindow.document.execCommand("InsertHTML", false, 'test text');

But when I try it to run in the context of my chrome extension I get this error:

TypeError: Cannot read property 'document' of undefined

What’s strange is that I can access the html of the iframe. So this code works perfectly from the chrome extension:

$("#iframe1").contents().find('div').html('test')

I tried putting “all_frames”: true in the manifest file but no luck 🙁

  • 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-14T00:09:20+00:00Added an answer on June 14, 2026 at 12:09 am

    To understand why your code does not work, I include a fragment of my previous answer:

    Content scripts do not have any access to a page’s global window object. For content scripts, the following applies:

    • The window variable does not refer to the page’s global object. Instead, it refers to a new context, a “layer” over the page. The page’s DOM is fully accessible. #execution-environment

    Given a document consisting of   <iframe id="frameName" src="http://domain/"></iframe>:

    • Access to the contents of a frame is restricted by the Same origin policy of the page; the permissions of your extension does not relax the policy.
    • frames[0] and frames['frameName'], (normally referring to the the frame’s containing global window object) is undefined.
    • var iframe = document.getElementById('frameName');
      • iframe.contentDocument returns a document object of the containing frame, because content scripts have access to the DOM of a page. This property is null when the Same origin policy applies.
      • iframe.contentDocument.defaultView (refers to the window object associated with the document) is undefined.
      • iframe.contentWindow is undefined.

    Solution for same-origin frames

    In your case, either of the following will work:

    // jQuery:
    $("#iframe1").contents()[0].execCommand( ... );
    
    // VanillaJS
    document.getElementById("iframe1").contentDocument.execCommand( ... );
    
    // "Unlock" contentWindow property by injecting code in context of page
    var s = document.createElement('script');
    s.textContent = 'document.getElementById("iframe1").contentWindow.document.execCommand( ... );';
    document.head.appendChild(s);
    

    Generic solution

    The generic solution is using "all_frames": true in the manifest file, and use something like this:

    if (window != top) {
        parent.postMessage({fromExtension:true}, '*');
        addEventListener('message', function(event) {
            if (event.data && event.data.inserHTML) {
                document.execCommand('insertHTML', false, event.data.insertHTML);
            }
        });
    } else {
        var test_html = 'test string';
        // Explanation of injection at https://stackoverflow.com/a/9517879/938089 :
        // Run code in the context of the page, so that the `contentWindow`
        //  property becomes accessible
        var script = document.createElement('script');
        script.textContent = '(' + function(s_html) {
            addEventListener('message', function(event) {
                if (event.data.fromExtension === true) {
                    var iframe = document.getElementById('iframe1');
                    if (iframe && (iframe.contentWindow === event.source)) {
                        // Window recognised, post message back
                        iframe.contentWindow.postMessage({insertHTML: s_html}, '*');
                    }
                }
            });
        } + ')(' + JSON.stringify(test_html) + ');';
        (document.head||document.documentElement).appendChild(script);
        script.parentNode.removeChild(script);
    }
    

    This demo is for educational purposes only, do not use this demo in a real extension. Why? Because it uses postMessage to pass messages around. These events can also be generated by the client, which causes a security leak (XSS: arbitrary HTML injection).

    The alternative to postMessage is Chrome’s message API. For a demo, see this answer. You won’t be able to compare the window objects though. What you can do is to rely the window.name property. The window.name property is automatically set to the value of the iframe’s name attribute (just once, when the iframe is loaded).

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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
I am reading a book about Javascript and jQuery and using one of the
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.