PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
$ brew install phantomjs
var page = require('webpage').create();
page.open('https://github.com/', function() {
page.render('github.png');
phantom.exit();
});var page = require('webpage').create();
page.open('https://status.github.com/', function(status) {
if (status !== 'success') {
console.error('fail to open page');
} else {
var msg = page.evaluate(function() {
var msg = document.getElementById('message');
return [msg.className, msg.innerText];
});
console.log('[' + msg[0] + '] ' + msg[1]);
}
phantom.exit();
});[good] All systems operationalここが問題

///<reference path="./typings/tsd.d.ts" />
import url = require('url');
import querystring = require('querystring');
import phantom = require('phantom');
var clientId = "1426039047681363";
var redirectUri = "http://localhost:3000/auth/facebook/callback";
function openOAuthDialog(ph: phantom.PhantomJS, callback: (err: any, token: string) => void): void {
var params = {
redirect_uri: redirectUri,
response_type: 'code',
client_id: clientId
};
openPage(ph, params, (page: phantom.WebPage, targetUrl: string): boolean => {
var location = url.parse(targetUrl);
var redirectLocation = url.parse(redirectUri);
debuglog('location: ' + location.host + ', ' + location.pathname);
(次のページで解説するので省略)
}, callback);
} if (location.host === 'www.facebook.com') {
switch (location.pathname) {
case '/login.php':
page.evaluate((login_email, login_password) => {
var email = <HTMLInputElement>document.getElementById('email');
if (email) {
email.value = login_email;
(<HTMLInputElement>document.getElementById('pass')).value = login_password;
(<HTMLFormElement>document.getElementById('login_form')).submit();
}
}, () => {}, login_email, login_password);
return false;
case '/v2.0/dialog/oauth':
page.evaluate(() => {
(<HTMLFormElement>document.getElementById('platformDialogForm')).submit();
});
return false;
default:
return false;
}
} else if (location.host === redirectLocation.host) {
var qs = querystring.parse(location.query);
openAccessToken(ph, qs.code, callback);
return true;
}
} else if (location.host === redirectLocation.host) {
var qs = querystring.parse(location.query);
openAccessToken(ph, qs.code, callback);
return true;
}
function openAccessToken(ph: phantom.PhantomJS, code: string, callback: (err: any, token: string) => void): void {
debuglog('code=' + code);
var params = {
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'token',
code: code
};
openPage(ph, params, (page: phantom.WebPage, targetUrl: string): boolean => {
var location = url.parse(targetUrl);
var redirectLocation = url.parse(redirectUri);
debuglog('location: ' + location.host + ', ' + location.pathname);
if (location.host === redirectLocation.host) {
var qs = querystring.parse(location.hash);
callback(null, qs['#access_token']);
ph.exit();
return true;
} else {
return false;
}
}, callback);
}