简体   繁体   English

将div从父网站复制到iframe中的textarea

[英]Copy div from parent website to a textarea in iframe

In the Google Translator I've made a second instance of Google Translate with 在Google翻译器中,我使用了第二个Google翻译实例

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: 我正在尝试将文本从第一个实例的自动更正复制到第二个实例的textarea: 图片

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): 我正在尝试这样做(如果我只是在Chrome检查器中复制html(使用[0]或[1]选择具有相同ID的元素},则此代码有效,但是只能使用嵌入iframe的两个翻译实例) ):

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

But the console says that it cannot read property "document" of undefined at eval, and says that the problem is in this line: 但是控制台说它无法在eval处读取未定义的属性“ document”,并说问题出在这一行:

  TheiFrameInstance.contentWindow.document.querySelectorAll("#source").value = childAnchors1.textContent;

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. 我还尝试通过“ iframenaturalID”调用iframe,并尝试在没有contentWindow情况下编写TheiFrameInstance.contentWindow.document.querySelectorAll ,但似乎没有任何效果。 I would very much appreciate any help. 非常感谢您的帮助。

Had similar problem recently trying to access iframe. 最近在尝试访问iframe时遇到了类似的问题。 Unfortunately it is impossible IF it is a cross-domain. 不幸的是,如果它是跨域的,那是不可能的。 Read more: Get element from within an iFrame 阅读更多: 从iFrame中获取元素

Hope it helps. 希望能帮助到你。

Ok, I made it work with a different method of creating iframe: 好的,我使它与创建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)

And (copying textcontent from the correction div to the iframe textarea): 并且(将文本内容从更正div复制到iframe文本区域):

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

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; 现在它做了我想做的事情,但是我仍然收到错误消息: Uncaught TypeError: Cannot set property 'value' of null at eval ,它指向这一行: source.value = destination.textContent; . It's not a big problem though, but still it's strange that it returns this mistake... 虽然这不是一个大问题,但仍然奇怪的是它返回了这个错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM