我有這個腳本的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