常见问题解答


console.log 无法输出到 Linux 终端

--enable-logging=stderr 应该在命令行中使用;更多信息请参见:https://www.chromium.org/for-testers/enable-logging

var crypto = require('crypto') 获取错误对象

Chromium 有自己的全局 crypto 对象,无法覆盖。因此,您无法使用相同的变量名 crypto。将您的变量名更改为其他名称,例如 nodeCrypto,将起作用。

AngularJS 中的图像损坏,并在 DevTools 中收到“无法加载资源 XXX net::ERR_UNKNOWN_URL_SCHEME”

AngularJS 为未知方案添加了 unsafe: 前缀以防止 XSS 攻击。NW.js 和 Chrome 应用程序中的 URL 以 chrome-extension: 方案开头,AngularJS 不认识该方案。解决方法是通过添加以下行将已知方案的白名单配置到 AngularJS 中

myApp.config(['$compileProvider',
  function($compileProvider) {
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*((https?|ftp|file|blob|chrome-extension):|data:image\/)/);
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|chrome-extension):/);
  }]);

无法在 AngularJS 2+ 中看到异常报告

AngularJS 2 尝试在全局变量 global 中注册异常处理程序。但是,它已经在 NW.js 环境中存在,这会阻止 DevTools 中显示的异常报告。解决方法是在加载任何 AngularJS 库之前将 global 重命名为其他名称。例如,

<script>
window.nw_global = window.global;
window.global = undefined;
</script>
<!-- Angular 2 Dependencies -->

如何使用 ESC 键退出全屏模式?

通常用户希望使用 ESC 键退出全屏模式。默认情况下,NW.js 不会为退出全屏模式绑定 ESC 快捷键,而是提供用于进入和退出全屏模式的 API。这将使开发人员能够更好地控制全屏模式。

要启用 ESC 键以退出全屏模式,可以使用 快捷键 API

nw.App.registerGlobalHotKey(new nw.Shortcut({
  key: "Escape",
  active: function () {
    // decide whether to leave fullscreen mode
    // then ...
    nw.Window.get().leaveFullscreen();
  }
}));

如何在 VSCode 中为 nw 获取“智能感知”(自动完成)?

Screenshot of VSCode Intellisense for NW.js

  1. 安装 nwjs-types
    npm install --save-dev nwjs-types
    
  2. 强制 VSCode 的 TypeScript 引擎加载。
  3. 在 JS 文件的顶部放置 // @ts-check
  4. 您无需使用 TypeScript 才能使此方法起作用。
  5. VSCode 在看到 // @ts-check 时会加载 TypeScript 引擎,然后在您的 node_modules 中查找类型,然后找到并加载 nwjs-types
  6. 然后智能感知将知道如何为 nw. 自动完成。