Created
September 23, 2016 10:36
-
-
Save sawantuday/d86102cea18d05cd96e6bd418635c76d to your computer and use it in GitHub Desktop.
PhantomJS script for Angular JS sites.
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
var system = require('system'); | |
if (system.args.length < 3) { | |
console.log("Missing arguments."); | |
phantom.exit(); | |
} | |
var server = require('webserver').create(); | |
var port = parseInt(system.args[1]); | |
var urlPrefix = system.args[2]; | |
var parse_qs = function(s) { | |
var queryString = {}; | |
var a = document.createElement("a"); | |
a.href = s; | |
a.search.replace( | |
new RegExp("([^?=&]+)(=([^&]*))?", "g"), | |
function($0, $1, $2, $3) { queryString[$1] = $3; } | |
); | |
return queryString; | |
}; | |
var renderHtml = function(url, cb) { | |
var page = require('webpage').create(); | |
page.settings.loadImages = false; | |
page.settings.localToRemoteUrlAccessEnabled = true; | |
page.onCallback = function() { | |
cb(page.content); | |
page.close(); | |
}; | |
// page.onConsoleMessage = function(msg, lineNum, sourceId) { | |
// console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); | |
// }; | |
page.onInitialized = function() { | |
page.evaluate(function() { | |
setTimeout(function() { | |
window.callPhantom(); | |
}, 10000); | |
}); | |
}; | |
page.open(url); | |
}; | |
server.listen(port, function (request, response) { | |
var route = parse_qs(request.url)._escaped_fragment_; | |
var url = urlPrefix | |
+ request.url.slice(1, request.url.indexOf('?')) | |
+ '#!' + decodeURIComponent(route); | |
renderHtml(url, function(html) { | |
response.statusCode = 200; | |
response.write(html); | |
response.close(); | |
}); | |
}); | |
console.log('Listening on ' + port + '...'); | |
console.log('Press Ctrl+C to stop.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment