Last active
September 15, 2015 11:31
-
-
Save TheUltDev/6302c488d28917b7a634 to your computer and use it in GitHub Desktop.
Automates some annoying limitations of taking Defensive Driving online.
This file contains 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
// ==UserScript== | |
// @name Defensive Driving | |
// @namespace http://course.defensivedriving.com | |
// @version 0.1 | |
// @description automatically wait for too fast time | |
// @author You | |
// @match http://course.defensivedriving.com/* | |
// @grant none | |
// ==/UserScript== | |
// Load delay from disk | |
var delay = parseInt(localStorage['delay']); | |
// Always save videos on the page | |
var history = localStorage['videos']; | |
var dump = history ? JSON.parse(history) : []; | |
var players = document.getElementsByClassName('playerStyle'); | |
// Scrape player contents | |
for (var i = 0; i < players.length; i++) { | |
dump.push(players[i].innerHTML); | |
} | |
// Append the new videos to the list. | |
localStorage['videos'] = JSON.stringify(dump); | |
// Click handler for the load video button | |
function onLoadVideoBtnClick(e) { | |
// Alert user no videos were found | |
if (!dump) { | |
alert("No videos found so far!\n\nSorry :("); | |
console.warn('No videos found!', localStorage['videos'], dump); | |
return false; | |
} | |
// Clear history | |
localStorage.removeItem('videos'); | |
// Create a videos list | |
var videoList = document.createElement('div'); | |
for (var i = 0; i < dump.length; i++) { | |
var data = dump[i]; | |
// Create iframe for video | |
var videoFrame = document.createElement('iframe'); | |
videoFrame.onload = function () { | |
videoFrame.contentWindow.document.write(data); | |
videoFrame.contentWindow.document.close(); | |
}; | |
videoFrame.src = "about:blank"; | |
videoFrame.style.width = "350px"; | |
videoFrame.style.height = "600px"; | |
videoFrame.style.float = "left"; | |
videoFrame.style.border = "0"; | |
videoList.appendChild(videoFrame); | |
} | |
// Append iframe to the DOM | |
document.body.appendChild(videoFrame); | |
} | |
// Course page | |
//if (location.href.indexOf('toofast.html') !== -1) { | |
// Create load video button | |
var loadBtn = document.createElement('button'); | |
// Assign the load logic | |
loadBtn.onclick = onLoadVideoBtnClick; | |
// Make it visible and "pretty"! | |
loadBtn.innerText = 'Load Videos'; | |
loadBtn.style.padding = '10px 30px'; | |
loadBtn.style.fontSize = '6em'; | |
loadBtn.style.backgroundColor = 'rgb(250, 220, 22)'; | |
loadBtn.style.border = '5px solid rgb(128, 128, 128)'; | |
// Append button to the DOM | |
document.body.appendChild(loadBtn); | |
// Speeding Page Triggered: (toofast.html) | |
if (location.href.indexOf('toofast.html') !== -1) { | |
// Clear alerts | |
localStorage.removeItem('alert'); | |
// Get the time needed to wait on previous page | |
var list = document.getElementsByTagName('td'), | |
timeLength = -1, | |
timeSpent = -1; | |
// Search all elements for a full text dump | |
for (var i = 0; i < list.length; i++) { | |
var item = list[i]; | |
// Element matches our text search | |
if (item.textContent.indexOf('minutes') !== -1) { | |
// We can parse it as a number, awesome. | |
var time = parseInt(item.textContent); | |
if (!isNaN(time)) { | |
// Look for the total time first | |
if (timeLength === -1) { | |
timeLength = time; | |
// Now look for our current time | |
} else if (timeSpent === -1) { | |
timeSpent = time; | |
// We're done here. | |
} else { | |
break; | |
} | |
} | |
} | |
}; | |
// The time we need to wait for. | |
var offset = (timeLength - timeSpent) * 60 * 1000; | |
// The time we can wait before we may timeout our session | |
localStorage['delay'] = Math.min(5 * 60 * 1000, offset); | |
// Click on previous after a couple seconds | |
setTimeout(function() { | |
TurnPage('prev'); | |
}, 2000); | |
// Waiting Page: we're waiting on our speeding delay | |
} else if (delay) { | |
// Update title the user | |
document.title = 'Waiting ' + delay / 1000 + 's'; | |
// Clear delay time | |
localStorage.removeItem('delay'); | |
// Alert user on next page | |
localStorage['alert'] = "on"; | |
// Go to next page (toofast page); | |
setTimeout(function() { | |
TurnPage('next'); | |
}, delay); | |
// New page after waiting that's NOT the speeding page | |
} else if (localStorage['alert'] === 'on') { | |
// Send email / text alert | |
// TODO: replace with push notification | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', 'https://dd.webscript.io/alert', true); | |
xhr.send(); | |
// Clear alerts | |
localStorage.removeItem('alert'); | |
// Freeze thread and wait for user. | |
alert('Ready to continue!'); | |
} | |
console.log('USERSCRIPT', delay, localStorage['alert']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment