Beispiel #1
0
page.onConsoleMessage = function (msg) {
  if (msg.indexOf(preErr) === 0) {
    errd = true;
    lastLog = Date.now();
    system.stdout.write(msg.substring(preErr.length));
    return;
  }
  if (msg.indexOf(preOut) === 0) {
    lastLog = Date.now();
    system.stdout.write(msg.substring(preOut.length));
    return;
  }
  if (msg.indexOf('[EXIT ') === 0) {
    if (msg !== '[EXIT 0]') {
      errd = true;
    }
    exit();
    return;
  }
  if (msg === '[TEST_EXIT]') {
    if (!done && Date.now() - lastLog > 100) {
      exit();
    }
  } else {
    lastLog = Date.now();
    console.log(msg);
  }
};
            var interval, checker = (function () {
                var html = page.evaluate(function () {
                    var bodys = document.getElementsByTagName('body');
                    if (bodys.length > 0) {
                        var body = bodys[0];

                        //如果 <body> 的 data-ajax-status 屬性值是 "success" 表示 AJAX(JS) 已完成 (angular controller 須寫回應完成) 
                        if (body.getAttribute('data-ajax-status') === 'success') {
                            return document.getElementsByTagName('html')[0].outerHTML;
                        }
                    }
                });

                if (html) {     //有取得 html 資料
                    clearInterval(interval);
                    system.stdout.write(html);      //輸出 html 內容 stream
                    phantom.exit();
                } else {        //無取得 html 資料,重新執行 setInterval

                    //執行次數遞減1
                    count--;
                    if (count <= 0) {       //如果限執行次數 <= 0
                        clearInterval(interval);
                        system.stdout.write(defaultValue);      //無法取得 html 內容,輸出傳入預設值參數
                        phantom.exit();
                    }
                }
            });
Beispiel #3
0
(function () {
    'use strict';
    var page   = require('webpage').create(),
        system = require('system');

    function evaluate() {
        var p = page.evaluate(function () {
            return document.documentElement.outerHTML;
        });
        console.log(p);
        phantom.exit();
    }

    if (system.args.length < 2) {
        system.stdout.writeLine('PhantomJS: no filename provided.');
        phantom.exit(1);
    }
    if (system.args.length < 3) {
        system.stdout.writeLine('PhantomJS: no timeout provided.');
        phantom.exit(1);
    }

    // Run the page
    page.open(system.args[1], function (status) {
        if (status !== 'success') {
            console.log('PhantomJS: Unable to open: ' + system.args[1]);
            phantom.exit();
        } else {
            setTimeout(evaluate, system.args[2]);
        }
    });
}());
Beispiel #4
0
                setTimeout(function () {

                    var filename = options.output.filepath,
                        msg = [],
                        now = new Date(),
                        y = '' + now.getFullYear(),
                        m = now.getMonth() + 1,
                        d = now.getDate(),
                        h = now.getHours(),
                        i = now.getMinutes(),
                        s = now.getSeconds(),
                        rand = parseInt(Math.random() * 10000000 + 10000000, 10).toString(36).substr(0, 5);

                    m = (m < 10 ? '0' : '') + m;
                    d = (d < 10 ? '0' : '') + d;
                    h = (h < 10 ? '0' : '') + h;
                    i = (i < 10 ? '0' : '') + i;
                    s = (s < 10 ? '0' : '') + s;

                    filename = filename.replace(/YYYYMMDD/g, y + m + d).replace(/HHMMSS/g, h + i + s).replace(/RANDOM/g, rand);

                    page.render(filename, {
                        format: options.output.format,
                        quality: options.output.quality
                    });

                    //console.log('[success] [output:' + fs.workingDirectory + '/' + filename + ']');

                    msg.push(JSON.stringify(retObj.targets));

                    msg.push('Your request completed successfully.');
                    msg.push('- request');
                    msg.push('&nbsp;&nbsp;. url: ' + options.url);
                    msg.push('&nbsp;&nbsp;. selector: ' + options.container);
                    msg.push('&nbsp;&nbsp;. userAgent: ' + options.userAgent);
                    msg.push('&nbsp;&nbsp;. viewport: ' + options.viewportSize.width + 'x' + options.viewportSize.height);
                    msg.push('&nbsp;&nbsp;. format: ' + options.output.format);
                    if (options.output.format == 'jpg') {
                        msg.push('&nbsp;&nbsp;. quality: ' + options.output.quality);
                    }
                    msg.push('- results');
                    msg.push('&nbsp;&nbsp;. output: ' + fs.workingDirectory + '/' + filename);
                    msg.push('&nbsp;&nbsp;. elapsed: ' + (new Date() - that._ts) + 'ms');
                    msg.push('&nbsp;&nbsp;. resultSize: ' + retObj.width + 'x' + retObj.height);


                    system.stdout.writeLine(JSON.stringify([1, 2, 3]));
                    system.stdout.writeLine(JSON.stringify(retObj.targets));

                    //fs.write('aaa.txt', JSON.stringify(retObj));

                    that.quit(0, msg.join("\n"));

                }, 1000);
