最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome iframe parent is undefined - Stack Overflow

programmeradmin4浏览0评论

I have this script for Gmail. It runs inside the canvas_frame iframe.
I want to get a handle to the parent document, using parent.document. But in Chrome tells me that it's undefined. Works fine in Firefox, but blows up on Chrome.
So how exactly do I get a handle to the parent document, from within an iframe, in Chrome.
Chrome ver: 11.0.686.3

Here's the code that's failing:

function init() {
    try {
        if(parent == null) {
            console.log(typeof parent);
            window.setTimeout(init, 200);
            return;
        }
        // SOME MORE STUFF
    } catch(e) { console.log(e) }
}

This part just outputs undefined endlessly in the log window.
Here's a test script that produces the same result. It outputs undefined followed by cQ endlessly.

// ==UserScript==
// @name           TEST SCRIPT FOR CHROME
// @version        1.0
// @namespace      1nfected
// @description    TEST
// @include        /*
// @include        /*
// ==/UserScript==

(function() {
if(document.documentElement.className != 'cQ') {
    console.log('not our frame');
    return;
}
function init() {
    if(window.parent == null) {
        console.log(typeof window.parent);
        console.log(document.documentElement.className);
        window.setTimeout(init, 1000);
        return;
    }
    console.log('Found the parent');
}
init();
})();

I have this script for Gmail. It runs inside the canvas_frame iframe.
I want to get a handle to the parent document, using parent.document. But in Chrome tells me that it's undefined. Works fine in Firefox, but blows up on Chrome.
So how exactly do I get a handle to the parent document, from within an iframe, in Chrome.
Chrome ver: 11.0.686.3

Here's the code that's failing:

function init() {
    try {
        if(parent == null) {
            console.log(typeof parent);
            window.setTimeout(init, 200);
            return;
        }
        // SOME MORE STUFF
    } catch(e) { console.log(e) }
}

This part just outputs undefined endlessly in the log window.
Here's a test script that produces the same result. It outputs undefined followed by cQ endlessly.

// ==UserScript==
// @name           TEST SCRIPT FOR CHROME
// @version        1.0
// @namespace      1nfected
// @description    TEST
// @include        http://mail.google./*
// @include        https://mail.google./*
// ==/UserScript==

(function() {
if(document.documentElement.className != 'cQ') {
    console.log('not our frame');
    return;
}
function init() {
    if(window.parent == null) {
        console.log(typeof window.parent);
        console.log(document.documentElement.className);
        window.setTimeout(init, 1000);
        return;
    }
    console.log('Found the parent');
}
init();
})();
Share edited Nov 27, 2016 at 21:39 Alexis Wilke 20.9k11 gold badges106 silver badges179 bronze badges asked Mar 6, 2011 at 14:38 Vishal ShahVishal Shah 4,1544 gold badges24 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

UserScripts in Chrome are limited, especially when it es to iframes.

Any reason why you can't do it the other way? For instance:

var frame = document.getElementById('canvas_frame');
if (frame) {
  var dom = frame.contentDocument;
}

The better answer here would be Chrome Extensions, you would have more control with a Content Script instead of a User Script.

I finally realised that in Google Chrome, userscripts are denied access to window.parent .
It would only work if I injected the script into the webpage.

发布评论

评论列表(0)

  1. 暂无评论