2016-11-22 74 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后加入。

相关问题