system模块

PhantomJS提供了system模块, 用于获取系统信息、系统环境变量、执行phantomjs jsFile [args]...命令时候传入的参数。

导入模块:

var system = require('system');

在交互模式下,先导入此模块,再输入system,会打印出此对象的详细构造:

{
   "args": [
      ""
   ],
   "env": {
      "ANDROID_HOME": "/Users/leleliu008/bin/android-sdk-macOS",
      "Apple_PubSub_Socket_Render": "/private/tmp/com.apple.launchd.UjlaYPro0S/Render",
      "CLICOLOR": "1",
      "COLORFGBG": "15;0",
      "COMMAND_MODE": "unix2003",
      "HOME": "/Users/leleliu008",
      "ITERM_PROFILE": "Default",
      "ITERM_SESSION_ID": "w0t0p0:63420658-02DA-462B-96F3-D2071567616A",
      "LC_CTYPE": "UTF-8",
      "LOGNAME": "leleliu008",
      "LSCOLORS": "gxfxaxdxcxegedabagacad",
      "OLDPWD": "/Users/leleliu008/git/document/node.js",
      "PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/leleliu008/bin/android-sdk-macOS/tools:/Users/leleliu008/bin/android-sdk-macOS/platform-tools:/Users/leleliu008/bin/android-sdk-macOS/build-tools/23.0.2",
      "PWD": "/Users/leleliu008/git/document",
      "SECURITYSESSIONID": "186a7",
      "SHELL": "/bin/bash",
      "SHLVL": "1",
      "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.3nieIp2L2g/Listeners",
      "TERM": "xterm-256color",
      "TERM_PROGRAM": "iTerm.app",
      "TERM_PROGRAM_VERSION": "3.0.14",
      "TERM_SESSION_ID": "w0t0p0:63420658-02DA-462B-96F3-D2071567616A",
      "TMPDIR": "/var/folders/jw/7vmz4m1970932zln9tb1whxr0000gn/T/",
      "USER": "leleliu008",
      "XPC_FLAGS": "0x0",
      "XPC_SERVICE_NAME": "0",
      "_": "/usr/local/bin/phantomjs",
      "__CF_USER_TEXT_ENCODING": "0x1F5:0x0:0x0"
   },
   "isSSLSupported": true,
   "os": {
      "architecture": "64bit",
      "name": "mac",
      "release": "16.4.0",
      "version": "unknown"
   },
   "pid": 5464,
   "standarderr": {
      "objectName": "",
      "destroyed(QObject*)": "[Function]",
      "destroyed()": "[Function]",
      "objectNameChanged(QString)": "[Function]",
      "deleteLater()": "[Function]",
      "read(QVariant)": "[Function]",
      "read()": "[Function]",
      "write(QString)": "[Function]",
      "seek(qlonglong)": "[Function]",
      "readLine()": "[Function]",
      "writeLine(QString)": "[Function]",
      "atEnd()": "[Function]",
      "flush()": "[Function]",
      "close()": "[Function]",
      "getEncoding()": "[Function]",
      "setEncoding(QString)": "[Function]"
   },
   "standardin": {
      "objectName": "",
      "destroyed(QObject*)": "[Function]",
      "destroyed()": "[Function]",
      "objectNameChanged(QString)": "[Function]",
      "deleteLater()": "[Function]",
      "read(QVariant)": "[Function]",
      "read()": "[Function]",
      "write(QString)": "[Function]",
      "seek(qlonglong)": "[Function]",
      "readLine()": "[Function]",
      "writeLine(QString)": "[Function]",
      "atEnd()": "[Function]",
      "flush()": "[Function]",
      "close()": "[Function]",
      "getEncoding()": "[Function]",
      "setEncoding(QString)": "[Function]"
   },
   "standardout": {
      "objectName": "",
      "destroyed(QObject*)": "[Function]",
      "destroyed()": "[Function]",
      "objectNameChanged(QString)": "[Function]",
      "deleteLater()": "[Function]",
      "read(QVariant)": "[Function]",
      "read()": "[Function]",
      "write(QString)": "[Function]",
      "seek(qlonglong)": "[Function]",
      "readLine()": "[Function]",
      "writeLine(QString)": "[Function]",
      "atEnd()": "[Function]",
      "flush()": "[Function]",
      "close()": "[Function]",
      "getEncoding()": "[Function]",
      "setEncoding(QString)": "[Function]"
   }
}
1.1、system.args数组

system.args数组是执行phantomjs jsFile [args]...命令时候传入的参数列表。

bash中的参数一样,system.args[0]是本程序的名称,从system.args[1]开始才是真正的参数。

示例:

var system = require('system');
if (system.args.length === 1) {
    console.log('Usage: phantomjs xx.js <URL>');
} else {
    //TODO
}
phantom.exit();

运行:

phantomjs xx.js yy
1.2、system.env对象

system.env对象是操作系统的环境变量集合。

示例:

var system = require('system');
console.log(system.env.PATH);
phantom.exit();

运行:

phantomjs xx.js
1.3、system.os对象

system.os对象可以获取到操作系统名称和版本、CPU架构等信息。

示例:

var system = require('system');
console.log("当前系统的CPU架构:" + system.os.architecture + ", " + system.os.name + "操作系统,系统版本是" + system.os.release);
phantom.exit();

运行:

phantomjs xx.js
1.4、system.standardin对象

标准输入对象,里面有很多相关操作。

1.5、system.standardout对象

标准输出对象,里面有很多相关操作。

1.6、system.standarderr对象

标准错误输入对象,里面有很多相关操作。