Beispiel #5
0
page.onConsoleMessage = function (msg) {
  clearTimeout(timeoutTimer); //Stop from timing out
  system.stdout.write(msg);
  system.stdout.write('\n');
  var match = doneRegEx.exec(msg);
  if (match) {
    var rc = (match[1] === '0' && errorCount === 0) ? 0 : 1;
    system.stdout.writeLine('');
    phantom.exit(rc);
  }
};
 function incrementOffset( codeList, offset, maxSize ) {
    var SANITY = maxSize;
    if(DEBUG) stdout.writeLine( "incrementOffset(" + offset + ")" );
    offset = (offset+1) % maxSize;
    if(DEBUG) stdout.writeLine( "    offset=" + offset );
    while( codeList[offset] !== null ) {
       offset = (offset+1) % maxSize;
       if( SANITY-- < 0 ) { console.log("INSANE!!!!"); break; }
    }
    if(DEBUG) stdout.writeLine( "    return(" + offset + ")" );
    return offset;
 };
function doDone(data) {
    try {
        data = JSON.stringify(data);
    } catch (e) {
        return console.log(e);
    }

    system.stdout.write(data);
    system.stdout.flush();

    loop();
}
Beispiel #8
0
page.onConsoleMessage = function (msg) {
    system.stdout.write(msg);
    var match = doneRegEx.exec(msg);
    if (match) {
        rc = match[1]==="0" ? 0 : 1;
        return;
    }
    match = noReallyDoneRegEx.exec(msg);
    if (match) {
        system.stdout.writeLine("");
        phantom.exit(rc);
    }
};
Beispiel #9
0
page.onCallback = function(msg) {
    system.stdout.write(msg);
    var match = doneRegEx.exec(msg);
    if (match) {
        rc = match[1]==="0" ? 0 : 1;
        return;
    }
    match = noReallyDoneRegEx.exec(msg);
    if (match) {
        system.stdout.write('\n');
        phantom.exit(rc);
    }
}
Beispiel #10
0
/*
  method used to read input of the user
 */
function consoleRead(interactive_str) {
  var system = require('system');
  if (interactive_str)
    system.stdout.writeLine(interactive_str);
  var line = system.stdin.readLine();
  return line;
}
Beispiel #11
0
function end(result) {
	var json = JSON.stringify(output,null,4);
	system.stdout.write(json);
	if (filePath)
		fs.write("output.json", json, 'w');
	phantom.exit(result);
}
Beispiel #12
0
 function evaluate() {
     var p = page.evaluate(function () {
         return document.documentElement.outerHTML;
     });
     system.stdout.writeLine(p);
     phantom.exit();
 }
