2011-09-01 58 views
1

我試圖添加一個iframe到使用Firefox擴展的網頁的內容/ DOM。如何使用firefox擴展將iframe添加到內容/ DOM?

這裏是我的代碼:

const XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); 

function addIframe(src) { 
    // Create the script node 
    let iframe = document.createElementNS(XUL, "iframe"); 
    iframe.setAttribute("src", src); 

     window.content.appendChild(iframe); 
} 

這是一個從這個回答修改後的版本: Execute JS from Firefox extension

我換成這一行:

// Inject it into the top-level element of the document 
document.documentElement.appendChild(script); 

這一行:

window.content.appendChild(iframe); 

因爲它將iframe插入到瀏覽器的鑲邊而不是內容區域DOM中。什麼是正確的命令插入到頁面? (我的命令不起作用)

回答

1

而正確的答案是......我!

const XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); 

function addIframe(src) { 
    // Create the script node 
    let iframe = document.createElementNS(XUL, "iframe"); 
    iframe.setAttribute("src", src); 

     window._content.document.body.appendChild(iframe); 
     return; 
} 
+1

不完全正確,我很驚訝,它適合你。你應該在同一個文檔中創建元素,並將其插入到:'window.content.document.createElementNS(...)'中。 PS:你應該使用'window.content'而不是'window._content'。 –