Managing Sessions


Overview

In this example we're going to demonstrate automating a simple workflow where we log in to a real estate portal in order to programmatically detect new entries being added. The script will run at a 30 minute interval searching the 75252 zip code, and will only index entries added after the 'last_search' value.

Demo Real Estate Portal
Username: browserbucketdemo@mailinator.com
Password: Pass123


Browser Script

If you're loading a remote script, be sure to set the Access-Control-Allow-Origin header to *. This is required


var SessionInfo = {};
var BrowserID = -1;

sendGETRequest('GetBrowserInfo', '', loading_step_0);


function loading_step_0(obj)
{
BrowserID = obj.BrowserID;
sendGETRequest('GetData',  ('&BrowserID='+obj.BrowserID), loading_step_1);
}

function loading_step_1(obj)
{
    
SessionInfo = obj;

console.log(JSON.stringify(SessionInfo));

if(!obj.hasOwnProperty("step"))
{
    
var url = 'https://{Your_Server}/instructions.php';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

var obj = JSON.parse(this.responseText);
obj.step = 0;

sendPOSTRequest('SaveData',('&BrowserID='+BrowserID) , JSON.stringify(obj), loading_step_1_1)

}
};
xmlhttp.open('GET', url);
xmlhttp.send();


 //sendPOSTRequest('SaveData', params, post_body, callback_is)
}
 if(obj.step == 0)
 {
  if(window.location.href.indexOf("session-login.php") != -1)
  {
   document.getElementById('username').value = obj.username;
   document.getElementById('inputPassword').value = obj.password;
   document.getElementById('Login').submit();
  }
  else if(window.location.href.indexOf("session-members-page.php") != -1)
  {
   document.getElementById('zip_code').value = obj.zip_code;
   document.getElementById('Login').submit();
  }
  else if(window.location.href.indexOf("session-search-results.php") != -1)
  {
    if(window.jQuery)
    {
        jQueryCode();
    }
    else
    {  
        console.log("loading a fresh jQuery");
        var script = document.createElement('script'); 
        document.head.appendChild(script);  
        script.type = 'text/javascript';
        script.src = "//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js";
        script.onload = jQueryCode;
    }
  }
  else
  {
   window.location.href = 'https://www.browserbucket.com/playground/session-login.php';   
  }
 }

}


function loading_step_1_1(obj)
{
window.location.href = 'https://www.browserbucket.com/playground/session-login.php';
}



jQueryCode = function(){

var newEntryData = new Array();
$('table').find('tr').each(function(index){
    
    if(SessionInfo.last_search < (Date.parse($(this).find('td').eq(4).text())/1000))
    {
        console.log($(this).find('td').eq(4).text());
        var arraylength = newEntryData.length;
        newEntryData[arraylength] = {};
        newEntryData[arraylength]['Address'] = $(this).find('td').eq(0).text();
        newEntryData[arraylength]['Status'] = $(this).find('td').eq(1).text();
        newEntryData[arraylength]['List_Price'] = $(this).find('td').eq(2).text();
        newEntryData[arraylength]['Tax_Assessment'] = $(this).find('td').eq(3).text();
        newEntryData[arraylength]['Date'] = $(this).find('td').eq(4).text();
        console.log( $(this).find('td').eq(0).text() + " - " + $(this).find('td').eq(4).text());
    }
    
});

var url = 'https://browserbucket.com/code-examples/extracted-data.php';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) 
{

sendGETRequest('ReleaseBrowser', ('&BrowserID=' + BrowserID), function(){});

}
};
xmlhttp.open('POST', url);
xmlhttp.send(JSON.stringify(newEntryData));


}




function sendGETRequest(endpoint, params, callback_is)
{

var authdata = "?Token=go7Y5ynGOtCgOkmeBZnxg04ShjMHLLW1&UserID=cbb28f187319784cd6be2aa53b053155";    
var url = 'https://api.browserbucket.com/' + endpoint + '/'+ authdata + params;    
 
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

var obj = JSON.parse(this.responseText);

callback_is(obj);

}
};
xmlhttp.open('GET', url);
xmlhttp.send();
}


function sendPOSTRequest(endpoint, params, post_body, callback_is)
{

var authdata = "?Token=go7Y5ynGOtCgOkmeBZnxg04ShjMHLLW1&UserID=cbb28f187319784cd6be2aa53b053155";    
var url = 'https://api.browserbucket.com/' + endpoint + '/'+ authdata + params;    
    
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

var obj = JSON.parse(this.responseText);

callback_is(obj);

}
};
xmlhttp.open('POST', url);
xmlhttp.send(post_body);
}