Skip to content

Instantly share code, notes, and snippets.

@Opus1no2
Last active April 21, 2016 20:43
Show Gist options
  • Save Opus1no2/ddc2454a2f7fca9e3c62a24fbb202e23 to your computer and use it in GitHub Desktop.
Save Opus1no2/ddc2454a2f7fca9e3c62a24fbb202e23 to your computer and use it in GitHub Desktop.
(function (w) {
var referrer,
currentDomain,
domainPattern = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im;
function isArrayWithLength(array) {
if (Array.isArray(array) && array.length === 2) {
return array[1];
}
}
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0,
v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function main() {
referrerMatch = document.referrer.match(domainPattern);
currentMatch = window.location.href.match(domainPattern);
referrer = isArrayWithLength(referrerMatch);
currentDomain = isArrayWithLength(currentMatch);
if (referrer === undefined || !new RegExp(referrer).test(currentDomain)) {
if (localStorage.getItem('olab-session-token')) {
localStorage.removeItem('olab-session-token');
}
localStorage.setItem('olab-session-token', uuid());
console.log('POST DATA');
}
if (referrer && new RegExp(referrer).test(currentDomain)) {
if (localStorage.getItem('olab-session-token') === undefined) {
localStorage.setItem('olab-session-token', uuid());
}
console.log('POST DATA');
}
}
w.onload = main;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment