我有这个脚本的Gmail。它运行在iframe的canvas_frame
内。
我想使用parent.document
来获取父文档的句柄。但在Chrome中告诉我,它是未定义的。在Firefox中可以正常工作,但在Chrome上会出现问题。
那么,我如何在Chrome中从iframe中获取父文档的句柄。
Chrome的版本:11.0.686.3铬iframe父母未定义
这里是一个的失败代码:
function init() {
try {
if(parent == null) {
console.log(typeof parent);
window.setTimeout(init, 200);
return;
}
// SOME MORE STUFF
} catch(e) { console.log(e) }
}
这部分只是在日志窗口输出undefined
不休。
这是一个测试脚本,可以产生相同的结果。它不断地输出undefined
和cQ
。
// ==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();
})();
那,使用内容脚本会更好,因为你得到了完全控制(有点)。 – 2011-03-16 00:07:55
我做了一点阅读最终也发现,在code.google.com对此提起了问题。我认为内容脚本也不会有任何好处。 – 1nfected 2011-03-21 14:55:01