天天看點

phantomjs 中文文檔

phantomjs 中文文檔 轉載

入門教程:轉載 http://www.cnblogs.com/front-Thinking/p/4321720.html

1.介紹

簡介

  PhantomJS是一個基于webkit的JavaScript API。它使用QtWebKit作為它核心浏覽器的功能,使用webkit來編譯解釋執行JavaScript代碼。任何你可以在基于webkit浏覽器做的事情,它都能做到。它不僅是個隐形的浏覽器,提供了諸如CSS選擇器、支援Web标準、DOM操作、JSON、HTML5、Canvas、SVG等,同時也提供了處理檔案I/O的操作,進而使你可以向作業系統讀寫檔案等。PhantomJS的用處可謂非常廣泛,諸如網絡監測、網頁截屏、無需浏覽器的 Web 測試、頁面通路自動化等。

位址

  PhantomJS官方位址:http://phantomjs.org/。

  PhantomJS官方API:http://phantomjs.org/api/。

  PhantomJS官方示例:http://phantomjs.org/examples/。

  PhantomJS GitHub:https://github.com/ariya/phantomjs/。

 

2.子產品目錄

Command Line Interface

phantom Object

Web Page Module

Child Process Module

File System Module

System Module

Web Server Module

3.api目錄

執行腳本

phantomjs [options] somescript.js [arg1 [arg2 […]]]

如果後面沒有任何參數, phantomjs将會進入互動模式

Command-line Options 指令行參數

  • –help or -h

  lists all possible command-line options. Halts immediately, will not run a script passed as argument.

  列出所有的指令行選項.不執行之後的腳本檔案.

  • –version or -v

  prints out the version of PhantomJS. Halts immediately, will not run a script passed as argument.

  列印出其版本.不執行之後的腳本檔案.

  • –cookies-file=/path/to/cookies.txt

  specifies the file name to store the persistent Cookies.

  指定持久儲存cookies的檔案.

  • –disk-cache=[true|false]

  enables disk cache (at desktop services cache storage location, default is false). Also accepted: [yes|no].

  啟用磁盤高速緩存(在桌面服務緩存的存儲位置,預設為false)也可接收[yes][no]

  • –ignore-ssl-errors=[true|false]

  ignores SSL errors, such as expired or self-signed certificate errors (default is false). Also accepted: [yes|no].

  忽略SSL錯誤,比如過期或自簽名的證書錯誤。(預設為false)也可接收[yes][no]

  • –load-images=[true|false]

  load all inlined images (default is true). Also accepted: [yes|no].

  加載所有圖檔。(預設為true)也可接收[yes][no]

  • –local-storage-path=/some/path

  path to save LocalStorage content and WebSQL content.

  儲存LocalStorage和WebSQL内容的檔案路徑。

  • –local-storage-quota=number

  maximum size to allow for data.

  允許資料的最大長度。

  • –local-to-remote-url-access=[true|false]

  allows local content to access remote URL (default is false). Also accepted: [yes|no].

  允許本地内容通路遠端URL,預設為false。也可接收[yes][no]。

  • –max-disk-cache-size=size

  limits the size of disk cache (in KB).

  限制磁盤緩存大小(機關為kb)。

  • –output-encoding=encoding

  sets the encoding used for terminal output (default is utf8).

  設定終端輸出編碼格式(預設為utf-8)

  • –remote-debugger-port

    starts the script in a debug harness and listens on the specified port

  調試腳本端口

  • –remote-debugger-autorun

  runs the script in the debugger immediately: ‘yes’ or ‘no’ (default)

  在debugger時候立刻運作腳本

  • –proxy=address:port

  specifies the proxy server to use (e.g. –proxy=192.168.1.42:8080).

  指定要使用的代理伺服器。

  • –proxy-type=[http|socks5|none]

  specifies the type of the proxy server (default is http).

  指定代理伺服器類型(預設http)

  • –proxy-auth

  specifies the authentication information for the proxy, e.g. –proxy-auth=username:password).

  指定代理的認證資訊

  • –script-encoding=encoding

  sets the encoding used for the starting script (default is utf8).

  設定腳本的啟動編碼(預設utf-8)

  • –ssl-protocol=[sslv3|sslv2|tlsv1|any’]

  sets the SSL protocol for secure connections (default is SSLv3).

  為安全連接配接建立SSL協定(預設SSLv3)

  • –ssl-certificates-path=

  Sets the location for custom CA certificates (if none set, uses system default).

  設定自定義CA憑證的位置(設定為空則使用系統預設)

  • –web-security=[true|false]

  enables web security and forbids cross-domain XHR (default is true). Also accepted: [yes|no].

  開啟網絡安全,并禁止跨域XHR(預設開啟)也可接收[yes][no]。

  • –webdriver(暫未翻譯)

  starts in ‘Remote WebDriver mode’ (embedded GhostDriver): ‘[[:]]’ (default ‘127.0.0.1:8910’)

  • –webdriver-selenium-grid-hub (暫未翻譯)

  URL to the Selenium Grid HUB: ‘URLTOHUB’ (default ‘none’) (NOTE: works only together with ‘–webdriver’)

自從1.3版本,可以建立一個json檔案,将以上配置寫入其中,并以這個配置檔案啟動:

  • –config=/path/to/config.json

  請注意:這個json檔案中上述選項寫法略有所不同:去掉所有的虛線,并且按照駝峰命名法來書寫。并且部分選項名稱有所變化。見下例:

{
    /* Same as: –ignore-ssl-errors=true */
    "ignoreSslErrors": true,
    /* Same as: –max-disk-cache-size=1000 */
    "maxDiskCacheSize": 1000,
    /* Same as: –output-encoding=utf8 */
    "outputEncoding": “utf8”
    /* etc. */
}
           

未直接翻譯的選項名稱:

  • –disk-cache => diskCacheEnabled
  • –load-images => autoLoadImages
  • –local-storage-path => offlineStoragePath
  • –local-storage-quota => offlineStorageDefaultQuota
  • –local-to-remote-url-access => localToRemoteUrlAccessEnabled
  • –web-security => webSecurityEnabled

phantom Object phantom 對象

  • phantom.cookies {Object[]}

  Get or set Cookies for any domain (though, for setting, use of phantom.addCookie is preferred). These Cookies are stored in the CookieJar and will be supplied when opening pertinent WebPages.

  設定或擷取cookies。(用于設定時,其值會被phantom.addCookie設定的值覆寫)。Cookies儲存在CookieJar中,在打開相關頁面時被使用。

  This array will be pre-populated by any existing Cookie data stored in the cookie file specified in the PhantomJS startup config/command-line options, if any.

  這個數列将被在config檔案或是指令行中設定的cookie檔案資料預填充。

  • phantom.cookiesEnabled {Boolean}

  Controls whether the CookieJar is enabled or not. Defaults to true.

  控制CookieJar是否開啟。預設true。

  • phantom.libraryPath {String}

  This property stores the path which is used by injectJs function to resolve the script name. Initially it is set to the location of the script invoked by PhantomJS.

  儲存injectJs函數注入腳本時的位置。最初BALABALA……(沒用了)。

  • phantom.version {Object}

  Read-only. The version of the executing PhantomJS instance. Example value: { ‘major’: 1, ‘minor’: 7, ‘patch’: 0 }.

  隻讀。執行phantomJs執行個體的版本。

  • addCookie(Object) {Boolean}

  Add a Cookie to the CookieJar. Returns true if successfully added, otherwise false.

  在CookJar中加入一個Cookie。成功傳回true,否則傳回false。

phantom.addCookie({
  \'name\': \'Added-Cookie-Name\',
  \'value\': \'Added-Cookie-Value\',
  \'domain\': \'.google.com\'
});
           
  • clearCookies() {void}

  Delete all Cookies in the CookieJar.

  删除CookieJar中的Cookies。

  • deleteCookie(cookieName) {Boolean}

  Delete any Cookies in the CookieJar with a ‘name’ property matching cookieName. Returns true if successfully deleted, otherwise false.

  删除與名稱相配的Cookies。成功傳回true,否則傳回false。

  • phantom.exit(returnValue) {void}

  Exits the program with the specified return value. If no return value is specified, it is set to 0.

  帶有指定傳回值的結束程式。預設為0.