Beispiel #13
0
 window.setInterval(function() {
   var has_loading_more = page.evaluate(function() {
     var elements = window.document.getElementsByTagName('div');
     console.log('elements==' + elements.length);
     for (var i=0;i<elements.length;i++){
       if(elements[i].className=='loading-more'){
         if(elements[i].getAttribute("style")!='display: none; '){
           return true;
         }else{
           return false;
         }
       }
     }
     return false;
   });
   if(has_loading_more) { // Didn't find
     page.evaluate(function() {
     window.document.body.scrollTop = document.body.scrollHeight;
     });
     console.log('set window.document.body.scrollTop = document.body.scrollHeight');
   } else { // Found
     var pc = page.evaluate(function() {
       return document.documentElement.innerHTML;
     });
     system.stdout.writeLine(pc);
     phantom.exit();
   }
 }, 100); // Number of milliseconds to wait between scrolls
Beispiel #14
0
    page.onResourceReceived = function (res) {
		//只截取start封包
		if( res.stage != 'start')
			return;
		//只截取status为200的封包
		if( res.status != '200' )
			return;
		//只截取包含微信数据的封包
		var urlTest = (/^https:\/\/wx2\.qq\.com\/cgi-bin\/mmwebwx-bin\/webwxsync.*$/);
		if( urlTest.test(res.url) == false )
			return;
		//数据解析并打印出来
		var data = JSON.parse(res.data);
		if( data.BaseResponse.Ret != 0 )
			return;	
		for( var i = 0 ; i != data.AddMsgList.length ; ++i ){
			var msg = data.AddMsgList[i];
			if( msg.Content.length > 1024 )
				continue;
			if( msg.MsgType != 1 )
				continue;
			system.stdout.writeLine(
				'msgId: ' + msg.MsgId +
				' FromUserName: '******' ToUserName: '******' Content: ' + msg.Content +
				'<br/>');
		}
    };
page.open("file:///" + system.args[1], function(status) {
    if (status !== "success") {
      console.log("Couldn't load the page");
      phantom.exit(1);
    }
    system.stdout.writeLine("");
});
Beispiel #16
0
function write(error, data) {
  if (error) {
    system.stderr.writeLine(error);
  } else {
    system.stdout.writeLine(JSON.stringify(data));
  }
}
	page.onLoadFinished = function( status ){
		if( status !== "success" ){
			throw new Error( "Page did not load in Phantom" );
		}
		var svg = page.evaluate(function(name, color){
				var document = window.document;
				var els = document.querySelectorAll( "svg :not([fill=none])" );
				var ret = {};

				Array.prototype.forEach.call(els, function(el){
					el.setAttribute( "fill", color );
				});
				els = document.querySelectorAll( "svg [stroke]:not([stroke=none])" );
				Array.prototype.forEach.call(els, function(el){
					el.setAttribute( "stroke", color );
				});


				ret[ name ] = document.querySelector( ".container" ).innerHTML;
				return ret;
			}, name, color);

		for( var i in svg ){
			if( svg.hasOwnProperty( i ) ){
				svg[i] = uncomment.XMLHeader( svg[i] );
			}
		}
		system.stdout.write(encodeURI(JSON.stringify(svg)));
		phantom.exit(0);

	};
Beispiel #18
0
 function log(message) {
     if (system.stdout) {
         system.stdout.write(message);
     } else {
         console.log(message);
     }
 }
Beispiel #19
0
 setTimeout(function() {
   // http://phantomjs.org/api/webpage/method/evaluate.html
   var el = page.evaluate(function f(s) {
     // if selector is invalid, return the entire document
     if (s.value === undefined || s.value == "null") {
       return document.documentElement.outerHTML;
     // default to querySelector (over querySelectorAll)
     } else if (s.all === undefined || s.all == "null" || s.all == "false") {
       return document.querySelector(s.value).outerHTML;
     } else {
       var nodeList = document.querySelectorAll(s.value);
       var html = [];
       for (var i = 0; i < nodeList.length; i++) {
         html.push(nodeList[i].outerHTML);
       }
       return html;
     }
   }, selector);
   // write to file if specified; otherwise to stdout
   if (sys.args[5] === undefined || sys.args[5] == "null") {
     sys.stdout.write(el);
   } else {
     try {
      fs.write(sys.args[5], el, 'w');
     } catch(e) {
       console.log(e);
     }
   }
   phantom.exit(0);
 }, timeout);
