mirror of
https://github.com/vimagick/dockerfiles.git
synced 2024-12-23 01:39:27 +02:00
add webkit
This commit is contained in:
parent
32692c6878
commit
fa55cbc786
226
webkit/client.js
Normal file
226
webkit/client.js
Normal file
@ -0,0 +1,226 @@
|
||||
|
||||
var webpage = require('webpage');
|
||||
|
||||
|
||||
function setCookies(page, cookies) {
|
||||
for(var i=0; i<cookies.length; i++) {
|
||||
var item = cookies[i];
|
||||
var ok = page.addCookie(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function setHeaders(page, headers) {
|
||||
for(var i=0; i<headers.length; i++) {
|
||||
var item = headers[i];
|
||||
var k = item.name;
|
||||
var v = item.value;
|
||||
page.customHeaders[k] = v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function injectScripts(page, scripts, arr) {
|
||||
for(var i=0; i<scripts.length; i++) {
|
||||
var item = scripts[i];
|
||||
var ok = page.injectJs(item);
|
||||
arr.push(ok);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function execScripts(page, scripts, arr) {
|
||||
for(var i=0; i<scripts.length; i++) {
|
||||
var code = scripts[i];
|
||||
var out = page.evaluate(function(code) {
|
||||
return eval(code);
|
||||
}, code);
|
||||
arr.push(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function urlopen(req, callback) {
|
||||
|
||||
var page = webpage.create();
|
||||
var startTime = new Date().getTime();
|
||||
var result = {
|
||||
request: req,
|
||||
serviceMetadata: [],
|
||||
injectedScriptOutput: [],
|
||||
execScriptsOutput: {
|
||||
preInjected: [],
|
||||
postInjected: [],
|
||||
},
|
||||
};
|
||||
|
||||
setCookies(page, req.cookies);
|
||||
setHeaders(page, req.customRequestHeaders);
|
||||
|
||||
var isDebug = req.isDebug;
|
||||
|
||||
var delayTimer, timeoutTimer = setTimeout(function() {
|
||||
if(delayTimer !== undefined) {
|
||||
clearTimeout(delayTimer);
|
||||
}
|
||||
result.totalTime = new Date().getTime() - startTime;
|
||||
result.responseCategory = 'TIMEOUT';
|
||||
callback('timeout', result);
|
||||
page.close();
|
||||
}, req.timeout);
|
||||
|
||||
page.settings.navigationLocked = true;
|
||||
page.settings.userAgent = req.userAgent;
|
||||
page.settings.loadImages = req.loadImages;
|
||||
page.settings.resourceTimeout = req.timeout;
|
||||
|
||||
page.viewportSize = req.viewportSize;
|
||||
page.zoomFactor = req.viewportSize.zoomFactor || 1;
|
||||
page.clipRect = req.clipRectangle;
|
||||
page.scrollPosition = req.scrollTo; //FIXME: it doesn't work as expected
|
||||
|
||||
page.onConsoleMessage = function(msg, lineNum, sourceId) {
|
||||
//console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
|
||||
};
|
||||
|
||||
page.onError = function(msg, trace) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onError',
|
||||
timestamp: new Date().getTime(),
|
||||
details: msg,
|
||||
});
|
||||
};
|
||||
|
||||
page.onLoadStarted = function() {
|
||||
};
|
||||
|
||||
page.onLoadFinished = function(status) {
|
||||
if(status === 'success') {
|
||||
delayTimer = setTimeout(getResult, req.delayTime);
|
||||
} else {
|
||||
result.totalTime = new Date().getTime() - startTime;
|
||||
result.responseCategory = 'FAIL';
|
||||
callback('fail', result);
|
||||
page.close();
|
||||
}
|
||||
};
|
||||
|
||||
page.onResourceRequested = function(request, network) {
|
||||
// TODO: allow whitelist
|
||||
for(var i=0; i<req.resourceUrlBlacklist.length; i++) {
|
||||
var black = RegExp(req.resourceUrlBlacklist[i]);
|
||||
if(black.test(request.url)) {
|
||||
network.abort();
|
||||
result.serviceMetadata.push({
|
||||
category: 'onResourceRequested:abort',
|
||||
timestamp: new Date().getTime(),
|
||||
details: request.url,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(isDebug) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onResourceRequested',
|
||||
timestamp: new Date().getTime(),
|
||||
details: request.url,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
page.onResourceReceived = function(response) {
|
||||
if(response.id === 1 && response.stage === 'end') {
|
||||
result.pageResponseMetadata = response;
|
||||
}
|
||||
if(isDebug) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onResourceReceived',
|
||||
timestamp: new Date().getTime(),
|
||||
details: response.url,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
page.onResourceTimeout = function(request) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onResourceTimeout',
|
||||
timestamp: new Date().getTime(),
|
||||
details: request.url,
|
||||
});
|
||||
};
|
||||
|
||||
page.onResourceError = function(resourceError) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onResourceError',
|
||||
timestamp: new Date().getTime(),
|
||||
details: resourceError.url,
|
||||
});
|
||||
};
|
||||
|
||||
page.onUrlChanged = function(targetUrl) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onUrlChanged',
|
||||
timestamp: new Date().getTime(),
|
||||
details: targetUrl,
|
||||
});
|
||||
};
|
||||
|
||||
page.onNavigationRequested = function(url, type, willNavigate, main) {
|
||||
result.serviceMetadata.push({
|
||||
category: 'onNavigationRequested',
|
||||
timestamp: new Date().getTime(),
|
||||
details: url,
|
||||
});
|
||||
};
|
||||
|
||||
function getResult() {
|
||||
clearTimeout(timeoutTimer);
|
||||
page.evaluate(function(result) {
|
||||
window.pjsc_meta = {};
|
||||
window.pjsc_meta.pageResponse = result.pageResponseMetadata;
|
||||
window.pjsc_meta.pageResponse.request = result.request;
|
||||
}, result);
|
||||
execScripts(page, req.execScripts.preInjected, result.execScriptsOutput.preInjected);
|
||||
injectScripts(page, req.injectedScripts, result.injectedScriptOutput);
|
||||
execScripts(page, req.execScripts.postInjected, result.execScriptsOutput.postInjected);
|
||||
var data = null;
|
||||
switch(req.requestType) {
|
||||
case 'jpg':
|
||||
case 'png':
|
||||
case 'gif':
|
||||
//TODO: setup a fileserver
|
||||
data = '/tmp/' + req.requestId;
|
||||
page.render(data, {format: req.requestType});
|
||||
break;
|
||||
case 'script':
|
||||
data = page.evaluate(function() {
|
||||
return window.pjsc_output;
|
||||
});
|
||||
break;
|
||||
case 'plain':
|
||||
data = page.plainText;
|
||||
break;
|
||||
case 'text':
|
||||
default:
|
||||
data = page.content;
|
||||
break;
|
||||
}
|
||||
result.resultUrl = page.url;
|
||||
result.pageContent = data;
|
||||
result.totalTime = new Date().getTime() - startTime;
|
||||
result.responseCategory = 'OK';
|
||||
callback(null, result);
|
||||
page.close();
|
||||
}
|
||||
|
||||
if(req.targetContent) {
|
||||
page.setContent(req.targetContent, req.targetUrl);
|
||||
} else {
|
||||
page.open(req.targetUrl);
|
||||
}
|
||||
|
||||
return req.requestId;
|
||||
}
|
||||
|
||||
module.exports.urlopen = urlopen;
|
||||
|
6
webkit/config.json
Normal file
6
webkit/config.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"diskCacheEnabled": true,
|
||||
"autoLoadImages": true,
|
||||
"offlineStoragePath": "/tmp/phantomjs",
|
||||
"offlineStorageDefaultQuota": 1000000000
|
||||
}
|
1416
webkit/includes/underscore.js
Normal file
1416
webkit/includes/underscore.js
Normal file
File diff suppressed because it is too large
Load Diff
673
webkit/includes/underscore.string.js
Normal file
673
webkit/includes/underscore.string.js
Normal file
@ -0,0 +1,673 @@
|
||||
// Underscore.string
|
||||
// (c) 2010 Esa-Matti Suuronen <esa-matti aet suuronen dot org>
|
||||
// Underscore.string is freely distributable under the terms of the MIT license.
|
||||
// Documentation: https://github.com/epeli/underscore.string
|
||||
// Some code is borrowed from MooTools and Alexandru Marasteanu.
|
||||
// Version '2.4.0'
|
||||
|
||||
!function(root, String){
|
||||
'use strict';
|
||||
|
||||
// Defining helper functions.
|
||||
|
||||
var nativeTrim = String.prototype.trim;
|
||||
var nativeTrimRight = String.prototype.trimRight;
|
||||
var nativeTrimLeft = String.prototype.trimLeft;
|
||||
|
||||
var parseNumber = function(source) { return source * 1 || 0; };
|
||||
|
||||
var strRepeat = function(str, qty){
|
||||
if (qty < 1) return '';
|
||||
var result = '';
|
||||
while (qty > 0) {
|
||||
if (qty & 1) result += str;
|
||||
qty >>= 1, str += str;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
var slice = [].slice;
|
||||
|
||||
var defaultToWhiteSpace = function(characters) {
|
||||
if (characters == null)
|
||||
return '\\s';
|
||||
else if (characters.source)
|
||||
return characters.source;
|
||||
else
|
||||
return '[' + _s.escapeRegExp(characters) + ']';
|
||||
};
|
||||
|
||||
// Helper for toBoolean
|
||||
function boolMatch(s, matchers) {
|
||||
var i, matcher, down = s.toLowerCase();
|
||||
matchers = [].concat(matchers);
|
||||
for (i = 0; i < matchers.length; i += 1) {
|
||||
matcher = matchers[i];
|
||||
if (!matcher) continue;
|
||||
if (matcher.test && matcher.test(s)) return true;
|
||||
if (matcher.toLowerCase() === down) return true;
|
||||
}
|
||||
}
|
||||
|
||||
var escapeChars = {
|
||||
lt: '<',
|
||||
gt: '>',
|
||||
quot: '"',
|
||||
amp: '&',
|
||||
apos: "'"
|
||||
};
|
||||
|
||||
var reversedEscapeChars = {};
|
||||
for(var key in escapeChars) reversedEscapeChars[escapeChars[key]] = key;
|
||||
reversedEscapeChars["'"] = '#39';
|
||||
|
||||
// sprintf() for JavaScript 0.7-beta1
|
||||
// http://www.diveintojavascript.com/projects/javascript-sprintf
|
||||
//
|
||||
// Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
|
||||
// All rights reserved.
|
||||
|
||||
var sprintf = (function() {
|
||||
function get_type(variable) {
|
||||
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
|
||||
}
|
||||
|
||||
var str_repeat = strRepeat;
|
||||
|
||||
var str_format = function() {
|
||||
if (!str_format.cache.hasOwnProperty(arguments[0])) {
|
||||
str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
|
||||
}
|
||||
return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
|
||||
};
|
||||
|
||||
str_format.format = function(parse_tree, argv) {
|
||||
var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
|
||||
for (i = 0; i < tree_length; i++) {
|
||||
node_type = get_type(parse_tree[i]);
|
||||
if (node_type === 'string') {
|
||||
output.push(parse_tree[i]);
|
||||
}
|
||||
else if (node_type === 'array') {
|
||||
match = parse_tree[i]; // convenience purposes only
|
||||
if (match[2]) { // keyword argument
|
||||
arg = argv[cursor];
|
||||
for (k = 0; k < match[2].length; k++) {
|
||||
if (!arg.hasOwnProperty(match[2][k])) {
|
||||
throw new Error(sprintf('[_.sprintf] property "%s" does not exist', match[2][k]));
|
||||
}
|
||||
arg = arg[match[2][k]];
|
||||
}
|
||||
} else if (match[1]) { // positional argument (explicit)
|
||||
arg = argv[match[1]];
|
||||
}
|
||||
else { // positional argument (implicit)
|
||||
arg = argv[cursor++];
|
||||
}
|
||||
|
||||
if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {
|
||||
throw new Error(sprintf('[_.sprintf] expecting number but found %s', get_type(arg)));
|
||||
}
|
||||
switch (match[8]) {
|
||||
case 'b': arg = arg.toString(2); break;
|
||||
case 'c': arg = String.fromCharCode(arg); break;
|
||||
case 'd': arg = parseInt(arg, 10); break;
|
||||
case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
|
||||
case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
|
||||
case 'o': arg = arg.toString(8); break;
|
||||
case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
|
||||
case 'u': arg = Math.abs(arg); break;
|
||||
case 'x': arg = arg.toString(16); break;
|
||||
case 'X': arg = arg.toString(16).toUpperCase(); break;
|
||||
}
|
||||
arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);
|
||||
pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';
|
||||
pad_length = match[6] - String(arg).length;
|
||||
pad = match[6] ? str_repeat(pad_character, pad_length) : '';
|
||||
output.push(match[5] ? arg + pad : pad + arg);
|
||||
}
|
||||
}
|
||||
return output.join('');
|
||||
};
|
||||
|
||||
str_format.cache = {};
|
||||
|
||||
str_format.parse = function(fmt) {
|
||||
var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
|
||||
while (_fmt) {
|
||||
if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {
|
||||
parse_tree.push(match[0]);
|
||||
}
|
||||
else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {
|
||||
parse_tree.push('%');
|
||||
}
|
||||
else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
|
||||
if (match[2]) {
|
||||
arg_names |= 1;
|
||||
var field_list = [], replacement_field = match[2], field_match = [];
|
||||
if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
|
||||
if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
}
|
||||
else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
}
|
||||
else {
|
||||
throw new Error('[_.sprintf] huh?');
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Error('[_.sprintf] huh?');
|
||||
}
|
||||
match[2] = field_list;
|
||||
}
|
||||
else {
|
||||
arg_names |= 2;
|
||||
}
|
||||
if (arg_names === 3) {
|
||||
throw new Error('[_.sprintf] mixing positional and named placeholders is not (yet) supported');
|
||||
}
|
||||
parse_tree.push(match);
|
||||
}
|
||||
else {
|
||||
throw new Error('[_.sprintf] huh?');
|
||||
}
|
||||
_fmt = _fmt.substring(match[0].length);
|
||||
}
|
||||
return parse_tree;
|
||||
};
|
||||
|
||||
return str_format;
|
||||
})();
|
||||
|
||||
|
||||
|
||||
// Defining underscore.string
|
||||
|
||||
var _s = {
|
||||
|
||||
VERSION: '2.4.0',
|
||||
|
||||
isBlank: function(str){
|
||||
if (str == null) str = '';
|
||||
return (/^\s*$/).test(str);
|
||||
},
|
||||
|
||||
stripTags: function(str){
|
||||
if (str == null) return '';
|
||||
return String(str).replace(/<\/?[^>]+>/g, '');
|
||||
},
|
||||
|
||||
capitalize : function(str){
|
||||
str = str == null ? '' : String(str);
|
||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||
},
|
||||
|
||||
chop: function(str, step){
|
||||
if (str == null) return [];
|
||||
str = String(str);
|
||||
step = ~~step;
|
||||
return step > 0 ? str.match(new RegExp('.{1,' + step + '}', 'g')) : [str];
|
||||
},
|
||||
|
||||
clean: function(str){
|
||||
return _s.strip(str).replace(/\s+/g, ' ');
|
||||
},
|
||||
|
||||
count: function(str, substr){
|
||||
if (str == null || substr == null) return 0;
|
||||
|
||||
str = String(str);
|
||||
substr = String(substr);
|
||||
|
||||
var count = 0,
|
||||
pos = 0,
|
||||
length = substr.length;
|
||||
|
||||
while (true) {
|
||||
pos = str.indexOf(substr, pos);
|
||||
if (pos === -1) break;
|
||||
count++;
|
||||
pos += length;
|
||||
}
|
||||
|
||||
return count;
|
||||
},
|
||||
|
||||
chars: function(str) {
|
||||
if (str == null) return [];
|
||||
return String(str).split('');
|
||||
},
|
||||
|
||||
swapCase: function(str) {
|
||||
if (str == null) return '';
|
||||
return String(str).replace(/\S/g, function(c){
|
||||
return c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase();
|
||||
});
|
||||
},
|
||||
|
||||
escapeHTML: function(str) {
|
||||
if (str == null) return '';
|
||||
return String(str).replace(/[&<>"']/g, function(m){ return '&' + reversedEscapeChars[m] + ';'; });
|
||||
},
|
||||
|
||||
unescapeHTML: function(str) {
|
||||
if (str == null) return '';
|
||||
return String(str).replace(/\&([^;]+);/g, function(entity, entityCode){
|
||||
var match;
|
||||
|
||||
if (entityCode in escapeChars) {
|
||||
return escapeChars[entityCode];
|
||||
} else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
|
||||
return String.fromCharCode(parseInt(match[1], 16));
|
||||
} else if (match = entityCode.match(/^#(\d+)$/)) {
|
||||
return String.fromCharCode(~~match[1]);
|
||||
} else {
|
||||
return entity;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
escapeRegExp: function(str){
|
||||
if (str == null) return '';
|
||||
return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
|
||||
},
|
||||
|
||||
splice: function(str, i, howmany, substr){
|
||||
var arr = _s.chars(str);
|
||||
arr.splice(~~i, ~~howmany, substr);
|
||||
return arr.join('');
|
||||
},
|
||||
|
||||
insert: function(str, i, substr){
|
||||
return _s.splice(str, i, 0, substr);
|
||||
},
|
||||
|
||||
include: function(str, needle){
|
||||
if (needle === '') return true;
|
||||
if (str == null) return false;
|
||||
return String(str).indexOf(needle) !== -1;
|
||||
},
|
||||
|
||||
join: function() {
|
||||
var args = slice.call(arguments),
|
||||
separator = args.shift();
|
||||
|
||||
if (separator == null) separator = '';
|
||||
|
||||
return args.join(separator);
|
||||
},
|
||||
|
||||
lines: function(str) {
|
||||
if (str == null) return [];
|
||||
return String(str).split("\n");
|
||||
},
|
||||
|
||||
reverse: function(str){
|
||||
return _s.chars(str).reverse().join('');
|
||||
},
|
||||
|
||||
startsWith: function(str, starts){
|
||||
if (starts === '') return true;
|
||||
if (str == null || starts == null) return false;
|
||||
str = String(str); starts = String(starts);
|
||||
return str.length >= starts.length && str.slice(0, starts.length) === starts;
|
||||
},
|
||||
|
||||
endsWith: function(str, ends){
|
||||
if (ends === '') return true;
|
||||
if (str == null || ends == null) return false;
|
||||
str = String(str); ends = String(ends);
|
||||
return str.length >= ends.length && str.slice(str.length - ends.length) === ends;
|
||||
},
|
||||
|
||||
succ: function(str){
|
||||
if (str == null) return '';
|
||||
str = String(str);
|
||||
return str.slice(0, -1) + String.fromCharCode(str.charCodeAt(str.length-1) + 1);
|
||||
},
|
||||
|
||||
titleize: function(str){
|
||||
if (str == null) return '';
|
||||
str = String(str).toLowerCase();
|
||||
return str.replace(/(?:^|\s|-)\S/g, function(c){ return c.toUpperCase(); });
|
||||
},
|
||||
|
||||
camelize: function(str){
|
||||
return _s.trim(str).replace(/[-_\s]+(.)?/g, function(match, c){ return c ? c.toUpperCase() : ""; });
|
||||
},
|
||||
|
||||
underscored: function(str){
|
||||
return _s.trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
|
||||
},
|
||||
|
||||
dasherize: function(str){
|
||||
return _s.trim(str).replace(/([A-Z])/g, '-$1').replace(/[-_\s]+/g, '-').toLowerCase();
|
||||
},
|
||||
|
||||
classify: function(str){
|
||||
return _s.capitalize(_s.camelize(String(str).replace(/[\W_]/g, ' ')).replace(/\s/g, ''));
|
||||
},
|
||||
|
||||
humanize: function(str){
|
||||
return _s.capitalize(_s.underscored(str).replace(/_id$/,'').replace(/_/g, ' '));
|
||||
},
|
||||
|
||||
trim: function(str, characters){
|
||||
if (str == null) return '';
|
||||
if (!characters && nativeTrim) return nativeTrim.call(str);
|
||||
characters = defaultToWhiteSpace(characters);
|
||||
return String(str).replace(new RegExp('^' + characters + '+|' + characters + '+$', 'g'), '');
|
||||
},
|
||||
|
||||
ltrim: function(str, characters){
|
||||
if (str == null) return '';
|
||||
if (!characters && nativeTrimLeft) return nativeTrimLeft.call(str);
|
||||
characters = defaultToWhiteSpace(characters);
|
||||
return String(str).replace(new RegExp('^' + characters + '+'), '');
|
||||
},
|
||||
|
||||
rtrim: function(str, characters){
|
||||
if (str == null) return '';
|
||||
if (!characters && nativeTrimRight) return nativeTrimRight.call(str);
|
||||
characters = defaultToWhiteSpace(characters);
|
||||
return String(str).replace(new RegExp(characters + '+$'), '');
|
||||
},
|
||||
|
||||
truncate: function(str, length, truncateStr){
|
||||
if (str == null) return '';
|
||||
str = String(str); truncateStr = truncateStr || '...';
|
||||
length = ~~length;
|
||||
return str.length > length ? str.slice(0, length) + truncateStr : str;
|
||||
},
|
||||
|
||||
/**
|
||||
* _s.prune: a more elegant version of truncate
|
||||
* prune extra chars, never leaving a half-chopped word.
|
||||
* @author github.com/rwz
|
||||
*/
|
||||
prune: function(str, length, pruneStr){
|
||||
if (str == null) return '';
|
||||
|
||||
str = String(str); length = ~~length;
|
||||
pruneStr = pruneStr != null ? String(pruneStr) : '...';
|
||||
|
||||
if (str.length <= length) return str;
|
||||
|
||||
var tmpl = function(c){ return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' '; },
|
||||
template = str.slice(0, length+1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'
|
||||
|
||||
if (template.slice(template.length-2).match(/\w\w/))
|
||||
template = template.replace(/\s*\S+$/, '');
|
||||
else
|
||||
template = _s.rtrim(template.slice(0, template.length-1));
|
||||
|
||||
return (template+pruneStr).length > str.length ? str : str.slice(0, template.length)+pruneStr;
|
||||
},
|
||||
|
||||
words: function(str, delimiter) {
|
||||
if (_s.isBlank(str)) return [];
|
||||
return _s.trim(str, delimiter).split(delimiter || /\s+/);
|
||||
},
|
||||
|
||||
pad: function(str, length, padStr, type) {
|
||||
str = str == null ? '' : String(str);
|
||||
length = ~~length;
|
||||
|
||||
var padlen = 0;
|
||||
|
||||
if (!padStr)
|
||||
padStr = ' ';
|
||||
else if (padStr.length > 1)
|
||||
padStr = padStr.charAt(0);
|
||||
|
||||
switch(type) {
|
||||
case 'right':
|
||||
padlen = length - str.length;
|
||||
return str + strRepeat(padStr, padlen);
|
||||
case 'both':
|
||||
padlen = length - str.length;
|
||||
return strRepeat(padStr, Math.ceil(padlen/2)) + str
|
||||
+ strRepeat(padStr, Math.floor(padlen/2));
|
||||
default: // 'left'
|
||||
padlen = length - str.length;
|
||||
return strRepeat(padStr, padlen) + str;
|
||||
}
|
||||
},
|
||||
|
||||
lpad: function(str, length, padStr) {
|
||||
return _s.pad(str, length, padStr);
|
||||
},
|
||||
|
||||
rpad: function(str, length, padStr) {
|
||||
return _s.pad(str, length, padStr, 'right');
|
||||
},
|
||||
|
||||
lrpad: function(str, length, padStr) {
|
||||
return _s.pad(str, length, padStr, 'both');
|
||||
},
|
||||
|
||||
sprintf: sprintf,
|
||||
|
||||
vsprintf: function(fmt, argv){
|
||||
argv.unshift(fmt);
|
||||
return sprintf.apply(null, argv);
|
||||
},
|
||||
|
||||
toNumber: function(str, decimals) {
|
||||
if (!str) return 0;
|
||||
str = _s.trim(str);
|
||||
if (!str.match(/^-?\d+(?:\.\d+)?$/)) return NaN;
|
||||
return parseNumber(parseNumber(str).toFixed(~~decimals));
|
||||
},
|
||||
|
||||
numberFormat : function(number, dec, dsep, tsep) {
|
||||
if (isNaN(number) || number == null) return '';
|
||||
|
||||
number = number.toFixed(~~dec);
|
||||
tsep = typeof tsep == 'string' ? tsep : ',';
|
||||
|
||||
var parts = number.split('.'), fnums = parts[0],
|
||||
decimals = parts[1] ? (dsep || '.') + parts[1] : '';
|
||||
|
||||
return fnums.replace(/(\d)(?=(?:\d{3})+$)/g, '$1' + tsep) + decimals;
|
||||
},
|
||||
|
||||
strRight: function(str, sep){
|
||||
if (str == null) return '';
|
||||
str = String(str); sep = sep != null ? String(sep) : sep;
|
||||
var pos = !sep ? -1 : str.indexOf(sep);
|
||||
return ~pos ? str.slice(pos+sep.length, str.length) : str;
|
||||
},
|
||||
|
||||
strRightBack: function(str, sep){
|
||||
if (str == null) return '';
|
||||
str = String(str); sep = sep != null ? String(sep) : sep;
|
||||
var pos = !sep ? -1 : str.lastIndexOf(sep);
|
||||
return ~pos ? str.slice(pos+sep.length, str.length) : str;
|
||||
},
|
||||
|
||||
strLeft: function(str, sep){
|
||||
if (str == null) return '';
|
||||
str = String(str); sep = sep != null ? String(sep) : sep;
|
||||
var pos = !sep ? -1 : str.indexOf(sep);
|
||||
return ~pos ? str.slice(0, pos) : str;
|
||||
},
|
||||
|
||||
strLeftBack: function(str, sep){
|
||||
if (str == null) return '';
|
||||
str += ''; sep = sep != null ? ''+sep : sep;
|
||||
var pos = str.lastIndexOf(sep);
|
||||
return ~pos ? str.slice(0, pos) : str;
|
||||
},
|
||||
|
||||
toSentence: function(array, separator, lastSeparator, serial) {
|
||||
separator = separator || ', ';
|
||||
lastSeparator = lastSeparator || ' and ';
|
||||
var a = array.slice(), lastMember = a.pop();
|
||||
|
||||
if (array.length > 2 && serial) lastSeparator = _s.rtrim(separator) + lastSeparator;
|
||||
|
||||
return a.length ? a.join(separator) + lastSeparator + lastMember : lastMember;
|
||||
},
|
||||
|
||||
toSentenceSerial: function() {
|
||||
var args = slice.call(arguments);
|
||||
args[3] = true;
|
||||
return _s.toSentence.apply(_s, args);
|
||||
},
|
||||
|
||||
slugify: function(str) {
|
||||
if (str == null) return '';
|
||||
|
||||
var from = "ąàáäâãåæăćęèéëêìíïîłńòóöôõøśșțùúüûñçżź",
|
||||
to = "aaaaaaaaaceeeeeiiiilnoooooosstuuuunczz",
|
||||
regex = new RegExp(defaultToWhiteSpace(from), 'g');
|
||||
|
||||
str = String(str).toLowerCase().replace(regex, function(c){
|
||||
var index = from.indexOf(c);
|
||||
return to.charAt(index) || '-';
|
||||
});
|
||||
|
||||
return _s.dasherize(str.replace(/[^\w\s-]/g, ''));
|
||||
},
|
||||
|
||||
surround: function(str, wrapper) {
|
||||
return [wrapper, str, wrapper].join('');
|
||||
},
|
||||
|
||||
quote: function(str, quoteChar) {
|
||||
return _s.surround(str, quoteChar || '"');
|
||||
},
|
||||
|
||||
unquote: function(str, quoteChar) {
|
||||
quoteChar = quoteChar || '"';
|
||||
if (str[0] === quoteChar && str[str.length-1] === quoteChar)
|
||||
return str.slice(1,str.length-1);
|
||||
else return str;
|
||||
},
|
||||
|
||||
exports: function() {
|
||||
var result = {};
|
||||
|
||||
for (var prop in this) {
|
||||
if (!this.hasOwnProperty(prop) || prop.match(/^(?:include|contains|reverse)$/)) continue;
|
||||
result[prop] = this[prop];
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
repeat: function(str, qty, separator){
|
||||
if (str == null) return '';
|
||||
|
||||
qty = ~~qty;
|
||||
|
||||
// using faster implementation if separator is not needed;
|
||||
if (separator == null) return strRepeat(String(str), qty);
|
||||
|
||||
// this one is about 300x slower in Google Chrome
|
||||
for (var repeat = []; qty > 0; repeat[--qty] = str) {}
|
||||
return repeat.join(separator);
|
||||
},
|
||||
|
||||
naturalCmp: function(str1, str2){
|
||||
if (str1 == str2) return 0;
|
||||
if (!str1) return -1;
|
||||
if (!str2) return 1;
|
||||
|
||||
var cmpRegex = /(\.\d+)|(\d+)|(\D+)/g,
|
||||
tokens1 = String(str1).toLowerCase().match(cmpRegex),
|
||||
tokens2 = String(str2).toLowerCase().match(cmpRegex),
|
||||
count = Math.min(tokens1.length, tokens2.length);
|
||||
|
||||
for(var i = 0; i < count; i++) {
|
||||
var a = tokens1[i], b = tokens2[i];
|
||||
|
||||
if (a !== b){
|
||||
var num1 = parseInt(a, 10);
|
||||
if (!isNaN(num1)){
|
||||
var num2 = parseInt(b, 10);
|
||||
if (!isNaN(num2) && num1 - num2)
|
||||
return num1 - num2;
|
||||
}
|
||||
return a < b ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (tokens1.length === tokens2.length)
|
||||
return tokens1.length - tokens2.length;
|
||||
|
||||
return str1 < str2 ? -1 : 1;
|
||||
},
|
||||
|
||||
levenshtein: function(str1, str2) {
|
||||
if (str1 == null && str2 == null) return 0;
|
||||
if (str1 == null) return String(str2).length;
|
||||
if (str2 == null) return String(str1).length;
|
||||
|
||||
str1 = String(str1); str2 = String(str2);
|
||||
|
||||
var current = [], prev, value;
|
||||
|
||||
for (var i = 0; i <= str2.length; i++)
|
||||
for (var j = 0; j <= str1.length; j++) {
|
||||
if (i && j)
|
||||
if (str1.charAt(j - 1) === str2.charAt(i - 1))
|
||||
value = prev;
|
||||
else
|
||||
value = Math.min(current[j], current[j - 1], prev) + 1;
|
||||
else
|
||||
value = i + j;
|
||||
|
||||
prev = current[j];
|
||||
current[j] = value;
|
||||
}
|
||||
|
||||
return current.pop();
|
||||
},
|
||||
|
||||
toBoolean: function(str, trueValues, falseValues) {
|
||||
if (typeof str === "number") str = "" + str;
|
||||
if (typeof str !== "string") return !!str;
|
||||
str = _s.trim(str);
|
||||
if (boolMatch(str, trueValues || ["true", "1"])) return true;
|
||||
if (boolMatch(str, falseValues || ["false", "0"])) return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Aliases
|
||||
|
||||
_s.strip = _s.trim;
|
||||
_s.lstrip = _s.ltrim;
|
||||
_s.rstrip = _s.rtrim;
|
||||
_s.center = _s.lrpad;
|
||||
_s.rjust = _s.lpad;
|
||||
_s.ljust = _s.rpad;
|
||||
_s.contains = _s.include;
|
||||
_s.q = _s.quote;
|
||||
_s.toBool = _s.toBoolean;
|
||||
|
||||
// Exporting
|
||||
|
||||
// CommonJS module is defined
|
||||
if (typeof exports !== 'undefined') {
|
||||
if (typeof module !== 'undefined' && module.exports)
|
||||
module.exports = _s;
|
||||
|
||||
exports._s = _s;
|
||||
}
|
||||
|
||||
// Register as a named module with AMD.
|
||||
if (typeof define === 'function' && define.amd)
|
||||
define('underscore.string', [], function(){ return _s; });
|
||||
|
||||
|
||||
// Integrate with Underscore.js if defined
|
||||
// or create our own underscore object.
|
||||
root._ = root._ || {};
|
||||
root._.string = root._.str = _s;
|
||||
}(this, String);
|
140
webkit/parser.js
Normal file
140
webkit/parser.js
Normal file
@ -0,0 +1,140 @@
|
||||
|
||||
var utils = require('./utils');
|
||||
|
||||
var setDefaults = function(req) {
|
||||
|
||||
/*
|
||||
- isDebug
|
||||
- requestId
|
||||
- targetUrl (required)
|
||||
- targetContent
|
||||
- requestType
|
||||
- timeout
|
||||
- loadImages
|
||||
- delayTime
|
||||
- userAgent
|
||||
- resourceUrlWhitelist (FIXME)
|
||||
- resourceUrlBlacklist
|
||||
- viewportSize
|
||||
- clipRectangle
|
||||
- scrollTo (FIXME)
|
||||
- injectedScripts
|
||||
- execScripts (FIXME: targetPageLoaded)
|
||||
*/
|
||||
|
||||
if(req.isDebug === undefined) {
|
||||
req.isDebug = false;
|
||||
} else if(typeof req.isDebug !== 'boolean') {
|
||||
throw 'isDebug: type-error';
|
||||
}
|
||||
|
||||
if(req.requestId === undefined) {
|
||||
req.requestId = utils.guid();
|
||||
} else if(typeof req.requestId !== 'string') {
|
||||
throw 'requestId: type-error';
|
||||
}
|
||||
|
||||
if(req.userAgent === undefined) {
|
||||
req.userAgent = 'Mozilla/5.0 PhantomJS/1.9.8';
|
||||
} else if(typeof req.userAgent !== 'string') {
|
||||
throw 'userAgent: type-error';
|
||||
}
|
||||
|
||||
if(req.targetUrl === undefined) {
|
||||
throw 'targetUrl: missing';
|
||||
} else if(typeof req.targetUrl !== 'string') {
|
||||
throw 'targetUrl: type-error';
|
||||
}
|
||||
|
||||
if(req.targetContent === undefined) {
|
||||
} else if(typeof req.targetContent !== 'string') {
|
||||
throw 'targetContent: type-error';
|
||||
}
|
||||
|
||||
if(req.requestType === undefined) {
|
||||
req.requestType = 'text';
|
||||
} else if(typeof req.requestType !== 'string') {
|
||||
throw 'targetType: type-error';
|
||||
}
|
||||
|
||||
if(req.delayTime === undefined) {
|
||||
req.delayTime = 0;
|
||||
} else if(typeof req.delayTime !== 'number') {
|
||||
throw 'delayTime: type-error';
|
||||
} else if(req.delayTime < 0 || req.delayTime > 300000) {
|
||||
throw 'delayTime: out-of-range';
|
||||
}
|
||||
|
||||
if(req.timeout === undefined) {
|
||||
req.timeout = 60000;
|
||||
} else if(typeof req.timeout !== 'number') {
|
||||
throw 'timeout: type-error';
|
||||
} else if(req.timeout < 1000 || req.timeout > 300000) {
|
||||
throw 'timeout: out-of-range';
|
||||
}
|
||||
|
||||
if(req.loadImages === undefined) {
|
||||
req.loadImages = false;
|
||||
} else if(typeof req.loadImages !== 'boolean') {
|
||||
throw 'timeout: type-error';
|
||||
}
|
||||
|
||||
if(req.cookies === undefined) {
|
||||
req.cookies = [];
|
||||
} else if(req.cookies.length === undefined) {
|
||||
throw 'cookies: type-error';
|
||||
}
|
||||
|
||||
if(req.customRequestHeaders === undefined) {
|
||||
req.customRequestHeaders = [];
|
||||
} else if(req.customRequestHeaders.length === undefined) {
|
||||
throw 'customRequestHeaders: type-error';
|
||||
}
|
||||
|
||||
if(req.resourceUrlWhitelist === undefined) {
|
||||
req.resourceUrlWhitelist = [];
|
||||
} else if(req.resourceUrlWhitelist.length === undefined) {
|
||||
throw 'resourceUrlWhitelist: type-error';
|
||||
}
|
||||
|
||||
if(req.resourceUrlBlacklist === undefined) {
|
||||
req.resourceUrlBlacklist = []; // set it to ['.*\.css.*'] to block stylesheet
|
||||
} else if(req.resourceUrlBlacklist.length === undefined) {
|
||||
throw 'resourceUrlBlacklist: type-error';
|
||||
}
|
||||
|
||||
if(req.viewportSize === undefined) {
|
||||
req.viewportSize = {width: 800, height: 600, zoomFactor: 1};
|
||||
} else if(typeof req.viewportSize !== 'object') {
|
||||
throw 'viewportSize: type-error';
|
||||
}
|
||||
|
||||
if(req.clipRectangle === undefined) {
|
||||
req.clipRectangle = {top: 0, left: 0, width: 0, height: 0};
|
||||
} else if(typeof req.clipRectangle !== 'object') {
|
||||
throw 'clipRectangle: type-error';
|
||||
}
|
||||
|
||||
if(req.scrollTo === undefined) {
|
||||
req.scrollTo = {top: 0, left: 0};
|
||||
} else if(typeof req.scrollTo !== 'object') {
|
||||
throw 'scrollTo: type-error';
|
||||
}
|
||||
|
||||
if(req.injectedScripts === undefined) {
|
||||
req.injectedScripts = [];
|
||||
} else if(req.injectedScripts.length === undefined) {
|
||||
throw 'injectedScripts: type-error';
|
||||
}
|
||||
|
||||
if(req.execScripts === undefined) {
|
||||
req.execScripts = {preInjected: [], postInjected: []};
|
||||
} else if(typeof req.execScripts !== 'object') {
|
||||
throw 'execScripts: type-error';
|
||||
}
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
module.exports.setDefaults = setDefaults;
|
||||
|
62
webkit/server.js
Executable file
62
webkit/server.js
Executable file
@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env phantomjs
|
||||
|
||||
var system = require('system');
|
||||
var server = require('webserver').create();
|
||||
var parser = require('./parser');
|
||||
var client = require('./client');
|
||||
var utils = require('./utils');
|
||||
|
||||
|
||||
var port;
|
||||
if(system.args.length > 1) {
|
||||
port = parseInt(system.args[1]);
|
||||
} else {
|
||||
port = 1024;
|
||||
}
|
||||
|
||||
|
||||
var ok = server.listen(port, function(request, response) {
|
||||
var req;
|
||||
if(request.method === 'POST') {
|
||||
try {
|
||||
var raw = request.postRaw || request.post || '{}',
|
||||
req = parser.setDefaults(JSON.parse(raw));
|
||||
utils.info('recv: %s', req.targetUrl);
|
||||
var reqId = client.urlopen(req, function(err, res) {
|
||||
if(err) {
|
||||
send(response, 500, res);
|
||||
} else {
|
||||
send(response, 200, res);
|
||||
}
|
||||
});
|
||||
utils.info('proc: %s (%s)', reqId, req.targetUrl);
|
||||
} catch(ex) {
|
||||
send(response, 400, ex);
|
||||
}
|
||||
} else {
|
||||
send(response, 405);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if(ok) {
|
||||
utils.info('service started (listen %d)', port);
|
||||
} else {
|
||||
utils.error('service failed to start');
|
||||
phantom.exit(1);
|
||||
}
|
||||
|
||||
|
||||
function send(response, code, data){
|
||||
if(code === 200) {
|
||||
utils.info('send: %s (%d)', data.request.requestId, code);
|
||||
} else {
|
||||
utils.warn('send: %s (%d)', data, code);
|
||||
}
|
||||
response.statusCode = code;
|
||||
response.setHeader('X-Powered-By', 'PhantomJS')
|
||||
response.setHeader('Content-Type', 'application/json')
|
||||
response.write(JSON.stringify(data || '{}'));
|
||||
response.close();
|
||||
}
|
||||
|
2
webkit/tests/input.js
Normal file
2
webkit/tests/input.js
Normal file
@ -0,0 +1,2 @@
|
||||
console.log('META:', JSON.stringify(window.pjsc_meta, undefined, 2));
|
||||
window.pjsc_output = window.pjsc_meta.pageResponse.request.requestId;
|
27
webkit/tests/test.js
Executable file
27
webkit/tests/test.js
Executable file
@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env phantomjs
|
||||
|
||||
var client = require('../client');
|
||||
var parser = require('../parser');
|
||||
|
||||
var req = {
|
||||
targetUrl: 'http://www.baidu.com',
|
||||
requestType: 'script',
|
||||
loadImages: true,
|
||||
resourceUrlBlacklist: [],
|
||||
timeout: 60000,
|
||||
delayTime: 1000,
|
||||
injectedScripts: ['./input.js'],
|
||||
execScripts: {preInjected: ['new Date().getTime()', 'Math.sqrt(2)'], postInjected: ['1+2+3']},
|
||||
}
|
||||
|
||||
var id = client.urlopen(parser.setDefaults(req), function(err, res) {
|
||||
if(err) {
|
||||
console.log('========== success =========');
|
||||
} else {
|
||||
console.log('========== fail =========');
|
||||
}
|
||||
console.log(JSON.stringify(res, undefined, 2));
|
||||
});
|
||||
|
||||
console.log(id);
|
||||
|
60
webkit/utils.js
Normal file
60
webkit/utils.js
Normal file
@ -0,0 +1,60 @@
|
||||
|
||||
var _ = require('./includes/underscore');
|
||||
var _s = require("./includes/underscore.string");
|
||||
|
||||
var log = function() {
|
||||
if(arguments.length < 2) {
|
||||
throw 'Error: log(lvl, fmt, ...)';
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var lvl = args.shift();
|
||||
var args = _.map(args, function(x) {
|
||||
if(typeof x === 'object') {
|
||||
return JSON.stringify(x);
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
});
|
||||
var now = new Date().toISOString();
|
||||
var msg = _s.sprintf.apply(null, args);
|
||||
console.log(_s.sprintf('%s [%s] %s', now, lvl, msg));
|
||||
}
|
||||
|
||||
var debug = function() {
|
||||
var args = ['DEBUG'].concat(Array.prototype.slice.call(arguments));
|
||||
log.apply(null, args);
|
||||
}
|
||||
|
||||
var info = function() {
|
||||
var args = ['INFO'].concat(Array.prototype.slice.call(arguments));
|
||||
log.apply(null, args);
|
||||
}
|
||||
|
||||
var warn = function() {
|
||||
var args = ['WARN'].concat(Array.prototype.slice.call(arguments));
|
||||
log.apply(null, args);
|
||||
}
|
||||
|
||||
var error = function() {
|
||||
var args = ['ERROR'].concat(Array.prototype.slice.call(arguments));
|
||||
log.apply(null, args);
|
||||
}
|
||||
|
||||
var guid = (function() {
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||
}
|
||||
return function() {
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
};
|
||||
})();
|
||||
|
||||
module.exports.guid = guid;
|
||||
module.exports.log = log;
|
||||
module.exports.debug = debug;
|
||||
module.exports.info = info;
|
||||
module.exports.warn = warn;
|
||||
module.exports.error = error;
|
||||
module.exports._ = _;
|
||||
module.exports._s = _s;
|
||||
|
Loading…
Reference in New Issue
Block a user