2013-02-15 42 views
0

(交叉貼here打開和寫入到一個新的窗口

嗨,

我有一個沙箱頁面(在我的清單中指定的),它加載到一個iframe中我的分機的背景頁面。從我的沙盒頁面中,我想打開一個新窗口並寫入它,即:

var win = window.open(); win.document.write('< p & gtHello!</p >');

這適用於我的擴展的後臺頁面和常規網頁,但是當從內容腳本或我的沙盒頁面調用時,窗口會打開,但我無法訪問win對象(已定義,但爲空)--- console .log輸出「窗口{}」)。

我認爲這是由於同源策略(每個窗口在沙箱環境內都被賦予一個uinque源)。然而,由於窗口打開about:空白頁面,我很困惑爲什麼這會很重要。

這是功能嗎?有沒有一個參數可以添加到我的清單中以避免這種情況?有沒有人知道不涉及使用postMessage回到我的背景頁面的解決方法?我的理想解決方案是讓我的沙盒腳本打開一個新窗口並直接與它交互,而不是通過消息傳遞。

我可以提供一個完整的例子,如果有必要,但我希望有人可能只是知道他們的頭頂。如果有幫助,我在Mac上運行Chrome 24.0.1312.57,在Ubuntu上運行24.0.1312.68。

感謝,

漢克

回答

0

每交叉後here,這個問題確實是打開的窗口被賦予獨特的起源。標準工作組(SWG)的成員認爲,對於以下內容不應例外:在繼承沙盒原點的空白頁面上更安全一些,這是故意的。

但是,爲了解決這個問題,至少爲了我的目的,我可以使用下面的方法。首先,忘記沙箱。此解決方法使用嵌入在背景頁面中的iframe,其src url設置爲data:text/html,...。這給iframe一個獨特的起源,所以它不再處於擴展空間。這意味着可以使用eval,並且不能訪問chrome apis。與沙盒不同,從iframe打開的窗口與iframe的起源相同,從而允許訪問創建的窗口。例如:

在後臺HTML頁面:

<html> 
<head> 
    ... 
    <script src="background.js"></script> 
    ... 
</head> 
<body> 
    ... 
    <iframe id="myframe"></iframe> 
    ... 
</body> 
</html> 

在background.js:

... 
document.getElementById('myframe').setAttribute('src', 'data:text/html,'+ 
    encodeURI('<html><head>'+ 
    '<script src='+chrome.extension.getURL('jquery.js')+'></script>'+ 
    '<script src='+chrome.extension.getURL('myscript.js')+'></script>'+ 
    ... 
    '</head><body></body></html>' 
)); 
... 

在myscript.js

jQuery(document).ready(function(){ 
    ... 
    // To receive messages from background.js. 
    window.addEventListener('message', function(e){ ... }); 

    // To send messages to background.js. 
    parent.postMessage({...}, '*'); 

    // To open AND ACCESS a window. 
    var win = window.open(); 
    win.document.write('Hello'); // Fails in sandbox, works here. 

    // Eval code, if you want. Can't do this from an extension 
    // page loaded normally unless you allow eval in your manifest. 
    // Here, it's okay! 
    eval('var x = window.open(); x.document.write("Hi!")'); 

    // No chrome apis. 
    chrome.log(chrome.extension); // -> undefined 
    chrome.log(chrome.windows); // -> undefined 

    // No direct access to background page (e.g., parent). 
    chrome.log(parent); // -> Window with no properties. 
    ... 
}); 
0

內容腳本,顧名思義,是您加載,所以我真的不知道怎麼你的腳本可以「常規網頁」上工作的外部一般網頁的一部分,但而不是內容腳本。你的意思是說,當你將代碼嵌入到自己的頁面中時,代碼能夠正常工作,而不是通過內容腳本嵌入到其他頁面中?

無論如何,如果腳本在您的背景頁面上正常工作,您可以隨時嘗試發送消息。 (更多在這裏:http://developer.chrome.com/extensions/messaging.html

腳本爲您的沙箱/ contentscript:

//send message to background page 
chrome.extension.sendMessage({todo: "newWindow"}); 

在後臺頁面:

//create a listener 
chrome.extension.onMessage.addListener(
    function(request, sender) { 
    if (request.todo === "newWindow") { 
    //do your stuff here 
     var win = window.open(); win.document.write('<p&gtHello!</p>'); 
    } 
    }); 
+0

我的意思是, ,如果你寫一個網頁,完全忽略擴展es'var win = window.open(); win.document.write('<p> Hello!</p >')',它按預期工作。但是,從我的擴展中的沙箱內,情況並非如此。在我的交叉帖子後面,來自chrome團隊的人證實了這個問題,即從沙箱打開的任何窗口都有唯一的來源。 – Hank 2013-02-16 16:24:31