簡體   English   中英

將div從父網站復制到iframe中的textarea

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

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

我正在嘗試將文本從第一個實例的自動更正復制到第二個實例的textarea: 圖片

我正在嘗試這樣做(如果我只是在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);

但是控制台說它無法在eval處讀取未定義的屬性“ document”,並說問題出在這一行:

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

我嘗試嵌入另一個網站,但也沒有成功。 我還嘗試通過“ iframenaturalID”調用iframe,並嘗試在沒有contentWindow情況下編寫TheiFrameInstance.contentWindow.document.querySelectorAll ,但似乎沒有任何效果。 非常感謝您的幫助。

最近在嘗試訪問iframe時遇到了類似的問題。 不幸的是,如果它是跨域的,那是不可能的。 閱讀更多: 從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)

並且(將文本內容從更正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);

現在它做了我想做的事情,但是我仍然收到錯誤消息: Uncaught TypeError: Cannot set property 'value' of null at eval ,它指向這一行: source.value = destination.textContent; 雖然這不是一個大問題,但仍然奇怪的是它返回了這個錯誤...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM