【问题标题】:Use of colors module in phantomjsphantomjs 中颜色模块的使用
【发布时间】:2014-07-11 17:12:26
【问题描述】:

我需要在 phantomjs 中使用颜色(或类似的 npm 模块,欢迎提出建议)。这是我的代码:

var page = require('webpage').create();  
var url = require('system').args[1];
var fs = require('fs');
var colors = require('colors');

console.log(">>In Phantomjs Process...".yellow);
page.open(url, function (status) {
    if (status !== 'success') {
       console.log(">>Error Opening Webpage.".red);
       phantom.exit();
    } 
    else {
       console.log(">>Page Loaded Successfully...".yellow);
       window.setTimeout(function () {
           fs.write('index.txt', page.content, 'w');
           phantom.exit();
       }, 10000);
    }
});

这段代码似乎在 Windows 7、x86 机器上运行良好,phantomjs 版本为 1.9.7,

$ phantomjs fetchDOM.js "http://www.google.com"
>>In Phantom Process...

>>Page Loaded Successfully...

但是在带有 phantomjs 版本 1.9.7 的 Ubuntu 13.04 x86 机器上,

$ phantomjs fetchDOM.js "http://www.google.com"
Unknown module colors for require()

为什么会这样?有什么解决方法吗?提前谢谢你。

颜色模块文档可以在这里找到:https://www.npmjs.org/package/colors

编辑:如果我将var colors = require('colors'); 替换为phantom.injectJs('colors');,则错误消失,但输出与预期不符。

$ phantomjs fetchDOM.js "http://www.google.com"
undefined

undefined

【问题讨论】:

  • 无法重现:您是否真的在脚本所在的文件夹中通过npm install colors 安装了颜色?它不适用于全局,因为 phantomjs 不使用 npm 存储库。
  • 是的,我已经做到了。
  • 你试过像require('./node_modules/colors/colors.js')这样直接要求吗?
  • 没有。返回相同的错误Unknown module ./node_modules/colors/colors.js for require()
  • 另见stackoverflow.com/a/24388906/841830(它适用于回答者,但不适用于我在Linux上,所以我现在很好奇他是否使用Windows,问题就在那里......但是我猜这个问题与我设置 npm 的方式有关。)

标签: node.js colors phantomjs


【解决方案1】:

问题是process API 在 PhantomJS 中不可用。我通过修改/colors/lib/system/supports-colors.js 中的support-colors.js 来修复它

/*
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
var system = require('system');
var argv = system.args;
var env = system.env;
var os = system.os;

module.exports = (function () {
  if (argv.indexOf('--no-color') !== -1 ||
    argv.indexOf('--color=false') !== -1) {
    return false;
  }

  if (argv.indexOf('--color') !== -1 ||
    argv.indexOf('--color=true') !== -1 ||
    argv.indexOf('--color=always') !== -1) {
    return true;
  }

  //if (process.stdout && !process.stdout.isTTY) {
  //  return false;
  //}

  if (os.architecture === 'win32' && os.name === 'windows') {
    return true;
  }

  if ('COLORTERM' in env) {
    return true;
  }

  if (env['TERM'] === 'dumb') {
    return false;
  }

  if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env['TERM'])) {
    return true;
  }

  return false;
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 2014-09-03
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-10-01
    相关资源
    最近更新 更多