0

I'm trying to embed Google Translate into Google Translate with iframe. I used the code:

var a = document.createElement('iframe');
a.src = "https://translate.google.com/"; 
a.id = "iframenaturalID";
a.width = "400";
a.height = "1100";
document.querySelector('body').appendChild(a)

But it creates another instance of Google Translate inside the iframe. So I get one iframe inside the main window, and the second iframe inside the first iframe.

I tried doing this (and multiple variations with window.parent [but I guess that works only when calling the parent window from iframe]):

var bodydocumentmain = document.getElementsByTagName("body")[0];
bodydocumentmain.id = "bodyMainID";
var innerMainBodyDoc3 = document.getElementById("bodyMainID");
var innerMainBodyDoc = innerMainBodyDoc3.contentDocument || innerMainBodyDoc3.contentWindow.document;
var a = innerMainBodyDoc.createElement('iframe');
a.src = "https://translate.google.com/"; 
a.id = "iframenaturalID";
a.width = "400";
a.height = "1100";
innerMainBodyDoc.querySelector('body').appendChild(a)

But that doesn't work. Of course I could simply do:

var iframe1 = document.getElementById('iframenaturalID');
var innerDoc = iframe1.contentDocument || iframe1.contentWindow.document;
innerDoc.getElementById('iframenaturalID').remove();

But that seems a bit crooked. Is it possible to make an iframe only inside the main window, without affecting the first iframe?

teg_brightly
  • 468
  • 6
  • 20
  • Lost me at *"embed Google Translate into Google Translate"*. Where are you actually running this code and what are expected results? – charlietfl Oct 14 '18 at 18:59
  • I'm running this code in Tampermonkey. The first instance gets some words input, and the second instance applies Google spelling suggestions. So the original input gets translated along with the auto-corrected text from the second instance (https://stackoverflow.com/questions/52674962/copy-div-from-parent-website-to-a-textarea-in-iframe). – teg_brightly Oct 14 '18 at 19:11
  • Because sometimes auto-correction corrects existing words, so having two instances helps to avoid that. – teg_brightly Oct 14 '18 at 19:22
  • So 2 instances would mean 2 iframes ... right? – charlietfl Oct 14 '18 at 19:33
  • The first instance is the main (the original) window, and the second is the embedded iframe. I should have said "instances of Google Translate". – teg_brightly Oct 14 '18 at 19:36
  • OK, so not clear what problem is adding iframe to main page to create a second instance – charlietfl Oct 14 '18 at 19:38
  • The iframe creates another Google Translate instance within itself (because it is the same page). So I thought there could be a way to prevent iframe recursion (creating iframe inside of iframe inside of iframe and so on). – teg_brightly Oct 14 '18 at 19:42

0 Answers0