我花了很長時間嘗試不同的東西來實現這個功能,但沒有任何功能,文檔沒有什麼幫助。從Firefox擴展中訪問iFrame的dom
我正試圖填充一個表格,我動態注入到一個頁面的iframe。要注入iframe我做:
myObject.iframe = document.createElement("iframe");
myObject.iframe.setAttribute("src", data.url);
myObject.iframe.setAttribute("id","extension-iframe");
myObject.window.document.getElementById('publisher').appendChild(myObject.iframe);
myObject.iframe.addEventListener("load", function(){
myObject.populate(data);
}, false);
它工作正常,並且會觸發populate()方法。我的問題是獲取該iframe的文檔或窗口對象。我試過以下所有內容:
myObject.iframe.window
myObject.iframe.document
myObject.iframe.content
但這些都是未定義的。
我也試過路過事件對象到iframe的load事件監聽器,然後做:
event.originalTarget
但是,這也不能工作。
我讀了以下文件:
- https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
- https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages
但無論哪種,我不理解它,或者它只是沒有適當解釋。
任何人都可以點亮一下嗎?
謝謝!
這可能適用於瀏覽器,但XUL iframe呢?我可以證實Lukasz Salamon的答案正在爲XUL工作。 – NoBugs 2013-06-01 02:12:39