2016-11-22 77 views
0

我正在使用xul創建一個firefox插件。我已經使用以下腳本添加了動態iframe:postMessage不能在firefox插件中使用xul動態添加iframe

//addon script: 
let chromeUrl = 'https://myserver/downloadProduct.html'; 
         Components.utils.import('resource://gre/modules/Services.jsm');//Services 
         let activeWindow = Services.wm.getMostRecentWindow('navigator:browser'); 

         let mainDocument = activeWindow.document; 

         let iframeEl; 
         iframeEl = mainDocument.createElement('iframe'); 

         iframeEl.id = "d"; 
         iframeEl.setAttribute('src',chromeUrl); 
         iframeEl.setAttribute("tooltip", "aHTMLTooltip"); 
         iframeEl.setAttribute("autocompleteenabled", true); 
         iframeEl.setAttribute("autocompletepopup", "PopupAutoComplete"); 
         iframeEl.setAttribute("disablehistory",true); 
         iframeEl.setAttribute('type', 'content'); 
         iframeEl.setAttribute('height', '32px'); 


         window.document.documentElement.insertBefore(iframeEl, window.document.documentElement.window); 
window.addEventListener("message", receiveMessage,false); 

上述腳本正在頁面上成功添加新的iframe。現在我想從iframe接收消息給我的插件。我在iframe腳本中創建了一個postMessage事件,腳本如下:

//iFrame Script: 
<script type="text/javascript"> 
     $(document).ready(function() { 
      $("#Download").click(function() { 
       parent.postMessage({ Action: "DOWNLOADED", Result: null }, "*"); 
      }) 

      $("#NotNow").click(function() { 
       parent.postMessage({ Action: "NOT_NOW", Result: null }, "*"); 
      }) 

      $("#Never").click(function() { 
       parent.postMessage({ Action: "DO_NOT_SHOW", Result: null }, "*"); 
      }) 
     }); 
    </script> 

但是我無法在我的firefox插件中接收消息。

任何人都可以幫忙嗎?

+0

我們將需要知道第二個代碼塊中第一個代碼塊中的「window」是什麼(即,您已經完成了'var window =?what?;')和'parent'。 – Makyen

+0

僅供參考:如果您正在使用未隨您的加載項提供的HTML文件(即來自基於Web的源代碼),那麼您很可能無法獲得批准在AMO上分發的加載項的用戶界面。 – Makyen

+0

請提供您的HTML文件(或至少一個有您的按鈕),所以我們有必要的代碼來測試(即[mcve])。 – Makyen

回答

0

解決辦法是:

window.document.getElementById("iframeId").contentWindow.document.getElementById("elementId").addEventListener('click', function() { 
           //Do something 
          }, false); 

該腳本可以直接在頁面上添加動態的iframe後加入。

相關問題