gpt4 book ai didi

google-chrome-extension - 从 chrome 扩展访问/编辑 iframe 的内容

转载 作者:行者123 更新时间:2023-12-04 05:21:13 32 4
gpt4 key购买 nike

我想通过 chrome 扩展编辑 iframe 的内容,但无法编辑,因为 iframe 的 contentWindow/contentDocument 返回未定义。
一直在寻找答案,据我所知,如果 iframe 从一开始就存在于页面上,则很容易修复,但事实并非如此。
有问题的 iframe 是 iframe 形式的 gmail 撰写文本区域,它被动态添加到 DOM 中。
有没有办法通过 chrome 扩展来做到这一点?

编辑:
对不起。
Rob W 让我意识到 contentDocument 确实可以访问。我在这里要做的是在 Gmail 中添加一些额外的功能,在工具栏中有一个按钮,可以在撰写窗口中添加一些 html。我现在让按钮部分工作,它添加了 html,但它不像它应该的那样在插入符号处执行它,因为我无法访问 contentWindow 以执行此操作:

range = iWindow.getSelection().getRangeAt(0);
node = range.createContextualFragment(html);
range.insertNode(node);

有什么解决办法吗?

最佳答案

contentWindow is not accessible in Chrome extensions .contentDocument ,另一方面,如果 iframe 位于同一来源(Gmail 就是这种情况),则可以正确返回框架内的文档。

以下是 Gmail 的示例。为简单起见,我假设文档中只有一个编辑器。

var poller = setInterval(function() {
var editor = document.querySelector('iframe.editable');
if (editor && editor.contentDocument && !editor.__rwHandled) {
editor.__rwHandled = true; // Run once for an editor instance
var textNode = document.createTextNode('It works!');
editor.contentDocument.body.appendChild(textNode);
}
}, 200);

每当您想停止轮询器时,只需使用 clearInterval(poller); .确保您的检测方法成本低廉。例如:查询 DOM 节点是可以的,打开新窗口不是。

关于google-chrome-extension - 从 chrome 扩展访问/编辑 iframe 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13669671/

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