(function () {

  var template = chrome.extension.getURL('box.html');
  var currentDate = new Date().toLocaleDateString('en-US');
  var LIKES_LIMIT = 10;

  function getLikesFromStorage () {
    var likes = parseInt(localStorage.getItem(currentDate + '-likes'));
    return isNaN(likes) ? 0 : likes;
  }

  function saveLikesToStorage (likes) {
    localStorage.setItem(currentDate + '-likes', likes);
  }

  function getTimeSpentFromStorage () {
    var timeSpent = parseInt(localStorage.getItem(currentDate + '-time-spent'));
    return isNaN(timeSpent) ? 0 : timeSpent;
  }

  function saveTimeSpentToStorage (time) {
    localStorage.setItem(currentDate + '-time-spent', time);
  }

  var currentLikes = getLikesFromStorage();
  var currentTimeSpent = getTimeSpentFromStorage();

  $.ajax({
    url: template,
    async: false,
    success: function (html) {
      var countBox = $(html);
      $('body').append(countBox);
      init();
    }
  });

  function timeFormat (totalSeconds) {
    function formatForTen (value) {
      return value < 10 ? '0' + value.toString() : value.toString();
    }
    var hours =  Math.floor(totalSeconds / 3600).toString();
    var totalSeconds = totalSeconds % 3600;
    var minutes = Math.floor(totalSeconds / 60).toString();
    minutes = hours > 0 ? formatForTen(minutes) : minutes;
    var seconds = (totalSeconds % 60);
    seconds = formatForTen(seconds);
    return (hours > 0 ? hours + ':' : '') + minutes + ':' + seconds;
  }

  function init () {
    $('.fbll-count').text(currentLikes);
    $('.fbll-limit').text(LIKES_LIMIT);
    $('.fbll-time-spent').text(timeFormat(currentTimeSpent));

    chrome.runtime.onMessage.addListener(
      function(message, sender, sendResponse) {
        console.log("Message from extension", message)
    });

    $('body').on('click', '.UFILikeLink', function (e) { 
      if (currentLikes < LIKES_LIMIT) {
        currentLikes++;
        $('.fbll-count').text(currentLikes);
        saveLikesToStorage(currentLikes);
      } else {
        alert('Sorry, no more likes for you today!');
        e.preventDefault();
        e.stopPropagation();
      }
    });

    var timer = null;

    function startTimer () {
      stopTimer();
      timer = setInterval(function () {
        currentTimeSpent++;
        $('.fbll-time-spent').text(timeFormat(currentTimeSpent));
      }, 1000);
    }

    function stopTimer () {
      clearInterval(timer);
    }

    $(window).on('focus', function () {
      currentTimeSpent = getTimeSpentFromStorage();
      $('.fbll-time-spent').text(timeFormat(currentTimeSpent));
      startTimer();
    });

    $(window).on('blur beforeunload', function () {
      saveTimeSpentToStorage(currentTimeSpent);
      stopTimer();
    });

    startTimer();
  }  
})();

@@ -154,42 +154,54 @@
 %0A%0A  
-function getLikesFromStorage
+var storage = %7B%0A    getLikes: function
  () %7B%0A
+  
     
@@ -264,32 +264,34 @@
 '-likes'));%0A    
+  
 return isNaN(lik
@@ -307,31 +307,26 @@
  likes;%0A
+  
   %7D
-%0A%0A  function
+,%0A   
  saveLik
@@ -327,25 +327,26 @@
 aveLikes
-ToStorage
+: function
  (likes)
@@ -344,24 +344,26 @@
 n (likes) %7B%0A
+  
     localSto
@@ -409,29 +409,24 @@
 kes);%0A  
-%7D%0A%0A  function
+  %7D,%0A   
  getTime
@@ -434,25 +434,26 @@
 pent
-FromStorage
+: function
  () %7B%0A
+  
     
@@ -529,24 +529,26 @@
 ent'));%0A    
+  
 return isNaN
@@ -578,29 +578,24 @@
 pent;%0A  
-%7D%0A%0A  function
+  %7D,%0A   
  saveTim
@@ -600,25 +600,26 @@
 imeSpent
-ToStorage
+: function
  (time) 
@@ -616,24 +616,26 @@
 on (time) %7B%0A
+  
     localSto
@@ -681,25 +681,32 @@
 ', time);%0A  
-%7D
+  %7D%0A  %7D;
 %0A%0A  var curr
@@ -716,24 +716,32 @@
 Likes = 
+storage.
 getLikes
 FromStor
@@ -732,27 +732,16 @@
 getLikes
-FromStorage
 ();%0A  va
@@ -753,32 +753,40 @@
 rentTimeSpent = 
+storage.
 getTimeSpentFrom
@@ -777,35 +777,24 @@
 getTimeSpent
-FromStorage
 ();%0A%0A  $.aja
@@ -1975,24 +1975,32 @@
        s
+torage.s
 aveLikes
 ToStorag
@@ -1991,25 +1991,16 @@
 aveLikes
-ToStorage
 (current
@@ -2506,16 +2506,24 @@
 Spent = 
+storage.
 getTimeS
@@ -2530,19 +2530,8 @@
 pent
-FromStorage
 ();%0A
@@ -2677,24 +2677,32 @@
  () %7B%0A      
+storage.
 saveTimeSpen
@@ -2706,17 +2706,8 @@
 pent
-ToStorage
 (cur

Refactor into storage module