Google Closure和Chrome打包的应用程序:兼容性吗? [英] Google Closure and Chrome packaged apps: compatibility?

查看:95
本文介绍了Google Closure和Chrome打包的应用程序:兼容性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Closure,并且正在尝试制作Chrome打包的应用.

I am using Google Closure and and I am trying to make a Chrome packaged app.

我对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;
  }
};

Google Closure是否与Chrome浏览器打包的应用程序不兼容? 对于大型Javascript项目,闭包有很多好处,因此很难放弃这样一个有价值的工具.

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.

我知道,如果除了Closure库之外还使用Closure Compiler,则不会出现goog.require,但这显然会使开发和调试更加困难.

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.

推荐答案

只要您未编译运行它或不使用高级编译功能进行编译,就必须使用document.createElement("script");来重新编写doc.write.

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");

因此将doc.write行替换为:

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.

这篇关于Google Closure和Chrome打包的应用程序:兼容性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