var timeToSee = '0'; // This is the hour in 24-hour time
var stocks = [ 'GOOG', 'TXN', 'AAPL' ]; // Array of stocks
var onlyOnce = 1; // If this is 1, then it only gives you the stocks the first time you unlock the screen after timeToSee. If it is 0, then it will display the stocks every time you unlock the screen after timeToSee
device.screen.on("unlock", function(){
var screenUnlocked = device.localStorage.getItem('stockScreenUnlocked');
var today = new Date();
if ((!screenUnlocked || screenUnlocked !== today.toLocaleDateString() || onlyOnce != 1) && timeToSee <= today.getHours())
{
var urlS = 'http://finance.google.com/finance/info?client=ig&q=';
urlS += stocks.join();
device.ajax(
{
url: urlS,
type: 'GET',
headers: {
'Content-Type': 'application/json'
}
}, function onSuccess(body, textStatus, response)
{
var notify;
body = body.substring(3);
body = body.replace(/(\r\n|\n|\r)/gm,"");
var bodyJson = JSON.parse(body);
if (!(body) && bodyJson)
{
notify = device.notifications.createNotification('Error');
notify.content = 'Error: ' + body;
notify.show();
}
else
{
for (var x=0; x < bodyJson.length;++x)
{
notify = device.notifications.createNotification('Stock: ' + bodyJson[x].t);
notify.content = 'Current: ' + bodyJson[x].l_cur + ', ' + bodyJson[x].c + ' updated at ' + bodyJson[x].lt;
notify.on('click', makeFunc(x));
notify.show();
}
if (onlyOnce == 1)
device.localStorage.setItem('stockScreenUnlocked', today);
}
},
function onError(textStatus, response)
{
notify = device.notifications.createNotification('Error');
notify.content = 'Error: ' + textStatus;
notify.show();
}
);
}
});
function makeFunc(j)
{
return function()
{
device.browser.launch('http://www.google.com/finance?q='+stocks[j]);
};
}