if (somethingIsWrong) {
  phantom.exit(1);
} else {
  phantom.exit(0);
}
           
  • phantom.injectJs(filename) {boolean}

  Injects external script code from the specified file into the Phantom outer space. If the file cannot be found in the current directory, libraryPath is used for additional look up. This function returns true if injection is successful, otherwise it returns false.

  從Phantom外部空間注入指定檔案的腳本代碼。如果目前目錄下未發現,将從libraryPath指定的位置查找。注入成功傳回true,失敗傳回false。

var wasSuccessful = phantom.injectJs(‘lib/utils.js’);
           
  • page.onError

  This callback is invoked when there is a JavaScript execution error not caught by a page.onError handler. This is the closest it gets to having a global error handler in PhantomJS, and so it is a best practice to set this onError handler up in order to catch any unexpected problems. The arguments passed to the callback are the error message and the stack trace [as an Array].

  當頁面有未能處理的腳本錯誤時調用這個回調函數。這個函數最先得到phantomJs中處理的全局錯誤,是以最好的方法是設定出錯處理程式以捕捉任何意想不到的問題。傳遞的參數是錯誤消息和堆棧追蹤(其為一個數組)。

phantom.onError = function (msg, trace) {
    var msgStack = [\'PHANTOM ERROR: \' + msg];
    if (trace && trace.length) {
        msgStack.push(\'TRACE:\');
        trace.forEach(function (t) {
            msgStack.push(\' -> \' + 
            (t.file || t.sourceURL) + \': \' + t.line + 
            (t.function ? \' (in function \' + t.function + \')\' : \'\'));
        });
    }
    console.error(msgStack.join(\'\n\'));
    phantom.exit(1);
};
           

Web Page Module Web頁面子產品

  To start using, you must require a reference to the webpage module then use it to create an instance:

  開始使用之前,你必須請求一個webpage子產品的引用,并創造一個執行個體:

var webPage = require(\'webpage\');
var page = webPage.create();
           

canGoBack

canGoForward

  • clipRect {object}

  This property defines the rectangular area of the web page to be rasterized when page.render is invoked. If no clipping rectangle is set, page.render will process the entire web page.

  這個屬性定義了一個網頁的矩形裁剪區域。供page.render函數調用。如果沒有設定裁剪區域,page.render将處理整個網頁。

var webPage = require(\'webpage\');
var page = webPage.create();
page.clipRect = {
  top: 14,
  left: 3,
  width: 400,
  height: 300
};
           
  • content {string}

  This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.

  See also page.plainText to get the content without any HTML tags.

  此屬性存儲網頁的内容(主要frame),包裹在一個HTML/XML元素中。設定這個屬性将有效地以新内容重新加載網頁。

Page.plainText将獲得沒有HTML标簽的内容。

var webPage = require(\'webpage\');
var page = webPage.create();
page.open(\'http://phantomjs.org\', function (status) {
  var content = page.content;
  console.log(\'Content: \' + content);
  phantom.exit();
});
           
  • Cookies

  Get or set Cookies visible to the current URL (though, for setting, use of page.addCookie is preferred). This array will be pre-populated by any existing Cookie data visible to this URL that is stored in the CookieJar, if any.

  對目前URL設定Cookie可見或擷取之,(然而就設定而言更推薦使用page.addcookie)。如果cookieJar中有cookie,并且對此URL可見,那麼這些cookie資料将會預加載。

  Cookies is an array of objects: javascript { domain: ‘example.com’, expires: ‘Sat Oct 11 2014 21:44:33 GMT+0200 (CEST)’, expiry: 1476128618, httponly: false, name: ‘cookieName’, path: ‘/’, secure: false, value: cookieValue }

  • customHeaders {object}

  This property specifies additional HTTP request headers that will be sent to the server for every request issued (for pages and resources). The default value is an empty object {}. Headers names and values get encoded in US-ASCII before being sent. Please note that the ‘User-Agent’ should be set using the page.settings, setting the ‘User-Agent’ property in this property will overwrite the value set via page.settings.

  這個屬性為每一個發出的請求指定額外的發送到伺服器HTTP請求頭(頁面和資源)。預設值是一個空對象{}。Headers發送之前會用US_ASCII碼編碼。請注意:’User-Agent’應該使用page.settings來設定;如果設定’User-Agent’屬性,則通過page.settings設定的屬性将被重寫。

var webPage = require(\'webpage\');
var page = webPage.create();
page.customHeaders = {
  "X-Test": "foo",
  "DNT": "1"
};
           

  Do you only want these customHeaders passed to the initial page.open request?

  你隻想讓這些headers隻傳遞到初始page.open請求嗎?

var webPage = require(\'webpage\');
var page = webPage.create();
page.customHeaders = {
  "X-Test": "foo",
  "DNT": "1"
};

page.onInitialized = function() {
  page.customHeaders = {};
};
           
  • Event

    focusedFrameName

  • frameContent {string}

  This property stores the content of the web page’s currently active frame (which may or may not be the main frame), enclosed in an HTML/XML element.

  Setting the property will effectively reload the web page with the new content.

  此屬性存儲頁面目前活動的frame内容(這可能不是主要frame),包裹在一個html/xml元素中。

  設定這個屬性将會用新的内容有效地重新加載網頁。

  • frameName

    framePlainText {string}

  Read-only. This property stores the content of the web page’s currently active frame (which may or may not be the main frame) as plain text — no element tags!

  隻讀。這個屬性存儲頁面目前活動的frame内容(這可能不是主要架構)——純文字,沒有元素标簽。

frameTitle

  • frameUrl {string}

  Read-only. This property gets the current URL of the web page’s currently active frame (which may or may not be the main frame).

  隻讀。此屬性擷取目前Web頁面活動frame的URL,(這個frame可能不是主要的frame)

  • framesCount

    framesName

  Returns array with frames names.

  傳回frame名稱組成的數組。

  • libraryPath {string}

  This property stores the path which is used by page.injectJs function to resolve the script name.

  Initially it is set to the location of the script invoked by PhantomJS.

  此屬性存儲由page.injectjs函數調用腳本的路徑名。最初balabala……

  • navigationLocked {boolean}

  This property defines whether navigation away from the page is permitted or not. If it is set to true, then the page is locked to the current URL. Defaults to false.

  這個屬性定義是否允許離開頁面。如果設定為true,那麼頁面将被鎖定到目前URL。預設值為false。

  • offlineStoragePath
  • offlineStorageQuota
  • ownsPages
  • pages
  • pagesWindowName
  • paperSize {object}

  This property defines the size of the web page when rendered as a PDF.

  這個屬性定義網頁呈現為一個PDF時的尺寸。

  • plainText {string}

  Read-only. This property stores the content of the web page (main frame) as plain text — no element tags!

  See also: page.content which returns the content with element tags.

  隻讀。此屬性存儲網頁(主frame)内容為純文字——沒有元素标簽。

  參見:page.content傳回帶元素标簽的内容。

  • scrollPosition {object}

  This property defines the scroll position of the web page.

  這個屬性定義網頁的滾動位置。

var webPage = require(\'webpage\');
var page = webPage.create();
page.scrollPosition = {
  top: 100,
  left: 0
};
           
  • settings {object}

  This property stores various settings of the web page:

  此屬性存儲網頁的各種設定:

  javascriptEnabled defines whether to execute the script in the page or not (defaults to true).

  确定是否在頁面執行腳本(預設為true)

  loadImages defines whether to load the inlined images or not (defaults to true).

  确定是否加載圖檔(預設為true)

  localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (defaults to false).

  确定本地資源(如檔案資源)可以通路遠端URL(預設為false)

  userAgent defines the user agent sent to server when the web page requests resources.

  定義當page請求資源時發送給伺服器的user-agent。

  userName sets the user name used for HTTP authentication.

  設定用于http授權的使用者姓名。

  password sets the password used for HTTP authentication.

  設定用于http授權的使用者密碼。

  XSSAuditingEnabled defines whether load requests should be monitored for cross-site scripting attempts (defaults to false).

  定義是否監控加載的請求以防其為跨站腳本攻擊。(預設為false)

  webSecurityEnabled defines whether web security should be enabled or not (defaults to true).

  定義是否開啟網頁安全(預設為true)

  resourceTimeout (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout.

  逾時時間(毫秒)任何資源請求在逾時以後停止,并繼續進行其餘頁面的部分。onResourceTimeout回調函數将會在逾時以後被調用。

  Note: The settings apply only during the initial call to the page.open function. Subsequent modification of the settings object will not have any impact.

  注意:該設定僅适用于page.open函數初始化期間。後續修改不會受到此影響。

var webPage = require(\'webpage\');
var page = webPage.create();
page.settings.userAgent = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36’;
           
  • title
page.open(\'http://phantomjs.org\', function (status) {
  console.log(page.title); // get page Title
  phantom.exit();
});
           
  • url {string}

  Read-only. This property gets the current URL of the web page (main frame).

var webPage = require(\'webpage\');
var page = webPage.create();
page.open(\'http://phantomjs.org\', function (status) {
  var url = page.url;
  console.log(\'URL: \' + url);
  phantom.exit();
});
           
  • viewportSize {object}

  This property sets the size of the viewport for the layout process. It is useful to set the preferred initial size before loading the page, e.g. to choose between ‘landscape’ vs ‘portrait’.

  這個屬性設定布局過程中的視窗大小。這被用于在加載頁面之前首選的初始值大小。例如從land***e和portrait之前選擇。

  Because PhantomJS is headless (nothing is shown), viewportSize effectively simulates the size of the window like in a traditional browser.

  因為Phantomjs是個***面浏覽器(不顯示任何東西),

  • windowName

    zoomFactor {number}

  This property specifies the scaling factor for the page.render and page.renderBase64 functions.

  The default is 1, i.e. 100% zoom.

  這個縮放因子指定page.render和page.renderBase64函數的縮放程度。預設為1,即百分之百縮放。

var webPage = require(\'webpage\');
var page = webPage.create();
page.zoomFactor = 0.25;
page.render(\'capture.png\');
           
  • addCookie(Cookie) {boolean}

  Add a Cookie to the page. If the domain does not match the current page, the Cookie will be ignored/rejected. Returns true if successfully added, otherwise false.

  給頁面添加一個cookie。如果域名不比對目前頁,cookie将被忽略或拒絕。如果成功傳回true。否則false。

var webPage = require(\'webpage\');
var page = webPage.create();

phantom.addCookie({
  \'name\'     : \'Valid-Cookie-Name\',   /* required property */
  \'value\'    : \'Valid-Cookie-Value\',  /* required property */
  \'domain\'   : \'localhost\',
  \'path\'     : \'/foo\',                /* required property */
  \'httponly\' : true,
  \'secure\'   : false,
  \'expires\'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <– expires in 1 hour */
});
           
  • clearCookies() {void}

  Delete all Cookies visible to the current URL.

  删除對目前URL可見的cookie。

  • close() {void}

  Close the page and releases the memory heap associated with it. Do not use the page instance after calling this.

  關閉page,釋放相關的記憶體。調用這個方法以後不再使用這個頁面執行個體。

  Due to some technical limitations, the web page object might not be completely garbage collected. This is often encountered when the same object is used over and over again. Calling this function may stop the increasing heap allocation.

  由于一些技術上的限制,網頁對象的垃圾收集可能不完全,導緻經常出現同一對象的反複調用。調用此函數可以阻止越來越多的記憶體配置設定。

  • deleteCookie(cookieName) {boolean}

  Delete any Cookies visible to the current URL with a ‘name’ property matching cookieName. Returns true if successfully deleted, otherwise false.

  删除一個對目前URL可見的,name屬性為cookieName的cookie。成功傳回true,否則false。

var webPage = require(\'webpage\');
var page = webPage.create();
page.deleteCookie(\'Added-Cookie-Name\');
evaluate(function, arg1, arg2, …) {object}
 
           

  Evaluates the given function in the context of the web page. The execution is sandboxed, the web page has no access to the phantom object and it can’t probe its own setting.

  在目前Web頁面上下文中執行給予的函數。這個函數執行在沙箱中,web頁面無法通路phantom的上下文和設定。

Get the page title from Bing.com (1)

var webPage = require(\'webpage\');
var page = webPage.create();
page.open(\'http://m.bing.com\', function(status) {
  var title = page.evaluate(function() {
    return document.title;
  });
  console.log(title);
  phantom.exit();
});
           

Get the page title from Bing.com (2)

  As of PhantomJS 1.6, JSON-serializable arguments can be passed to the function. In the following example, the text value of a DOM element is extracted.

  (自從phantom1.6)Json字元串可以傳遞給函數。在下面的例子中 ,一個dom元素的text value被提出了出來。

  The following example achieves the same end goal as the previous example but the element is chosen based on a selector which is passed to the evaluate call:

  下面的例子實作和之前的例子相同的目标,然而這個元素是基于一個傳遞到evaluate中的選擇器選擇的。

page.open(\'http://m.bing.com\', function(status) {
  var title = page.evaluate(function(s) {
    return document.querySelector(s).innerText;
  }, \'title\');
  console.log(title);
  phantom.exit();
});

           

  Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

  Note:傳給evaluate的參數和傳回值必須是一個簡單的原始對象。經驗之談:如果它可以序列化成json,那麼它就是合适的。

  Closures, functions, DOM nodes, etc. will not work!

  閉包,函數,DOM節點等等都是無法工作的!

  • evaluateAsync(function) {void}

  Evaluates the given function in the context of the web page without blocking the current execution. The function returns immediately and there is no return value. This is useful to run some script asynchronously.

  在web頁面上下文無阻塞立刻執行給定的函數。該函數立刻傳回,沒有傳回值。在異步執行一些腳本時有用。

  • getPage(windowName)
  • go(index)
  • goBack()
  • goForward()
  • includeJs(url, callback) {void}

  Includes external script from the specified url (usually a remote location) on the page and executes the callback upon completion.

  将指定URL(通常是一個遠端位址)上的腳本包括進這個page,完成後回調。

var webPage = require(\'webpage\');
var page = webPage.create();
page.includeJs(\'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\', function() {
  // jQuery is loaded, now manipulate the DOM
  var $loginForm = $(\'form#login\');
  $loginForm.find(\'input[name="username"]\').value(\'phantomjs\');
  $loginForm.find(\'input[name="password"]\').value(\'c45p3r\');
});
           
  • injectJs(filename) {boolean}

  Injects external script code from the specified file into the page (like page.includeJs, except that the file does not need to be accessible from the hosted page).

  If the file cannot be found in the current directory, libraryPath is used for additional look up.

  This function returns true if injection is successful, otherwise it returns false.

  以特殊檔案向頁面注入腳本代碼(類似page.indudejs,除了這個檔案不需要從hosted page擷取。)

  如果不能在目前檔案夾中找到檔案,程式将會去libraryPath路徑下尋找。

  如果注入成功,傳回true;否則傳回false。

var webPage = require(\'webpage\');
var page = webPage.create();
page.open(\'http://www.phantomjs.org\', function(status) {

  if (status === "success") {
  
    page.includeJs(\'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\', function() {
    
      if (page.injectJs(\'do.js\')) {
      
        var title = page.evaluate(function() {
          // returnTitle is a function loaded from our do.js file – see below
          return returnTitle();
        });
        console.log(title);
        phantom.exit();
      }
    });
  }
});

           
  • open(url, callback) {void} open(url, method, callback) {void} * open(url, method, data, callback) {void} open(url, settings, callback) {void}

  Opens the url and loads it to the page. Once the page is loaded, the optional callback is called using page.onLoadFinished, and also provides the page status to the function (‘success’ or ‘fail’).

  打開并加載URL到頁面。一旦加載完頁面,使用page.onloadFinished調用可能會有的回調。

  并且提供給該函數success或者fail的狀态。

GET google.com and report “success” or “fail”:

var webPage = require(\'webpage\');
var page = webPage.create();
page.open(\'http://www.google.com/\', function(status) {
  console.log(\'Status: \' + status);
  // Do other things here…
});
           

POST data to google.com and report “success” or “fail”:

  As of PhantomJS 1.2, the open function can be used to request a URL with methods other than GET. This syntax also includes the ability to specify data to be sent with the request. In the following example, we make a request using the POST method, and include some basic data.

  自從1.2版本開始,open函數還可用于不止GET方式請求URL。這個文法還包括傳輸資料給藥發送的請求的能力。下面是一個使用POST方法的例子,包括一些基本的資料。

var webPage = require(\'webpage\');
var page = webPage.create();
var postBody = \'user=username&password=password\';

page.open(\'http://www.google.com/\', \'POST\', postBody, function(status) {

  console.log(\'Status: \' + status);

  // Do other things here…

});
           

POST json data to your.custom.api in utf-8 encoding:

  As of PhantomJS 1.9, the open function can get an object of settings. and with a use of “encoding” key, you can set the custom encoding to your app. In this example, we’ve set the encoding to UTF8, and set the Content-Type header to application/json for making our server know the request has information in json format and not in urlencoded format.

  從1.9版本開始,open函數可以得到一個settings對象。使用“encoding”鍵,你可以設定應用的自定義編碼。在這個例子中,我們設定編碼為“utf-8”并且為application/json設定Conten-Type header,以便于讓服務端明白這個請求Json格式,不出現亂碼。

var webPage = require(\'webpage\');
var page = webPage.create();

var settings = {
  operation: "POST",
  encoding: "utf8",
  headers: {
    "Content-Type": "application/json"
  },
  data: JSON.stringify({
    some: "data",
    another: ["custom", "data"]
  })
};

page.open(\'http://your.custom.api\', settings, function(status) {
  console.log(\'Status:\' + status);
  // Do other things here…
});
           
  • openUrl(url, httpConf, settings)

    reload

    render(filename [, {format, quality}]) {void}

    Renders the web page to an image buffer and saves it as the specified filename.

  Currently, the output format is automatically set based on the file extension.

  将網頁儲存為指定檔案名的突變,目前輸出格式自動設定為檔案擴充名。

Supported formats

PNG GIF JPEG PDF

Quality

An integer between 0 and 100.

var webPage = require(\'webpage\');
var page = webPage.create();

page.viewportSize = { width: 1920, height: 1080 };

page.open("http://www.google.com", function start(status) {

  page.render(\'google_home.jpeg\', {format: \'jpeg\', quality: \'100\'});

  phantom.exit();

});
           
  • renderBase64(format)

  Renders the web page to an image buffer and returns the result as a Base64-encoded string representation of that image.

  将網頁儲存成圖檔buffer,并以base64編碼字元串傳回該圖檔。

Supported formats

PNG GIF JPEG

var webPage = require(\'webpage\');

var page = webPage.create();

page.viewportSize = {
    width: 1920,
    height: 1080
};

page.open(\'http://phantomjs.org\', function (status) {
  var base64 = page.renderBase64(\'PNG\');
  console.log(base64);
  phantom.exit();
});
           
  • sendEvent

  Sends an event to the web page.

  給頁面發送個事件。

  The events are not synthetic DOM events, each event is sent to the web page as if it comes as part of user interaction.

  這個事件并不是 虛拟DOM事件,每一個事件被當作使用者的互動發送到web頁。

  • Mouse events

  sendEvent(mouseEventType[, mouseX, mouseY, button=’left’])

  The first argument is the event type. Supported types are ‘mouseup’, ‘mousedown’, ‘mousemove’, ‘doubleclick’ and ‘click’. The next two arguments are optional but represent the mouse position for the event.

  The button parameter (defaults to left) specifies the button to push.

  For ‘mousemove’, however, there is no button pressed (i.e. it is not dragging).

  第一個參數是事件類型。支援的類型有“mouseup”,“mousedown”,“mousemove”,“doubleclick”和“click”。接下來的兩個可選參數代表了這個事件的滑鼠位置。

  Button參數(預設為左)規定了哪個按鈕被按下。

  然而對于“mousemove”事件是沒有按鈕被按下的。(不是拖拽。)

  • Keyboard events

sendEvent(keyboardEventType, keyOrKeys, [null, null, modifier])

  The first argument is the event type. The supported types are: keyup, keypress and keydown. The second parameter is a key (from page.event.key), or a string.

  第一個參數是事件類型。支援的類型有:“keyup”“keypress”和“keydown”。第二個參數是個按鍵(通過page.event.key),或者一個string.

  You can also indicate a fifth argument, which is an integer indicating the modifier key.

  你也可以加第五個參數,它是一個用來修飾key的整數。

0: No modifier key is pressed

0x02000000: A Shift key on the keyboard is pressed

0x04000000: A Ctrl key on the keyboard is pressed

0x08000000: An Alt key on the keyboard is pressed

0x10000000: A Meta key on the keyboard is pressed

0x20000000: A keypad button is pressed
           

  Third and fourth argument are not taken account for keyboard events. Just give null for them.

  第三個和第四個參數不對鍵盤事件起作用,給他們設定為null好了。

var webPage = require(\'webpage\');
var page = webPage.create();
page.sendEvent(\'keypress\', page.event.key.A, null, null, 0x02000000 | 0x08000000);
           
  • setContent

  Allows to set both page.content and page.url properties.

  The webpage will be reloaded with the new content and the current location set as the given url, without any actual http request being made.

  允許同時設定page.Content和page.url的屬性。網頁會重新加載一個新的内容,并且目前url被設定為被給予的URL。

  • Stop

    switchToFocusedFrame

    switchToFrame(frameName) or switchToFrame(framePosition)

    switchToMainFrame

    switchToParentFrame

    uploadFile(selector, filename)

    Uploads the specified file (filename) to the form element associated with the selector.

  将指定的檔案(檔案名)上傳給選擇器選擇的form元素。

  This function is used to automate the upload of a file, which is usually handled with a file dialog in a traditional browser. Since there is no dialog in this headless mode, such an upload mechanism is handled via this special function instead.

  這個函數用來執行一個檔案上傳。這通常通過傳統浏覽器裡的一個檔案對話框處理。既然這個浏覽器沒有界面,這樣一個上傳機制就由一個函數所代替。

var webPage = require(\'webpage\');
var page = webPage.create();
page.uploadFile(\'input[name=image]\', \'/path/to/some/photo.jpg\');
           
  • Callback Triggers

These functions call callbacks, used for tests…

closing(page)

initialized()

javaScriptAlertSent(message)

javaScriptConsoleMessageSent(message)

loadFinished(status)

loadStarted()

navigationRequested(url, navigationType, navigationLocked, isMainFrame)

rawPageCreated(page)

resourceError(resource)

resourceReceived(request)

resourceRequested(resource)

urlChanged(url)

  • onAlert

  This callback is invoked when there is a JavaScript alert on the web page. The only argument passed to the callback is the string for the message. There is no return value expected from the callback handler.

  當web page有javascript 的alert()出現時調用此回調。唯一的傳遞給回調的參數是消息的字元串。這個回調函數沒有任何傳回。

var webPage = require(\'webpage\');
var page = webPage.create();
page.onAlert = function(msg) {
  console.log(\'ALERT: \' + msg);
};
           
  • onClosing

  This callback is invoked when the WebPage object is being closed, either via page.close in the PhantomJS outer space or via window.close in the page’s client-side.

  It is not invoked when child/descendant pages are being closed unless you also hook them up individually. It takes one argument, closingPage, which is a reference to the page that is closing. Once the onClosing handler has finished executing (returned), the WebPage object closingPage will become invalid.

  當網頁對象被關閉時調用這個回調函數。phantomJs的環境中通過page.close和在頁面中通過window.close關閉頁面時觸發。

  當子頁面關閉時并不調用此函數,當你單獨打開他們時調用此函數。Closingpage參數指向正在關閉的頁面。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onClosing = function(closingPage) {
  console.log(\'The page is closing! URL: \' + closingPage.url);
};
           
  • onConfirm

  This callback is invoked when there is a JavaScript confirm on the web page. The only argument passed to the callback is the string for the message. The return value of the callback handler can be either true or false, which are equivalent to pressing the “OK” or “Cancel” buttons presented in a JavaScript confirm, respectively.

  當網頁上有一個javascript 的confirm函數出現時調用此回調。唯一一個被傳送到回調函數的參數是消息的字元串。回調函數的傳回值是true或者false,相當于在javascript環境下按下了一個确認按鈕或者取消按鈕。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onConfirm = function(msg) {
  console.log(\'CONFIRM: \' + msg);
  return true; // true === pressing the “OK” button, false === pressing the “Cancel” button
};
           
  • onConsoleMessage

  This callback is invoked when there is a JavaScript console message on the web page. The callback may accept up to three arguments: the string for the message, the line number, and the source identifier.

  當頁面javascript列印消息時調用此函數。這個回調函數接收三個參數(後兩個已經取消):消息(string),行數(number)(已取消),和元辨別符(已取消)。

  By default, console messages from the web page are not displayed. Using this callback is a typical way to redirect it.

  預設的,網頁列印台上顯示的文字不會展示出來。使用這個回調是一種間接方法。

  Note: line number and source identifier are not used yet, at least in phantomJS <= 1.8.1. You receive undefined values.

var webPage = require(\'webpage\');
var page = webPage.create();

page.onConsoleMessage = function(msg, lineNum, sourceId) {
  console.log(\'CONSOLE: \' + msg + \' (from line #\' + lineNum + \' in "\' + sourceId + \'")\');
};
           
  • onError

  This callback is invoked when there is a JavaScript execution error. It is a good way to catch problems when evaluating a script in the web page context. The arguments passed to the callback are the error message and the stack trace [as an Array].

  當有一個javascript執行錯誤時調用此回調函數。它是當web頁面上下文腳本發生錯誤時候捕獲錯誤的好辦法。傳遞給回掉函數的參數是錯誤消息和堆棧追蹤【數組】

var webPage = require(\'webpage\');
var page = webPage.create();

page.onError = function(msg, trace) {

  var msgStack = [\'ERROR:\' + msg];
  if (trace && trace.length) {
    msgStack.push(\'TRACE:\');
    trace.forEach(function(t) {
      msgStack.push(\' -> \' + t.file + \':\' + t.line + (t.function ? \'(in function "\' + t.function +\'")\' : \'\'));
    });
  }
  console.error(msgStack.join(\'\n\'));
};

           
  • onFilePicker
var webPage = require(\'webpage\');
var page = webPage.create();
var system = require(\'system\');

page.onFilePicker = function(oldFile) {
  if (system.os.name === \'windows\') {
    return \'C:\\Windows\\System32\\drivers\\etc\\hosts\';
  }
  return \'/etc/hosts\';
};
           
  • onInitialized

  This callback is invoked after the web page is created but before a URL is loaded. The callback may be used to change global objects.

  當頁面建立但是url尚未加載時調用此函數。回調也許會被用來改變全局變量。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onInitialized = function() {
  page.evaluate(function() {
    document.addEventListener(\'DOMContentLoaded\', function() {
      console.log(\'DOM content has loaded.\');
    }, false);
  });
};
           
  • onLoadFinished

  This callback is invoked when the page finishes the loading. It may accept a single argument indicating the page\'s status: \'success\' if no network errors occurred, otherwise \'fail\'.

  頁面完成加載時調用此回調函數。回調函數接收一個參數表示網頁的狀态:success表示沒有網絡錯誤發生,不然是fail。

  Also see page.open for an alternate hook for the onLoadFinished callback.

var webPage = require(\'webpage\');
var page = webPage.create();

page.onLoadFinished = function(status) {
  console.log(\'Status: \' + status);
  //Do other things here…
};

page.onLoadFinished = function(status) {
  console.log(\'Status: \' + status);
  //Do other things here…
};
           
  • onLoadStarted

  This callback is invoked when the page starts the loading. There is no argument passed to the callback.

  當頁面開始加載時調用此函數。沒有參數傳遞給回調。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onLoadStarted = function() {
    var currentUrl = page.evaluate(function() {
    return window.location.href;
});

console.log(\'Current page \' + currentUrl + \' will gone…\');
console.log(\'Now loading a new page…\');
};
           
  • onNavigationRequested

  By implementing this callback, you will be notified when a navigation event happens and know if it will be blocked (by page.navigationLocked).

  通過調用這一個回調函數,當導航事件發生時你将會得到通知,并且知道這個是否會被阻塞(通過page.navigationLocked)。

  Arguments

  url : The target URL of this navigation event

  導航事件的目标URL

  type : Possible values include: \'Undefined\', \'LinkClicked\', \'FormSubmitted\', \'BackOrForward\', \'Reload\', \'FormResubmitted\', \'Other\'

  可能值:\'Undefined\', \'LinkClicked\', \'FormSubmitted\', \'BackOrForward\', \'Reload\', \'FormResubmitted\', \'Other\'

  willNavigate : true if navigation will happen, false if it is locked (by page.navigationLocked)

  true當導航發生,被(通過page.navigationLocked)鎖住時是false

  main : true if this event comes from the main frame, false if it comes from an iframe of some other sub-frame.

  當這個事件來自首頁面時為true,否則是false。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onNavigationRequested = function(url, type, willNavigate, main) {
  console.log(\'Trying to navigate to: \' + url);
  console.log(\'Caused by: \' + type);
  console.log(\'Will actually navigate: \' + willNavigate);
  console.log(\'Sent from the page\\'s main frame: \' + main);
};
           
  • onPageCreated

  This callback is invoked when a new child window (but not deeper descendant windows) is created by the page, e.g. using window.open.

  頁面建立新的子視窗的時候調用此回調。例如使用window.open

  In the PhantomJS outer space, this WebPage object will not yet have called its own page.open method yet and thus does not yet know its requested URL (page.url).

  在phantomjs的外部環境 這個webpage對象尚未通路自己的page.open方法,是以它還不知道請求的URL。

  Therefore, the most common purpose for utilizing a page.onPageCreated callback is to decorate the page (e.g. hook up callbacks, etc.).

  是以,page.onpageCreated最常見的用途是裝飾頁面。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onPageCreated = function(newPage) {
  console.log(\'A new child page was created! Its requested URL is not yet available, though.\');
  // Decorate
  newPage.onClosing = function(closingPage) {
    console.log(\'A child page is closing: \' + closingPage.url);
  };
};
           
  • onPrompt

  This callback is invoked when there is a JavaScript prompt on the web page. The arguments passed to the callback are the string for the message (msg) and the default value (defaultVal) for the prompt answer. The return value of the callback handler should be a string.

  當網頁端有javascript 的prompt()時調用此函數。傳給回調函數的參數是msg消息和prompt()回答的預設值。傳回值應該是string格式。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onPrompt = function(msg, defaultVal) {
  if (msg === "What\'s your name?") {
    return \'PhantomJS\';
  }
  return defaultVal;
};
           
  • onResourceError

  This callback is invoked when a web page was unable to load resource. The only argument to the callback is the resourceError metadata object.

  當一個頁面無法加載資源時調用此函數。傳給回掉函數的參數是resourceerror中繼資料對象。

  The resourceError metadata object contains these properties:

  resourceError中繼資料含有下列屬性:

id : the number of the request

url : the resource url

errorCode : the error code

errorString : the error description

var webPage = require(\'webpage\');
var page = webPage.create();

page.onResourceError = function(resourceError) {
  console.log(\'Unable to load resource (#\' + resourceError.id + \'URL:\' + resourceError.url + \')\');
  console.log(\'Error code: \' + resourceError.errorCode + \'. Description: \' + resourceError.errorString);
};
           
  • onResourceReceived

  This callback is invoked when a resource requested by the page is received. The only argument to the callback is the response metadata object.

  當頁面請求的資源收到時調用回調。傳給回調的參數是resourceerror中繼資料對象。

  If the resource is large and sent by the server in multiple chunks, onResourceReceived will be invoked for every chunk received by PhantomJS.

  當資源過大時服務端将會分成多塊傳送資源。onResourceReceived()将會在每一塊資料被PhantomJs接收時調用。

  The response metadata object contains these properties:

id : the number of the requested resource

url : the URL of the requested resource

time : Date object containing the date of the response

headers : list of http headers

bodySize : size of the received content decompressed (entire content or chunk content)

contentType : the content type if specified

redirectURL : if there is a redirection, the redirected URL

stage : "start", "end" (FIXME: other value for intermediate chunk?)

status : http status code. ex: 200

statusText : http status text. ex: OK

var webPage = require(\'webpage\');
var page = webPage.create();

page.onResourceReceived = function(response) {
  console.log(\'Response (#\' + response.id + \', stage "\' + response.stage + \'"): \' + JSON.stringify(response));
};
           
  • onResourceRequested

  This callback is invoked when the page requests a resource. The first argument to the callback is the requestData metadata object. The second argument is the networkRequest object itself.

  頁面請求資源時調用此函數。第一個參數是resourceerror中繼資料對象,第二個參數是networkRequest對象本身。

  The requestData metadata object contains these properties:

id : the number of the requested resource

method : http method

url : the URL of the requested resource

time : Date object containing the date of the request

headers : list of http headers

  The networkRequest object contains these functions:

  abort() : aborts the current network request. Aborting the current network request will invoke onResourceError callback.

  終止目前的網絡請求,将會調用回調函數onResourceError。

  changeUrl(newUrl) : changes the current URL of the network request. By calling networkRequest.changeUrl(newUrl), we can change the request url to the new url. This is an excellent and only way to provide alternative implementation of a remote resource. (see Example-2)

  改變目前網絡請求的URL。這是提供一個遠端資源替代實作的唯一途徑。

  setHeader(key, value)

  Examples

1 Example-1

var webPage = require(\'webpage\');
var page = webPage.create();

page.onResourceRequested = function(requestData, networkRequest) {
  console.log(\'Request (#\' + requestData.id + \'): \' + JSON.stringify(requestData));
};
           

2 Example-2

Provide an alternative implementation of a remote javascript.

var webPage = require(\'webpage\');
var page = webPage.create();

page.onResourceRequested = function(requestData, networkRequest) {

  var match = requestData.url.match(/wordfamily.js/g);
  if (match != null) {
    console.log(\'Request (#\' + requestData.id + \'): \' + JSON.stringify(requestData));
    // newWordFamily.js is an alternative implementation of wordFamily.js
    // and is available in local path
    networkRequest.changeUrl(\'newWordFamily.js\'); 
  }
  
};
           
  • onResourceTimeout

  This callback is invoked when a resource requested by the page timeout according to settings.resourceTimeout. The only argument to the callback is the request metadata object.

  頁面請求的資源逾時調用此函數。傳給回調函數的唯一參數是請求中繼資料對象。

  The request metadata object contains these properties:

id: the number of the requested resource

method: http method

url: the URL of the requested resource

time: Date object containing the date of the request

headers: list of http headers

errorCode: the error code of the error

errorString: text message of the error

var webPage = require(\'webpage\');
var page = webPage.create();

page.onResourceTimeout = function(request) {
    console.log(\'Response (#\' + request.id + \'): \' + JSON.stringify(request));
};
           
  • onUrlChanged

  This callback is invoked when the URL changes, e.g. as it navigates away from the current URL. The only argument to the callback is the new targetUrl string.

  當url變化(比如它被導航離開目前頁面的url時)調用此回調函數。傳給回調函數的唯一參數是新的targetURl字元串。

  To retrieve the old URL, use the onLoadStarted callback.

  使用onLoadStarted回調函數以取回老的url。

var webPage = require(\'webpage\');
var page = webPage.create();

page.onUrlChanged = function(targetUrl) {
  console.log(\'New URL: \' + targetUrl);
};
           

Child Process Module

  The child_process module allows you to invoke subprocesses and communicate with them via stdin / stdout / stderr. This is useful for tasks such as printing, sending mail, or invoking scripts or programs written in another language (not Javascript).

  子程序子產品允許你調用子程序并且與之通過 stdin/stdiout/stderr進行交流。這對于列印或者發送郵件的任務來說很有用。或者調用其它語言編寫的腳本或者程式。

  To start using, you must require a reference to the child_process module:

var process = require("child_process")
var spawn = process.spawn
var execFile = process.execFile
var child = spawn("ls", ["-lF", "/rooot"])

child.stdout.on("data", function (data) {
  console.log("spawnSTDOUT:", JSON.stringify(data))
});

child.stderr.on("data", function (data) {
  console.log("spawnSTDERR:", JSON.stringify(data))
});

child.on("exit", function (code) {
  console.log("spawnEXIT:", code)
});

//child.kill("SIGKILL")

execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout))
  console.log("execFileSTDERR:", JSON.stringify(stderr))
});
           
  • Spawn
var page  = require(\'page\').create();
var spawn = require(\'child_process\').spawn;

page.open(\'http://google.com\', function(status){
  if( status == \'success\' ) {
    page.render(\'/tmp/google-snapshot.jpg\');
    spawn(\'/usr/bin/sensible-browser\', \'file:///tmp/google-snapshot.jpg\');
    phantom.exit();
  }
});
           

File System Module

  A set of API functions is available to access files and directories, modeled after the CommonJS Filesystem proposal.

  一租可以通路檔案和目錄的檔案系統方案的函數,來自CommonJS。

  To start using, you must require a reference to the fs module:

var fs = require(\'fs\');
           

  Stream objects are returned from the fs.open method.

  fs.open方法傳回一個流對象。

Separator

workingDirectory

var fs = require("fs");
console.log(fs.workingDirectory);// return "F:/liu/study/phantomjs/examples"(the directory the file exsit)
           
  • "absolute(string)" (string)

  Gets the absolute path to where the phantomjs is been run from. If you use relative addresses then you can get the actual address of directories above and below the phantomjs program.

  得到phantomjs運作的絕對路徑。如果你使用相對路徑,那麼你可以得到目錄的實際路徑

var fs = require("fs");

// Print the directory from which phantomjs was run:
var cwd = fs.absolute(".");
console.log(cwd);

// The parent directory:
var parent = fs.absolute("../");
console.log(parent);

// Absolute path of a child directory:
var kid = fs.absolute("/below");
console.log(kid);
           
  • changeWorkingDirectory (string) (BOOL)

  This allows you to change the workingDirectory and returns true if the change happened else false

  改變工作目錄,成功傳回true,錯誤傳回false

var fs = require("fs");

console.log(fs.workingDirectory); //prints the location where phantomjs is running
fs.changeWorkingDirectory("C:\\");
console.log(fs.workingDirectory); //prints C:/
           
  • copy(string source, string destination)"

  This will try to copy a file from one path to another.

  這個函數嘗試将一個檔案拷貝到另外一個檔案。

  Parameter 1 is the source file and parameter 2 is the destination path with the file name.

  參數1是源檔案,參數2是帶目标位址的檔案名。

  If the source file can"t be found then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution.

  如果源檔案無法被發現,将會抛出一個錯誤"無法複制檔案到目的地"并且暫停執行。

  If the destination can not be created then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution. It will not overwrite existing files.

  如果目的地檔案無法被建立,将會抛出一個錯誤"無法複制檔案到目的地"并且暫停執行。這個函數無法重寫已經存在的檔案。

var fs = require("fs");
fs.copy("A.txt", "folder/A.txt");
           
  • copyTree (string source, string destination)

  This will try to copy a directory tree from one path to another.

  這個函數将嘗試拷貝一個目錄結構到另外一個地方。

  Parameter 1 is the source folder tree and parameter 2 is the destination path.

  參數1是源檔案樹參數2是目的目錄。

  If the destination folder doesn"t exist, it will be created. Then, every file and folder will be copied in the destination folder. Folders will be recursively copied.

  如果目的檔案夾不存在,其将被建立。每一個檔案和每一個檔案夾都會被拷貝到目的檔案夾。檔案夾會被遞歸地拷貝。

  If one of the files or folders fails to be copied or created, it will throw a "Unable to copy directory tree SOURCE at DESTINATION" and hang execution.

  如果其中一個檔案無法被拷貝或者建立,其将會抛出錯誤"Unable to copy directory tree SOURCE at DESTINATION"并且暫停執行。

var fs = require("fs");

fs.copyTree("sourceFolder/", "destinationFolder/");
phantom.exit();
           
  • exists(string) (BOOL)

  This will return true if the file exists, otherwise it will return false. This follows symlinks to determinate if a file exists.

  傳回檔案是否存在。存在true,否則false。

var fs = require("fs");

var path = "output.txt";
if (!fs.exists(path))
  fs.write("Hello World", path, "w");
phantom.exit();

var fs = require("fs");

var path = "/Full/Path/To/test.txt";
if (fs.exists(path))
  console.log("\'"+path+"\' exists.");
else
  console.log("\'"+path+"\' doesn\\'t exist.");
phantom.exit();

           
  • "isAbsolute(string)" (BOOL)

  This will return true if the file path is absolute, otherwise it will return false if the path is relative.

  如果檔案路徑是絕對路徑傳回true,否則傳回false

var fs = require("fs");

var path = "/Full/Path/To/test.txt";
// isAbsolute(path) returns true if the specified path is an absolute path.
if (fs.isAbsolute(path))
  console.log("\'"+path+"\' is an absolute path.");
else
  console.log("\'"+path+"\' is NOT an absolute path.");

phantom.exit();
           
  • isDirectory(string)" (BOOL)
var fs = require("fs");

var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){
  // Note: If you didn"t end path with a slash, you need to do so here.
    var file = path + list[x];
    if(fs.isDirectory(file)){
        // Do something
    }
}

phantom.exit();
           
  • isExecutable(string)" (BOOL)
var fs = require("fs");

var path = "/Full/Path/To/exec";
if (fs.isExecutable(path))
  console.log("\'"+path+"\' is executable.");
else
  console.log("\'"+path+"\' is NOT executable.");

phantom.exit();
           
  • "isFile(string)" (BOOL)
var fs = require("fs");

var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){
  // Note: If you didn"t end path with a slash, you need to do so here.
    var file = path + list[x];
    if(fs.isFile(file)){
        // Do something
    }
};

phantom.exit();
           
  • "isLink(string)" (BOOL)

  This will return true if the file path is a symlink (or a shortcut on Windows), otherwise it will return false.

  判斷是否是個symlink(或者在windows下是個快捷方式)。

  Be aware that Unix-like systems and Windows systems don"t manage symlinks and shortcuts the same way.

  注意unix類系統和windows系統有不同的方式。

var fs = require("fs");

var path = "/Full/Path/To/file";
if (fs.isLink(path))
  console.log("\'"+path+"\' is a link.");
else
  console.log("\'"+path+"\' is an absolute path");

phantom.exit();
           
  • "isReadable(string)" (BOOL)

  This will return true if the file is readable, otherwise it will return false.

  是否可讀。

var fs = require("fs");

var path = "/Full/Path/To/file";
if (fs.isReadable(path)) {
  console.log("\'"+path+"\' is readable.");
  
  // Open the file
  var otherFile = fs.open(path, {
    mode: "r" //Read mode
  });
  // Do something with the opened file
  
}
else
  console.log("\'"+path+"\' is NOT readable.");

phantom.exit();
           
  • isWritable(string)" (BOOL)

  This will return true if the file is writable, otherwise it will return false.

  是否可寫。

var fs = require("fs");

var path = "/Full/Path/To/file";
if (fs.isWritable(path)) {

  console.log("\'"+path+"\' is writable.");
  var content = "Hello World!";
  fs.write(path, content, "w");
}
else
  console.log("\'"+path+"\' is NOT writable.");

phantom.exit();
           
  • list(string)" (Array)

  List the contents of a directory on the file system as simple array. This will returns an empty array if the directory is unreadable or doesn"t exist.

  列出一個目錄中的所有内容。如果目錄無法通路或者不存在傳回一個空數列。

// EXAMPLE: Show a list of files in a specific directory as a fully qualified path:

var fs = require("fs");

var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){

  // Note: If you didn"t end path with a slash, you need to do so here.
    var file = path + list[x];
    if(fs.isFile(file)){
        // Do something
    }
}

phantom.exit();
           
  • "makeDirectory(string)" (BOOL)

  Creates a directory at the given path.

  創造檔案夾。成功true,失敗false。如果已經存在檔案夾,傳回false

  This will returns true if the creation was successful, otherwise false. If the directory already exists, it will returns false.

var fs = require("fs");

var path = "/Full/Path/To/Test/";
if(fs.makeDirectory(test))
  console.log("\'"+path+"\' was created.");
else
  console.log("\'"+path+"\' is NOT created.");

phantom.exit();
           
  • makeTree(string)" (BOOL)

  Creates all necessary directories to be able to create the directory.

  This will returns true if the creation was successful, otherwise false. If the directory already exists, it will returns true.

var fs = require("fs");

var path = "/Full/Path/To/Test/";
if(fs.makeDirectory(test))
  console.log("\'"+path+"\' was created.");
else
  console.log("\'"+path+"\' is NOT created.");
 
phantom.exit();
           
  • move(string source, string destination)"

  This will try to move a file from one path to another.

  移動檔案。

  Parameter 1 is the source file and parameter 2 is the destination path with the file name.

  參數一為源檔案,參數二位目标檔案位址。

  If the source file can"t be found then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution.

  If the destination can not be created then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution. It will not overwrite existing files.

  If the source file can not be deleted then it will throw a "Unable to delete file SOURCE" and hang execution

var fs = require("fs");

fs.move("A.txt", "folder/A.txt");
           
  • "open(string path, string/Object parameters)" (Stream)

    打開檔案。

  The parameter object can contain :

mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.打開方式

charset : An IANA, case insensitive, charset name.字元集名稱

Stream objects are returned from the fs.open method. Don"t forget to close the stream.

  When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution.

var fs = require("fs");

var file = fs.open("/path/to/file", "r"); //Open Mode. A string made of "r", "w", "a/+", "b" characters.

var otherFile = fs.open("/path/to/otherFile", {
  mode: "r" //(see Open Mode above)
});

var yetAnotherFile = fs.open("/path/to/yetAnotherFile", {
  mode: "w", //(see Open Mode above)
  charset: "US-ASCII" //An IANA, case insensitive, charset name.
});

file.close();
otherFile.close();
yetAnotherFile.close();
           
  • "read(string path, string/Object parameters)" (string)

  The parameter object can contain :

mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.

charset : An IANA, case insensitive, charset name.

  Opens the path and returns the entire contents as a string.

  When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution.

  Note that the behavior of this method is closely related to fs.open.

var fs = require("fs");

var content = fs.read("file.txt");
console.log("read data:", content);
phantom.exit();
           
  • "readLink(string)" (string)

  This will return the absolute path of a file or folder pointed by a symlink (or shortcut on Windows). If the path is not an symlink or shortcut, it will return an empty string.

  傳回一個檔案或者檔案夾的軟連接配接的絕對路徑(在windows下是快捷方式)。如果路徑不是一個軟連結或者快捷方式,将傳回一個空字元串

var fs = require("fs");

var path = "/Full/Path/To/test.txt";
if (fs.isLink(path)) {
  var absolute = fs.readLink(path);
  console.log("The absolute path of " + path + " is " + absolute);
}
else
  console.log("\'"+path+"\' is an absolute path");

phantom.exit();
           
  • remove(string)"

  This will try to delete the specified file.

  删除指定檔案

  When errors occur during a call, it will throw a "Unable to remove file PATH" and hang execution.

var fs = require("fs");
var toDelete = "someFile.txt";
fs.removeDirectory(toDelete);
phantom.exit();
           
  • removeDirectory(string)"

  This will try to delete the specified folder.

  删除檔案夾

  The directory needs to be empty to be removed with this method. To delete an non empty folder, fs.removeTree should be used.

  When errors occur during a call, it will throw a "Unable to remove directory PATH" and hang execution.

var fs = require("fs");

var toDelete = "someFolder";
// Test if the folder is empty before deleting it
if(fs.list(toDelete).length === 0)
    fs.removeDirectory(toDelete);
phantom.exit();
           
  • "removeTree(string)"

  删除樹

  This will try to delete every file and folder in the specified folder and, finally, delete the folder itself.

  When errors occur during a call, it will throw a "Unable to remove directory tree PATH" and hang execution.

var fs = require("fs");

var toDelete = "someFolder";
fs.makeDirectory(toDelete);
fs.touch(toDelete + "/test.txt");

// It will delete the "someFolder" and the "test.txt" in it

fs.removeDirectory(toDelete);
phantom.exit();
           
  • size(string)" (int)

  This will return the specified file size in bytes, if it exists.

  傳回指定檔案的長度

  When errors occur during a call, it will throw a "Unable to read file PATH size" and hang execution.

var fs = require("fs");

var path = "someFolder/";
// Go through every element in the folder
fs.list(path).forEach(function(file) {
    file = path + file;
    // Remove every file that are larger than 100 bytes
    if(fs.isFile(file) && fs.size(file) > 100)
        fs.remove(file);
});

phantom.exit();
           
  • touch(string)"

  This will try to create an empty file at the specified path.

  連接配接一個特殊路徑的空檔案。

  When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution. The call won"t throw an error if the file already exists.

  Note that the behavior of this method is closely related to fs.open and Stream.write.

var fs = require("fs");

var path = "test.txt";
// Creates an empty file
fs.touch(path);
var text = fs.read(path);
// If the test.txt didn"t already exist, this will print an empty string
console.log(text);
phantom.exit();
           
  • "write(string source, string content, string/Object parameters)"

    寫入檔案

  The parameter object can contain :

mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.

charset : An IANA, case insensitive, charset name.

  If the source file can"t be openend then it will throw a "Unable to open file SOURCE" and hang execution.

var fs = require("fs");

var path = "output.txt";
var content = "Hello World!";
fs.write(path, content, "w");
phantom.exit();
           

System Module

  To start using, you must require a reference to the system module:

var system = require(\'system\');
           
  • system.args {String[]}

  Queries and returns a list of the command-line arguments. The first one is always the script name, which is then followed by the subsequent arguments.

  查詢并傳回一個清單的指令行參數。第一個是腳本名稱,其後是後續參數。

  The following example prints all of the command-line arguments:

var system = require(\'system\');

var args = system.args;
if (args.length === 1) {
  console.log(\'Try to pass some arguments when invoking this script!\');
} else {
  args.forEach(function(arg, i) {
    console.log(i + \': \' + arg);
  });
};
           
  • system.env {Object}

  Queries and returns a list of key-value pairs representing the environment variables.

  查詢并傳回一個鍵-值對清單表示環境變量

var system = require(\'system\');

var env = system.env;
Object.keys(env).forEach(function(key) {
  console.log(key + \'=\' + env[key]);
});
           
  • Os

  Read-only. An object providing information about the operating system, including architecture, name, and version.

  隻讀,一個提供有關作業系統的對象。包括系統位數,名稱和版本。

var system = require(\'system\');

var os = system.os;
console.log(os.architecture);  // \'32bit\'
console.log(os.name);  // \'windows\'
console.log(os.version);  // \'7\'
phantom.exit();
           
  • system.pid {Number}

  Introduced: PhantomJS 1.8 Read-only. The PID (Process ID) for the currently executing PhantomJS process.

  1.8引入,隻讀。正在執行的phantomjs程式的 PID(程式ID)

  • system.platform {String}

  Read-only. The name of the platform, for which the value is always \'phantomjs\'.

  隻讀。平台的名稱,總是"phantomjs"

var system = require(\'system\');
console.log(system.platform); // \'phantomjs\'
phantom.exit();
           

Web Server Module

  • Stability: EXPERIMENTAL

  穩定性:試驗性質

  Using an embedded web server module called Mongoose, PhantomJS script can start a web server. This is intended for ease of communication between PhantomJS scripts and the outside world and is not recommended for use as a general production server. There is currently a limit of 10 concurrent requests; any other requests will be queued up.

  通過使用一個名為"mongoose"嵌入的web服務子產品,phantomJs腳本可以開啟一個web伺服器。這個是 phantomjs腳本和外界進行交流的一種方式,但并不推薦用于一般的生産環境。目前有10個并發請求限制,其他任何請求都将排隊。

var webserver = require(\'webserver\');
           
  • port

  The port on which the server listen requests (read only)

  服務端監聽請求的端口号(隻讀。)

Close

Listen

request argument

  The request object passed to the callback function may contain the following properties:

  請求對象傳遞給回調函數可能包含以下屬性:

method: Defines the request method (\'GET\', \'POST\', etc.)

url: The path part and query string part (if any) of the request URL

httpVersion: The actual HTTP version

headers: All of the HTTP headers as key-value pairs

post: The request body (only for \'POST\' and \'PUT\' method requests)

postRaw:

  If the Content-Type header is set to \'application/x-www-form-urlencoded\' (the default for form submissions), the original contents of post will be stored in this extra property (postRaw) and then post will be automatically updated with a URL-decoded version of the data.

  如果Content-Type header設定成"application/x-www-form-urlencoded"(表單預設送出),原先送出的内容将被存在額外的屬性(postRow)中,并且原先的post将會自動被包括URL-decoded版本的資料重寫。

  • response argument

  The response object should be used to create the response using the following properties and functions:

  響應對象應該以以下屬性和函數建立:

headers:

  Stores all HTTP headers as key-value pairs. These must be set BEFORE calling write for the first time.

  以鍵值對存放一切http header。必須在write被第一次使用之前設定。

setHeader(name, value): sets a specific header

header(name): returns the value of the given header

statusCode: Sets the returned HTTP status code.

setEncoding(encoding): Indicate the encoding to use to convert data given to write(). By default data will be converted to UTF-8. Indicate "binary" if data is a binary string.

write(data): Sends a chunk for the response body. Can be called multiple times.

writeHead(statusCode, headers): Sends a response header to the request. The status code is a 3-digit HTTP status code (e.g. 404). The last argument, headers, are the response headers. Optionally one can give a human-readable headers collection as the second argument.

  • close(): Closes the HTTP connection.

  To avoid the client detecting a connection drop, remember to use write() at least once. Sending an empty string (i.e. response.write(")) would be enough if the only aim is, for example, to return an HTTP status code of 200 ("OK").

  為避免客戶檢測到一個連結中斷,記得使用至少一次write()發送一個空字元串。這種情況适用于目标僅僅是傳回 一個http狀态嗎200的情況。

  • closeGracefully(): same as close(), but ensures response headers have been sent first (and do at least a response.write("))

  自動地在關閉前發送一個空字元串。