2012-10-08 57 views
3

最近谷歌已經推出沙盒來增強其安全模式。他們recommend使用postMessage作爲與沙盒窗口進行通信的方式。但發佈的消息,我需要從後臺頁面發送的第一條消息:如何與Chrome打包應用程序中的沙盒窗口進行通信?

// in background page: 
chrome.app.runtime.onLaunched.addListener(function() { 
    chrome.app.window.create('index.html', { // index.html is sandboxed 
    'width': 800, 
    'height': 500 
    }, function(myWin) { 
     // myWin is ready, I want to post a message 
     console.log(myWin); // This is never called after version 23.0.1246, an error is thrown 
    }); 
}); 

23.0.1246版本運行良好,但與停止下次更新工作,也從未返回。現在這種技術會在dev和beta中引發錯誤(在24.0.128423.0.1271.17上測試)。

我已經(啓動應用後在後臺網頁控制檯)準備一個最小的Chrome封裝應用程序,顯示錯誤:https://github.com/losomo/sandbox_test

我已經提交了bug report,但不能等待好幾個月有人讀它之前,我需要在一個月內使用該應用程序。我該如何解決這個問題?我可以看到examples使用沙盒iframe仍然有效。有沒有一種方法可以在不使用iframe的情況下使用沙箱,並且仍然能夠與頁面進行通信?

這裏是清單:

{ 
    "name": "Sandbox test", 
    "description": "Sandbox test", 
    "manifest_version": 2, 
    "minimum_chrome_version": "23", 
    "version": "0.0.1", 
    "app": { 
    "background": { 
     "scripts": ["background.js"] 
    } 
    }, 
    "sandbox": { 
    "pages": ["index.html"] 
    }, 
    "permissions": [ 
    "app.window" 
    ] 
} 

而且index.html頁面:

<!doctype html> 
<html lang="cs"> 
<head> 
    <meta charset="UTF-8"> 
</head> 
<body> 
    Hi 
</body> 
</html> 
+0

你明白了嗎?你在哪裏實際發出postMessage請求。我沒有看到你的代碼。考慮通過這個[edit]鏈接添加。另外,請發佈您的清單文件。祝你好運! – jmort253

+0

Manifest文件位於[示例](https://github.com/losomo/sandbox_test)中。 postMessage應該用'console.log(myWin);'來代替,只是這樣表示代碼是不可訪問的。 – hlidka

回答

3

它是可以使用的postMessage與Chrome封裝應用程序的彈出窗口。但是,您嘗試使用的方法僅適用於非沙盒頁面,因爲您依賴受保護的Chrome API。

相反,訣竅是使用window.open打開沙箱彈出窗口。更重要的是,根據這Chromium Issue,這不是一個僥倖,window.open應該在沙盒頁面內工作,而且它!

manifest.json的:

在清單中,我不知道你試圖用app.window做。它沒有在Manifest Permissions Documentation中列出,所以我刪除了它。此外,您沒有啓用「後臺」權限。我不知道,如果它需要你想要做什麼,但如果你需要的應用程序在後臺運行,持續的文檔並呼籲它:

{ 
    "name": "Sandbox test", 
    "description": "Sandbox test", 
    "manifest_version": 2, 
    "minimum_chrome_version": "21", 
    "version": "0.0.1", 
    "app": { 
    "background": { 
     "scripts": ["background.js"] 
    } 
    }, 
    "sandbox": { 
    "pages": ["index.html"] 
    }, 
    "permissions": [ 
    "background" 
    ] 
} 

的index.html:

請記住,沙盒頁面無法訪問chrome API,因此我們包含sandbox.js來註冊postmessage監聽器。請參閱下一節。

<!doctype html> 
<html lang="cs"> 
<head> 
    <meta charset="UTF-8"> 
    <script type="text/javascript" src="sandbox.js"></script> 
</head> 
<body> 
    Hi. This page is sandboxed and cannot access chrome APIs! 
</body> 
</html> 

sandbox.js:

此文件包含在index.html頁面:

// register a postmessage listener in index.html sandbox 
window.addEventListener("message", function(event) { 
    console.info("message received in sandbox: " + event.data.message);  
}); 

背景。js:

請記住,您不能依靠Chrome API與沙箱進行通信!所以我們必須使用window.open而不是chrome.app *的API:

console.log("running in background... waiting for you to click the Sandbox App icon in chrome://newtab");  

// as soon as the launch icon is clicked, this fires 
window.addEventListener("DOMContentLoaded", function() { 

    // use window.open to create a popup 
    sandboxWin = window.open("index.html","SANDBOXED!","height=800,width=500");  

    // fire a postMessage event to the sandbox. Inspect the sandbox and see the 
     // message in the console. 
    sandboxWin.postMessage({"message":"It works!!"}, "*"); 

}); 

這是在測試鉻每日構建在Ubuntu 10.04:版本24.0.1309.0(164508)。這應該非常接近用於Windows/Mac Canary版本的版本,這些版本本質上也是每日更新的版本。

我懷疑你引用的文檔,建議使用這種方法,可能會被棄用。看起來谷歌正走向一個不同的方向。請參閱Sandbox Iframe mainpage.js,他們使用與使用沙盒化iframe進行通信時使用DOM postMessage API而不是Chrome API的相同技術!

相關問題