2011-09-08 88 views
1

我有一些無特權的應用程序與一些特權的JavaScript代碼與系統交互。現在我想混合使用託管在服務器中的應用程序的特權JavaScript(jsctypes)。遠程應用程序將加載到iframe中,無鉻應用程序和遠程應用程序之間的交互通過html5 postMessage進行。Iframe跨域通信和無鉻域

父做後消息包含iframe和iframe要成功接收與e.origin爲「資源:\ APP」 而如果我嘗試的postMessage從iframe與域資源window.parent:\應用在父在onMessage聽者不調用

佈局,

上執行,>無鉻實施例\ testapp \ index.html的 XUL應用程序在所述邊框生成文件夾中生成並顯示了下面的。

 
+-----------------------------------Chromeless----+ 
|             | 
| --- MessageToIframeButton      | 
|             | 
| +--------------------------Iframe--+   | 
| |Msg Recvd from: resource://app |   | 
| |(this is the message from parent) |   | 
| |         |   | 
| | _TxtBox_sendMessage   |   | 
| |         |   | 
| |         |   | 
| |         |   | 
| +----------------------------------+   | 
| Msg Recvd:          | 
|             | 
+-------------------------------------------------+ 

的postMessage內的iFrame

[Code] 
var sendMessage = function(){ 
     var iframe = window.parent; 
     iframe.postMessage("test","resouce://app"); 
    }; 

    [/Code] 

家長的onMessage,

  var onmessage = function(e) { 
       alert("message"); 
      } 
      if(typeof window.addEventListener != 'undefined') { 
       window.addEventListener('message', onmessage, false); 
      } 
      else if(typeof window.attachEvent != 'undefined') { 
       window.attachEvent('onmessage', onmessage); 
       } 

任何幫助表示讚賞!

Palant,我嘗試使用自定義事件來實現跨域通信,但不能得逞,

在index.html的具有特權的[無邊框的例子\ testapp \ index.html的]:

 var myExtension = { 
      myListener: function(evt) { 
      alert("Received from web page: " + 
      evt.target.getAttribute("attribute1")); 
     } 
     } 
document.addEventListener("MyExtensionEvent", function(e) {myExtension.myListener(e); }, false, true); // The last value is a Mozilla-specific value to indicate untrusted content is allowed to trigger the event. 
    //content.addEventListener("MyExtensionEvent", function(e) {myExtension.myListener(e); }, false, true); //Also tried with content. 

在遠程應用程序的iFrame remote.html: 上的一個按鈕的點擊,

var element = document.createElement("MyExtensionDataElement"); 
element.setAttribute("attribute1", "foobar"); 
document.documentElement.appendChild(element); 

var evt = document.createEvent("Events"); 
evt.initEvent("MyExtensionEvent", true, false); 
element.dispatchEvent(evt); 

觸發的事件不會泡到特權父域。如果一個eventListener被添加到iframe本身,則接收到調度的事件,並且類似地,如果在特權上下文(index.html)中生成定製事件,則父窗口確實收到通知但不通過層次結構。我是否缺少基本的東西?

+0

如果我將域名設置爲'*',iframe中的postMessage由Iframe本身而不是父窗口收到。 – Yeshvanthni

+0

同樣的示例html頁面(域名設置爲'*')在Firefox中打開郵件或者在Chrome瀏覽器中打開Chrome瀏覽器並打開瀏覽器中的html文件時發送消息給父窗口,在這種情況下,它就像一個文件和priviliged JavaScript沒有執行,也沒有像XULaplication那樣行爲 – Yeshvanthni

+0

這是因爲[link] http:// stackoverflow。com/questions/5463429/prevent-target-top-from-taking-over-ui-in-mozilla-chromeless。使Iframe頂部窗口的行爲。 – Yeshvanthni

回答

2

鑑於你鏈接到Prevent target="_top" from taking over UI in Mozilla Chromeless我猜你加載遠程應用程序的框架是一個內容框架(它肯定應該是)。這意味着在您的特權代碼和內容之間建立了一個安全邊界,特別是對於它看起來像在頂層的框架 - 它無法訪問特權文檔(易於檢查,將alert(window == window.parent)添加到框架代碼中) 。所有這些都是安全明智的,但這也意味着使用postMessage()進行通信是不可能的。

https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages上描述的通信方法有點尷尬。它的優點是可以安全地跨過安全邊界。

+0

謝謝ton.Although窗口!= window.parent.I嘗試window.location,window.parent.location,window.parent.parent.location,似乎都指向Iframe的位置。我希望自定義事件適合我。嘗試後會回發。 – Yeshvanthni