gpt4 book ai didi

javascript - Chrome iframe 父级未定义

转载 作者:行者123 更新时间:2023-11-29 10:24:13 24 4
gpt4 key购买 nike

我有这个 Gmail 脚本。它在 canvas_frame iframe 中运行。
我想使用 parent.document 获取父文档的句柄。但是在 Chrome 中告诉我它是未定义的。在 Firefox 中运行良好,但在 Chrome 中会崩溃。
那么在 Chrome 中如何从 iframe 中获取父文档的句柄。
Chrome 版本:11.0.686.3

这是失败的代码:

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();
})();

最佳答案

Chrome 中的用户脚本是有限的,尤其是涉及到 iframe 时。

为什么你不能以其他方式做到这一点?例如:

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

这里更好的答案是 Chrome Extensions ,您可以使用 Content Script 进行更多控制而不是用户脚本。

关于javascript - Chrome iframe 父级未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com