Permalink
Browse files

Move everything to background.js

  • Loading branch information...
1 parent 143aeee commit c062a96ed8bb4794a9db7aad10bdc3d9d07bfcad @alagu committed Mar 27, 2012
Showing with 289 additions and 271 deletions.
  1. +8 −271 background.html
  2. +281 −0 background.js
View
@@ -1,274 +1,11 @@
<!DOCTYPE html>
<html>
-<head>
-<script src="http://code.jquery.com/jquery-1.4.2.js"></script>
-<script src="json2.min.js"></script>
-<script src="user.js"></script>
-
-<script type="text/javascript">
-
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-24995053-1']);
- _gaq.push(['_trackPageview']);
-
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = 'https://ssl.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
-
-</script>
-<script>
-
- TIME_ONEHOUR = 3600;
- TIME_INFINITY = 365*86400;
- /* Get stored PNR*/
- function getStoredPNR()
- {
- var pnrnum = localStorage['pnrnum'];
-
- pnrnum = pnrnum ? pnrnum.split(',') : [];
- return pnrnum;
- }
-
-
- function trackEvent(event)
- {
- _gaq.push(['_trackEvent', event, 'event']);
- }
-
- function getJSON(url, callback) {
- var xhr = new XMLHttpRequest();
- xhr.open("GET", url, true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- var resp = xhr.responseText;
- callback(resp);
- }
- }
- xhr.send();
- }
-
- function onRequest(request, sender, callback) {
- if (request.action == 'getJSON') {
- getJSON(request.url, callback);
- }
- else if ( request.action == 'storePNR')
- {
-
- var pnrnum = getStoredPNR();
- var num = request.pnr_num_store + '';
-
- if(num.length == 10 && pnrnum.indexOf(num) == -1)
- {
- pnrnum.push(num);
- }
-
- localStorage['pnrnum'] = pnrnum.join(',');
-
- callback(num);
- }
- else if (request.action == 'getStoredPNR')
- {
- callback(getStoredPNR());
- }
- else if (request.action == 'track')
- {
- _gaq.push(['_trackEvent', request.event, 'event']);
- }
- }
- chrome.extension.onRequest.addListener(onRequest);
-
-
- /* Post install stuff */
- function install_notice() {
- if (localStorage.getItem('install_time'))
- {
- return;
- }
-
- var now = new Date().getTime();
- localStorage.setItem('install_time', now);
- chrome.tabs.create({url: "installed.html"});
- }
- install_notice();
-
-var Background = {};
-
-(function(){
- // If notification is enabled.
- if (window.webkitNotifications) {
- Background.timeInDays = function(days_diff)
- {
- return (days_diff)*86400*1000;
- }
-
- Background.timeInHours = function(hours_diff)
- {
- return (hours_diff)*3600*1000;
- }
-
- Background.timeInMins = function(mins_diff)
- {
- return (mins_diff)*60*1000;
- }
-
- Background.getDateString = function(timestamp) {
- var timeTuple = PNRStatus.getDate(timestamp);
- return timeTuple[0] + " " + timeTuple[1];
- }
-
- Background.showPopup = function(data) {
- var status = '';
- for (var passenger in data['data']['passenger']) {
- var passengerStatus = data['data']['passenger'][passenger]['status'];
- status += passengerStatus + ' ';
- }
-
-
- if (data['data']['from']['time'] != '') {
- status += '. Departs at ' + data['data']['from']['time'];
- }
-
- var notification = window.webkitNotifications.createNotification(
- 'icon.png',
- Background.getDateString(data['data']['travel_date']['timestamp']) + ': ' + data['data']['from']['name'] + ' to ' + data['data']['to']['name'], // The title.
- 'Status - ' + status
- );
-
- notification.show();
-
- setTimeout(function() {
- notification.cancel();
- }, 20000);
- }
-
- /* Stores PNR, and calls refresh again. */
- Background.storeAndMonitorPNR = function(pnr, pnr_data) {
- Background.monitorTicket(pnr);
- Background.storePNR(pnr, pnr_data);
- }
-
- Background.storePNR = function(pnr, pnr_data) {
- console.log("Fetched pnr" + pnr)
- pnr_data['updateTime'] = (new Date()).getTime();
- localStorage.setItem(Background.cacheKey(pnr), JSON.stringify(pnr_data));
- }
-
- Background.cacheKey = function(pnr) {
- return pnr + "-cached"
- }
-
- /* Gets the cached PNR status, or returns null */
- Background.getCachedPNRStatus = function(pnr) {
- if (localStorage.getItem(Background.cacheKey(pnr))) {
- data = $.parseJSON(localStorage.getItem(Background.cacheKey(pnr)));
- return data;
- } else {
- PNRStatus.getPNRStatus(pnr,function(pnr_data){ Background.storeAndMonitorPNR(pnr, $.parseJSON(pnr_data));})
- return null;
- }
- }
-
- /* Updates next popup time for cached data */
- /* What to notify:
- *
- * (Travel date < 7 days) and (Ticket last shown > 1 day)
- * (Travel date < 2 days) and (Ticket last shown > 6 hours)
- * (Travel date < 1 day) and (Ticket last shown > 2 hours)
- * (Travel date < 6 hours) and (Ticket last shown > 30 minutes)
- */
- Background.updateNextPopupTime = function(pnr_data) {
- var currentTimestamp = (new Date()).getTime();
- var travelTimestamp = pnr_data['data']['travel_date']['timestamp'] * 1000;
- var difference = travelTimestamp - currentTimestamp;
- var nextPopupTime = 0;
- if (difference < 0 )
- {
- // Old ticket
- nextPopupTime = -1;
- }
- else if (difference < Background.timeInHours(2))
- {
- nextPopupTime = currentTimestamp + Background.timeInMins(15);
- }
- else if(difference < Background.timeInHours(6))
- {
- nextPopupTime = currentTimestamp + Background.timeInHours(1);
- }
- else if(difference < Background.timeInDays(1))
- {
- nextPopupTime = currentTimestamp + Background.timeInHours(2);
- }
- else if (difference < Background.timeInDays(2)) {
- nextPopupTime = currentTimestamp + Background.timeInHours(6);
- }
- else if (difference < Background.timeInDays(6)) {
- nextPopupTime = currentTimestamp + Background.timeInDays(1);
- }
- else {
- nextPopupTime = currentTimestamp + Background.timeInDays(15);
- }
-
- console.log(nextPopupTime/(60*60*1000));
-
- localStorage.setItem(pnr_data['data']['pnr_number'] + '-next-pp', nextPopupTime);
-
- return 0;
- }
-
- /* Returns the time when next popup should be shown */
- Background.nextPopupTime = function(pnr) {
- var nextpp = localStorage.getItem(pnr + '-next-pp');
- if (nextpp == null) {
- return 0;
- }
- else if (nextpp == "-1") {
- return -1;
- }
- else {
- return Number(nextpp);
- }
- }
-
- Background.monitorTicket = function(pnr) {
- /*
- - Try to fetch data from localStore.
- - If not, populate the data and save it in localStore
- - See travel_date, ticket_status and last_shown
- - If it fits above conditions, then show popup.
- */
- var cached_data = Background.getCachedPNRStatus(pnr);
- if(!cached_data) {
- return;
- }
-
- if(cached_data.hasOwnProperty('data') && cached_data.data.hasOwnProperty('travel_date') ){
- var ticketDate = cached_data.data.travel_date.date;
- var nextPopupTime = Background.nextPopupTime(pnr);
- if ((new Date()).getTime() > nextPopupTime && nextPopupTime != -1) {
- Background.showPopup(cached_data);
- Background.updateNextPopupTime(cached_data);
- }
- }
- }
-
-
- Background.monitorAllTickets = function() {
- PNRStatus.populatePNR();
- for (var i=0; i< PNRStatus.pnrnum.length; i++) {
- Background.monitorTicket(PNRStatus.pnrnum[i]);
- }
- }
-
- setInterval(Background.monitorAllTickets, 60000);
- Background.monitorAllTickets();
- }
-})();
-
-</script>
-
-</head>
-<body>
-</body>
+ <head>
+ <script src="http://code.jquery.com/jquery-1.4.2.js"></script>
+ <script src="json2.min.js"></script>
+ <script src="user.js"></script>
+ <script src="background.js"></script>
+ </head>
+ <body>
+ </body>
</html>
Oops, something went wrong.

0 comments on commit c062a96

Please sign in to comment.