Created
October 11, 2016 18:38
-
-
Save pioh/d7d58d2ebac5ea6fde8448b756d912a3 to your computer and use it in GitHub Desktop.
jsWebProxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var page = require('webpage').create(), | |
system = require('system'), | |
t, address, timeout, count, regexp, finish; | |
address = system.args[1]; | |
timeout = +(system.args[2] || 10000) | |
count = system.args[3] || 1 | |
regexp = new RegExp(system.args[4] || '.*', 'g') | |
finish = new Date().getTime()+ timeout; | |
page.open(address, function (status) { | |
setInterval(function () { | |
var matches = page.evaluate(function (regexp) { | |
return document.body.innerHTML.match(regexp) | |
}, regexp); | |
var time = new Date().getTime(); | |
if(time > finish || (matches && matches.length >= count)) { | |
console.log(page.evaluate(function () { | |
return document.body.innerHTML; | |
})); | |
phantom.exit(); | |
} | |
}, 200); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let http = require('http') | |
let exec = require('exec'); | |
let serv = http.createServer((req, res) => { | |
let o = {} | |
let m = req.url.match(/(\w+)\=([^\&]+)/g) | |
if (!m) return req.end('bad') | |
m.forEach(a => { | |
a = a.split('=') | |
o[a[0]] = a[1] | |
}) | |
let args = [decodeURIComponent(o.url || 'http://google.ru'), | |
o.time || 10000, | |
o.count || 1, | |
decodeURIComponent(o.regexp || '.*')] | |
exec(['./phantomjs', './ph.js', ...args], function(err, out, code) { | |
if (err) { | |
res.writeHead(500, {'Content-Type': 'text/plain'}); | |
res.end(err); | |
} else { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end(out); | |
} | |
}); | |
}) | |
serv.listen('3000') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment