2016-11-22 9 views
0

xul을 사용하여 Firefox 추가 기능을 만들고 있습니다. 다음 스크립트를 사용하여 동적 iframe을 추가했습니다.xM을 사용하여 firefox addon에서 postMessage가 동적으로 추가 된 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에서 addon으로 메시지를 보내려고합니다. 다음과 같이 나는 스크립트를 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> 

을하지만 내 파이어 폭스 애드온에서 메시지를 수신 할 수 없습니다입니다.

아무도 도와 줄 수 있습니까?

+0

우리는 두 번째 코드 블록에서 첫 번째 코드 블록에 'window'가 무엇인지 (즉, var window = ?? what ??;) 및'parent'를 알고 있어야합니다. – Makyen

+0

참고 : 애드온과 함께 제공되지 않는 HTML 파일 (웹 기반 소스의 HTML 파일)을 사용하는 경우 AMO에서 배포하도록 승인 된 애드온을 얻지 못할 가능성이 있습니다 귀하의 사용자 인터페이스. – Makyen

+0

테스트에 필요한 코드 (예 : [mcve])를 갖기 위해 HTML 파일 (또는 버튼이있는 파일)을 제공하십시오. – Makyen

답변

0

해결 방법은 다음과 같습니다

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

이 스크립트는 바로 페이지에서 동적은 iframe을 추가 한 후 추가 할 수 있습니다.