Chrome iframe parent is undefined
I have this script for Gmail. It runs inside the canvas_frame
iframe.
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.
undefined
followed by cQ
endlessly.
// ==UserScript==
// @name TEST SCRIPT FOR CHROME
// @version 1.0
// @namespace 开发者_运维问答 1nfected
// @description TEST
// @include http://mail.google.com/*
// @include https://mail.google.com/*
// ==/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();
})();
UserScripts in Chrome are limited, especially when it comes 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.
精彩评论