Move spiderable phantom script to an asset

This commit is contained in:
David Glasser
2014-07-18 16:45:16 -07:00
parent d075b6dfd3
commit 39fc2ac12c
3 changed files with 27 additions and 20 deletions

View File

@@ -11,6 +11,8 @@ Package.on_use(function (api) {
api.add_files('spiderable.html', 'client');
api.add_files('spiderable.js', 'server');
api.add_files('phantom_script.js', 'server', { isAsset: true });
});
Package.on_test(function (api) {

View File

@@ -0,0 +1,22 @@
// 'url' is assigned to in a statement before this.
var page = require('webpage').create();
page.open(url);
setInterval(function() {
var ready = page.evaluate(function () {
if (typeof Meteor !== 'undefined'
&& typeof(Meteor.status) !== 'undefined'
&& Meteor.status().connected) {
Deps.flush();
return DDP._allSubscriptionsReady();
}
return false;
});
if (ready) {
var out = page.content;
out = out.replace(/<script[^>]+>(.|\n|\r)*?<\/script\s*>/ig, '');
out = out.replace('<meta name="fragment" content="!">', '');
console.log(out);
phantom.exit();
}
}, 100);

View File

@@ -47,6 +47,8 @@ Spiderable._urlForPhantom = function (siteAbsoluteUrl, requestUrl) {
return urlParser.format(parsedAbsoluteUrl);
};
var PHANTOM_SCRIPT = Assets.getText("phantom_script.js");
WebApp.connectHandlers.use(function (req, res, next) {
// _escaped_fragment_ comes from Google's AJAX crawling spec:
// https://developers.google.com/webmasters/ajax-crawling/docs/specification
@@ -68,26 +70,7 @@ WebApp.connectHandlers.use(function (req, res, next) {
// be able to contain newlines, it should be unable to exploit bash as
// well.
var phantomScript = "var url = " + JSON.stringify(url) + ";" +
"var page = require('webpage').create();" +
"page.open(url);" +
"setInterval(function() {" +
" var ready = page.evaluate(function () {" +
" if (typeof Meteor !== 'undefined' " +
" && typeof(Meteor.status) !== 'undefined' " +
" && Meteor.status().connected) {" +
" Deps.flush();" +
" return DDP._allSubscriptionsReady();" +
" }" +
" return false;" +
" });" +
" if (ready) {" +
" var out = page.content;" +
" out = out.replace(/<script[^>]+>(.|\\n|\\r)*?<\\/script\\s*>/ig, '');" +
" out = out.replace('<meta name=\"fragment\" content=\"!\">', '');" +
" console.log(out);" +
" phantom.exit();" +
" }" +
"}, 100);\n";
PHANTOM_SCRIPT;
// Run phantomjs.
//