2013-06-06 48 views
0

我正在使用Google Closure,並試圖製作Chrome打包的應用程序。谷歌關閉和Chrome打包的應用程序:兼容性?

我對goog.require調用會導致錯誤:

Uncaught document.write() is not available in packaged apps. 

罪魁禍首是base.js

goog.writeScriptTag_ = function(src) { 
    if (goog.inHtmlDocument_()) { 
    var doc = goog.global.document; 

    // If the user tries to require a new symbol after document load, 
    // something has gone terribly wrong. Doing a document.write would 
    // wipe out the page. 
    if (doc.readyState == 'complete') { 
     // Certain test frameworks load base.js multiple times, which tries 
     // to write deps.js each time. If that happens, just fail silently. 
     // These frameworks wipe the page between each load of base.js, so this 
     // is OK. 
     var isDeps = /\bdeps.js$/.test(src); 
     if (isDeps) { 
     return false; 
     } else { 
     throw Error('Cannot write "' + src + '" after document load'); 
     } 
    } 

    doc.write(
     '<script type="text/javascript" src="' + src + '"></' + 'script>'); 
    return true; 
    } else { 
    return false; 
    } 
}; 

是谷歌關閉谷歌瀏覽器打包應用程序不兼容? 關閉對於大型Javascript項目有很多好處,因此很難放棄這樣一個有價值的工具。

編輯:我知道,如果除了Closure庫使用Closure編譯器,沒有goog.require,但這顯然使開發和調試更困難。

回答

1

Closure Dev Mode & Chrome Packaged App -- "document.write() is not available in the sandbox of packaged apps"

只要你運行它未編譯或不符合你必須重新編寫使用了document.createElement(「腳本」)的doc.write高級編譯編譯;

因此,與替代TE doc.write行:

var script = document.createElement('script'); 
    script.src = src; 
    script.type = 'text/javascript'; 
    goog.global.document.getElementsByTagName("head")[0].appendChild(script); 
    return true; 

提前編譯代碼不應該需要這個,因爲它在一個文件中把所有的代碼使用起來。

+0

它保證工作?看起來,當我嘗試在另一個項目上動態加載Javascript時,腳本以不確定的順序異步執行。 –

+0

我證實了這一觀察。它可能適用於小文件或其他內容,但是JavaScript是非順序加載和評估的。 –

+0

這必須是谷歌使用document.write的原因。您可以嘗試calcdeps.py併爲所需的所有文件創建<腳本標記。您可以使用輸出列表來獲取列表,然後創建<腳本標記以在使用它們之前加載所有需要的js文件。 goog.require應該檢測到文件已經加載並且不會執行document.write。 https://developers.google.com/closure/library/docs/calcdeps#opts – HMR