将div从父网站复制到iframe中的textarea [英] Copy div from parent website to a textarea in iframe
问题描述
在Google翻译器中,我已经制作了第二个Google翻译实例
In the Google Translator I've made a second instance of Google Translate with
var makediv = document.createElement("secondinstance");
makediv.innerHTML = '<iframe id="iframenaturalID" width="1500" height="300" src="https://translate.google.com"></iframe>';
makediv.setAttribute("id", "iframeID");
var getRef = document.getElementById("gt-c");
var parentDiv = getRef.parentNode;
parentDiv.insertBefore(makediv, getRef);
我正在尝试将文本从第一个实例的自动更正复制到文本区域第二个例子:
And I'm trying to copy text from auto-correction of the first instance to the textarea of the second instance:
我是尝试这个(如果我只是在Chrome检查器中复制html {使用[0]或[1]选择具有相同ID的元素},此代码可以工作,但是只能使用iframe嵌入的两个翻译实例):
I'm trying this (this code works if I just copy html within the Chrome inspector{using [0] or [1] to select elements with the same IDs}, however it's only possible to use two instances of the translator with iframe embedding):
setInterval(function() {
var childAnchors1 = window.parent.document.querySelectorAll("#spelling-correction > a");
var TheiFrameInstance = document.getElementById("iframeID");
TheiFrameInstance.contentWindow.document.querySelectorAll("#source").value = childAnchors1.textContent;
}, 100);
但控制台说它无法读取eval中未定义的属性文档,并说问题在于这一行:
But the console says that it cannot read property "document" of undefined at eval, and says that the problem is in this line:
TheiFrameInstance.contentWindow.document.querySelectorAll("#source").value = childAnchors1.textContent;
我试过嵌入另一个网站,但它也没有用。我还尝试通过iframenaturalID调用iframe,并尝试在没有 contentWindow
TheiFrameInstance.contentWindow.document.querySelectorAll >,但似乎没有任何作用。
我非常感谢任何帮助。
I've tried embedding another website, and it also didn't work. I also tried to call iframe by "iframenaturalID", and tried to write TheiFrameInstance.contentWindow.document.querySelectorAll
without the contentWindow
, but nothing seems to work.
I would very much appreciate any help.
推荐答案
好的,我使用了另一种创建iframe的方法:
Ok, I made it work with a different method of creating iframe:
var a = document.createElement('iframe');
a.src = "https://translate.google.com";
a.id = "iframenaturalID";
a.width = "1000";
a.height = "500";
document.querySelector('body').appendChild(a)
和(复制textcontent从更正div到iframe textarea):
And (copying textcontent from the correction div to the iframe textarea):
let iframe = document.getElementById("iframenaturalID");
setInterval(function() {
let source = iframe.contentWindow.document.getElementById("source");
let destination = window.parent.document.querySelector("#spelling-correction > a");
source.value = destination.textContent;
}, 100);
现在它做我想做的事情,但是我仍然收到错误消息:未捕获的TypeError:无法在eval
处设置null
的属性'value',它指向此行: source.value = destination.textContent;
。这不是一个大问题,但它仍然很奇怪,它会返回这个错误...
Now it does what I tried to do, however I still get mistake message: Uncaught TypeError: Cannot set property 'value' of null
at eval
, which points at this line: source.value = destination.textContent;
. It's not a big problem though, but still it's strange that it returns this mistake...
这篇关于将div从父网站复制到iframe中的textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!