Skip to content

Instantly share code, notes, and snippets.

@shrkw
Last active December 21, 2015 19:09
Show Gist options
  • Save shrkw/6351931 to your computer and use it in GitHub Desktop.
Save shrkw/6351931 to your computer and use it in GitHub Desktop.
1 file screen capturing server depended on phantomjs
var page = require('webpage').create();
var server = require('webserver').create();
var system = require('system');
var host, port;
if (system.args.length !== 2) {
console.log('Usage: server.js <some port>');
phantom.exit(1);
} else {
port = system.args[1];
var service = server.listen(port, function (request, response) {
console.log(JSON.stringify(request, null, 4));
if (request.method != 'GET') {
response.statusCode = 403;
response.headers = {
"Cache": "no-cache",
"Content-Type": "text/html"
};
response.write("This service allow GET request only");
response.close();
return;
}
var validRequestPlease = function (message) {
response.statusCode = 200;
response.headers = {
"Cache": "no-cache",
"Content-Type": "text/html"
};
response.write("Please send valid request: " + message);
response.close();
};
url = request.url;
if (url == '/') {
validRequestPlease("Please point the url you want to capture");
return;
}
target_url = decodeURIComponent(url.slice(1, url.length)).replace(":/", "://");
console.log(target_url);
page.open(target_url, function (status) {
console.log(status);
if (status == "fail") {
validRequestPlease("some error occurred. " + err);
return;
} else {
page.evaluate(function () {
document.body.bgColor = 'white';
});
page.render(target_url.replace(/:|\/|\./g, "_") + '.png');
var b64 = page.renderBase64("PNG");
response.statusCode = 200;
response.headers = {
"Cache": "no-cache",
"Content-Type": "image/png;base64"
};
response.write(b64);
response.close();
}
});
});
if (service) {
console.log('Web server running on port ' + port);
} else {
console.log('Error: Could not create web server listening on port ' + port);
phantom.exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment