Skip to content

Instantly share code, notes, and snippets.

@tuannh99
Created August 21, 2015 08:44
Show Gist options
  • Save tuannh99/ef247247fda68793efcb to your computer and use it in GitHub Desktop.
Save tuannh99/ef247247fda68793efcb to your computer and use it in GitHub Desktop.
var system = require('system');
// Control the script arguments
if (system.args.length < 2 || system.args.length > 2) {
console.log("Invalid arguments");
phantom.exit();
}
console.log("===== VIDEO URL: " + system.args[1])
var page = new WebPage(),
fs = require("fs"),
CookieJar = "cookiejar_pluralsight.json",
testindex = 0,
loadInProgress = false,
loginUrl = "https://www.pluralsight.com/a/signin",
outterInterval = null,
mp4Urls = [],
DEBUGFILE = "debug.txt",
loggedIn = true,
requestingUrl = null,
historyUrl = "http://www.pluralsight.com/history";
// pretend to be a different browser, helps with some shitty browser-detection scripts
page.settings.userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36";
// Parse cookies, and let phantom save cookies
if (fs.isFile(CookieJar)) {
Array.prototype.forEach.call(JSON.parse(fs.read(CookieJar)), function(x) {
phantom.addCookie(x);
});
}
// Config to write data to cookies when we load new pages
page.onResourceReceived = function(response) {
fs.write(CookieJar, JSON.stringify(phantom.cookies), "w");
};
page.onLoadStarted = function() {
loadInProgress = true;
// Mark requestingUrl to check "redirect" behaviour while logging-in
requestingUrl = page.evaluate(function() {
return window.location.href;
});
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
page.onUrlChanged = function(targetUrl) {
// RULE: If from historyUrl, we got new URL => not logged in
if (requestingUrl === historyUrl) {
loggedIn = false;
}
};
var steps = [
function() {
// This is needed to prevent script auto go to next step
loadInProgress = true;
page.open(historyUrl);
},
function() {
if (!loggedIn) {
// Load Login Page
console.log("Not logged in yet. Gonna attemp to login now");
page.open(loginUrl);
} else {
console.log(">> Cookies is still valid. Logged In!");
}
},
function() {
if (!loggedIn) {
// This is needed to prevent script auto go to next step
loadInProgress = true;
window.setTimeout(function() {
// Enter Credentials
page.evaluate(function() {
var loginForm = $("form.reg")[0] || $("form")[0];
loginForm.elements["Username"].value = "[TYPE-USERNAME-HERE]";
loginForm.elements["Password"].value = "[TYPE-PASSWORD-HERE]";
// document.createElement('form').submit.call(document.getElementById('login-form'));
HTMLFormElement.prototype.submit.call(document.querySelectorAll("form")[0])
});
}, 1500);
}
},
function() {
loadInProgress = true;
window.setTimeout(function() {
// Render screenshot of current page
page.render("plural.png");
loadInProgress = false;
}, 1000);
}
];
outterInterval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
} else if (!loadInProgress && typeof steps[testindex] != "function") {
console.log("Done");
phantom.exit();
}
}, 50);
@Jacobvu84
Copy link

Note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment