Last active
May 10, 2019 21:49
-
-
Save pietervp/bcf5cc701ad3371cfd30 to your computer and use it in GitHub Desktop.
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 TML UserScript | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description | |
// @author Pieter Van Parys | |
// @match http://tickets.tomorrowland.com/* | |
// @grant none | |
// ==/UserScript== | |
/* jshint -W097 */ | |
'use strict'; | |
$(function(){ | |
// set global vars | |
var refreshInterval = 100; | |
var doUpdate = function(data){ | |
if (typeof(_latest_queue_data) != "undefined"){ | |
_latest_queue_data = data; | |
} | |
if (data["redirect"]) { | |
setToken(""); | |
loadFrontoffice(data["redirect"]); | |
} | |
}; | |
var sendRequest = function(timestamp){ | |
var updateStart = (new Date()).getTime(); | |
var args = { | |
// js months are zero based, add 1 ms make sure server code is not > 17:00:00.000 | |
now: timestamp.getTime() | |
}; | |
var token = getTokenEx(); | |
if (token) { | |
args.token = token; | |
} | |
$.getJSON(refresh_url_json + "?jsoncallback=?", args).success(doUpdate).error(function() { | |
console.log('error'); | |
}); | |
}; | |
// try to do it 'traditionaly' and do a deterministic | |
// refresh every 1/10 second | |
window.setInterval(function() | |
{ | |
sendRequest(new Date()); | |
} | |
, refreshInterval); | |
// send request at exacty 17:00H GMT+1 | |
window.setInterval(function() | |
{ | |
sendRequest(new Date(2016, 1, 6, 17, 0, 0, 1)); | |
}, refreshInterval); | |
}); | |
function getQueryStringObject() { | |
var queryObject = {}; | |
window.location.search.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3) { | |
queryObject[$1] = $3; | |
}); | |
return queryObject; | |
} | |
var getHashParams = function(hash) { | |
hashParams = {}; | |
hash.replace(/([^#&]+)/g, function(match) { | |
var param = match.split(/=(.+)?/); | |
var key = param[0]; | |
var value = param[1]; | |
hashParams[key] = value; | |
}); | |
return hashParams; | |
}; | |
var addPersonalDetailsToQueryObject = function(queryObject) { | |
var hashParams = getHashParams(window.location.hash); | |
queryObject["pld"] = hashParams["pld"]; | |
return queryObject; | |
}; | |
function stripFoParameters(queryObject) { | |
delete queryObject.event_id; | |
delete queryObject.point_of_sale_id; | |
delete queryObject.token; | |
return queryObject; | |
} | |
function convertToQueryString(queryObject) { | |
var queryString = ""; | |
for (var q in queryObject) { | |
queryString += "&" + q + "=" + queryObject[q]; | |
} | |
return queryString; | |
} | |
function setToken(token) { | |
queue_token = token; | |
var expires = new Date(); | |
expires.setMinutes(expires.getMinutes() + 10); | |
document.cookie = "token=" + token + "; expires=" + expires.toUTCString(); | |
if (!getToken()) { | |
document.cookie = "token=" + token; | |
} | |
} | |
function loadFrontoffice(url) { | |
var queryObject = stripFoParameters(getQueryStringObject()); | |
queryObject = addPersonalDetailsToQueryObject(queryObject); | |
var queryString = convertToQueryString(queryObject); | |
url = url + queryString; | |
document.location.href = url; | |
} | |
// copy of original, not sure if we can access original correctly | |
var getTokenEx = function() { | |
var cookies = document.cookie.split(";"); | |
for (var i = 0, l = cookies.length; i < l; i++) { | |
var cookie = cookies[i]; | |
var name = cookie.substr(0, cookie.indexOf("=")); | |
if (name.replace(/ /g, "") == "token") { | |
return cookie.substr(cookie.indexOf("=") + 1); | |
} | |
} | |
return queue_token; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment