问题
I am using Google Closure and and I am trying to make a Chrome packaged app.
My call to goog.require
causes an error:
Uncaught document.write() is not available in packaged apps.
The culprit is in 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;
}
};
Is Google Closure incompatible with Google Chrome packaged apps? Closure has so many benefits for large Javascript projects it is really hard to give up such a valuable tool.
EDIT: I know that if the Closure Compiler is used in addition to the Closure libraries, there is no goog.require, but that clearly makes development and debugging much more difficult.
回答1:
Closure Dev Mode & Chrome Packaged App -- "document.write() is not available in the sandbox of packaged apps"
As long as you run it uncompiled or don't compile with advanced compilation you have to re write the doc.write with document.createElement("script");
So replace te doc.write line with:
var script = document.createElement('script');
script.src = src;
script.type = 'text/javascript';
goog.global.document.getElementsByTagName("head")[0].appendChild(script);
return true;
Advance compiled code should not need this as it puts all the used code together in one file.
来源:https://stackoverflow.com/questions/16954731/google-closure-and-chrome-packaged-apps-compatibility