'use strict';

var URI = require('URIjs');
var config = require('./config.js');
var PocketClient = require('./pocket_client.js');
var common = require('./common.js');
var client = new PocketClient(config.pocket);
var runClearItemCacheInterval = 1000 * 60 * 60 * 3;

chrome.webNavigation.onCommitted.addListener(function(details) {
  if (details.frameId === 0) {
    var uri = new URI(details.url);
    var found = false;
    var key;

    var data = {
      domain: uri.host(),
      state: 'all',
      detailType: 'complete'
    };
    var success = function(pocketItem) {
      var matchedItem = client.urlMatch(details.url, pocketItem);
      if (matchedItem) {
        common.displaySavedIcon(details.tabId);
        common.itemCache.set(matchedItem);
      } else {
        common.displayUnsavedIcon(details.tabId);
      }



    };
    var error = function() {
      common.displayOfflineIcon(details.tabId);
    };

    if (localStorage.accessToken) {
      client.retrieve(localStorage.accessToken, data, success, error);
    } else {
      error();
    }
  }
});

// TODO:
var clearItemCache = function() {};
// window.setInterval(clearItemCache, runClearItemCacheInterval);

@@ -209,60 +209,8 @@
 et);
-%0Avar runClearItemCacheInterval = 1000 * 60 * 60 * 3;
 %0A%0Ach
@@ -874,16 +874,23 @@
     if (
+window.
 localSto
@@ -931,16 +931,23 @@
 etrieve(
+window.
 localSto
@@ -1034,56 +1034,552 @@
 );%0A%0A
+%0A%0A
 // 
-TODO:%0Avar clearItemCache = function() %7B%7D;%0A// 
+3 hours.%0Avar runClearItemCacheInterval = 1000 * 60 * 60 * 3;%0A%0Avar clearItemCache = function() %7B%0A  var reservedKeys = %7B%0A    'accessToken': true,%0A    'requestToken': true%0A  %7D;%0A%0A  chrome.tabs.query(%7B%7D, function(tabs) %7B%0A    var urls = %7B%7D;%0A    var i;%0A    var k;%0A%0A    for (i = 0; i %3C tabs.length; ++i) %7B%0A      urls%5Btabs%5Bi%5D.url%5D = true;%0A    %7D%0A%0A    for (i = 0; i %3C window.localStorage.length; ++i) %7B%0A      k = window.localStorage.key(i);%0A      if (!urls%5Bk%5D && !reservedKeys%5Bk%5D) %7B%0A        window.localStorage.removeItem(k);%0A      %7D%0A    %7D%0A  %7D);%0A%7D;%0A%0A
 wind
@@ -1633,12 +1633,63 @@
 eInterval);%0A
+window.onload = function() %7B%0A  clearItemCache();%0A%7D%0A

Add a background task: clear item cache