Beispiel #20
0
 function log(message) {
     if (isPhantomJs()) {
         console.log(message);
     } else {
         system.stdout.write(message);
     }
 }
Beispiel #21
0
page.onLoadFinished = function() {
    /// 页面载入完成,解析页面并做处理
    /// 只有当前页面的 window.location.href 中包括 url_no_schema,且源码包含关键字的时候,才返回结果 
    
    console.log("页面载入完成: " + Array.prototype.join.call(arguments, ' '));
    
    console.log("开始解析页面");
    var rect = page.evaluate(function() {
        return {
            href: window.location.href,
            innerHTML: document.body.innerHTML
        };
    });
    console.log("页面解析完成");
    
    
    /// 1. 判断 window.location.href 是否包含 url_no_schema
    if (rect.href.indexOf(url_no_schema) < 0) {    
        console.log("当前页面的 URL `" + rect.href + "' 不包含我们需要的网址, 跳过");
        return;
    }
    
    /// 2. 判断 HTML 代码中有没有关键字
    if (rect.innerHTML.indexOf(keyword) < 0) {
        console.log("当前页面的源码中没有关键字 `" + keyword + "',跳过");
        return;
    }
    
    console.log("在页面中发现了关键字 `" + keyword + "',返回当前页面的源码并退出");
    sys.stdout.write(rect.innerHTML);
    
    page.close();
    phantom.exit(0);
};
Beispiel #22
0
page.onCallback = function (message) {
  // write to the result file
  if (message.slice(0, 5) === "FAIL:") {
    fs.write(QUNIT_RESULT, message.slice(6) + "\n", "a"); 
  }
  // forward the message to the standard output
  if (message.slice(0, 6) === "PRINT:") { system.stdout.write(message.slice(7)); }
};
setTimeout(function() {
   system.stderr.writeLine('Timeout for url:' + url);
   var pc = page.evaluate(function() {
     return document.documentElement.innerHTML;
   });
   system.stdout.writeLine(pc);
   phantom.exit();
}, timeout);
Beispiel #24
0
function consoleRead() {
    var system = require('system');

    system.stdout.writeLine('CaptchaCode: ');
    var line = system.stdin.readLine();

    return line;
}
Beispiel #25
0
function svgToPng(svg) {
  var m = svg.match(/viewBox="\S+ \S+ (\S+) (\S+)"/);
  pngPage = webpage.create();
  pngPage.viewportSize = {width: +m[1], height: +m[2]};
  pngPage.setContent(svg, '');
  var b64png = pngPage.renderBase64('PNG');
  system.stdout.write(b64png);
}
 var interval = setInterval(function(){
   var currentTime = new Date().getTime();
   if(currentTime - lastTime > 3000){
     system.stdout.write(page.content);
     clearInterval(interval);
     phantom.exit();
   }
 }, 100);
 function advanceOffset( codeList, offset, delta, maxSize ) {
    var nextOffset = offset;
    for( var d=0; d<delta; ++d ) {
       nextOffset = incrementOffset( codeList, nextOffset, maxSize );
    }
    if(DEBUG) stdout.writeLine( "offset(" + offset + ") + delta(" + delta + ") = " + nextOffset );
    return nextOffset;
 };
Beispiel #28
0
 return function (result, err) {
   if (err) {
     system.stderr.writeLine(id + " " + JSON.stringify(err) + "\n");
   } else {
     system.stdout.writeLine(id + " " + JSON.stringify(result) + "\n");
   }
   setTimeout(captureInput, 0);
 };
Beispiel #29
0
page.open(system.args[1], function (status) {
  if (status !== 'success') {
    console.error('Error: could not load the page ' + system.args[1]);
    phantom.exit(1);
  }

  system.stdout.writeLine('');
});
page.onConsoleMessage = function(msg, lineNum, sourceId) {
    if (msg.slice(0, 6) === "!DATA!") {
        system.stdout.write(msg.slice(6));
        phantom.exit(0);
        return;
    }
    system.stderr.writeLine("CONSOLE: " + msg + " (from line #" + lineNum + " in \"" + sourceId + "\")");
};