Copy div from parent website to a textarea in iframe

前端 未结 2 1925
情歌与酒
情歌与酒 2021-01-28 21:49

In the Google Translator I\'ve made a second instance of Google Translate with

var makediv = document.createElement(\"secondinstance\");
makediv.innerHTML = \'&l         


        
相关标签:
2条回答
  • 2021-01-28 22:22

    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)
    

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

    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...

    0 讨论(0)
  • 2021-01-28 22:37

    Had similar problem recently trying to access iframe. Unfortunately it is impossible IF it is a cross-domain. Read more: Get element from within an iFrame

    Hope it helps.

    0 讨论(0)
提交回复
热议问题