forked from alexferreira/seojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seo-server-phantom.js
64 lines (58 loc) · 1.71 KB
/
seo-server-phantom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var system = require('system')
, args = system.args
, os = require('system').os;
if (args.length < 3) {
console.log("Missing arguments.");
phantom.exit();
}
var server = require('webserver').create();
var port = parseInt(args[1]) || 8888;
var urlPrefix = args[2];
var noEscape = args[3] || false;
var parse_qs = function(s) {
var queryString = {};
var a = document.createElement("a");
a.href = s;
a.search.replace(
/([^?=&]+)(=([^&]*))?/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.onInitialized = function() {
page.evaluate(function() {
document.addEventListener('_htmlReady', function() {
window.callPhantom();
}, false);
});
};
page.onCallback = function() {
page_content = noEscape ? page.content.replace(/#!/g, '') : page.content.replace(/#!/g, '/?_escaped_fragment_=');
cb(page_content);
page.close();
};
// page.onConsoleMessage = function(msg) {
// console.log('CONSOLE: ' + msg);
// };
page.open(url);
};
server.listen(port, function (request, response) {
var route, url;
if(noEscape){
url = urlPrefix + '#!' + request.url;
} else {
route = parse_qs(request.url)._escaped_fragment_;
url = urlPrefix + request.url.slice(1, request.url.indexOf('?')) + '#!' + route;
}
renderHtml(url, function(html) {
response.statusCode = 200;
response.write(html);
response.close();
});
});
console.log('Listening on ' + port + ' OS: '+os.name + ' '+ os.version + ' ' + os.architecture);
console.log('Press Ctrl+C to stop.');