Home | History | Annotate | Download | only in showHistory
      1 // Event listner for clicks on links in a browser action popup.
      2 // Open the link in a new tab of the current window.
      3 function onAnchorClick(event) {
      4   chrome.tabs.create({
      5     selected: true,
      6     url: event.srcElement.href
      7   });
      8   return false;
      9 }
     10 
     11 // Given an array of URLs, build a DOM list of those URLs in the
     12 // browser action popup.
     13 function buildPopupDom(divName, data) {
     14   var popupDiv = document.getElementById(divName);
     15 
     16   var ul = document.createElement('ul');
     17   popupDiv.appendChild(ul);
     18 
     19   for (var i = 0, ie = data.length; i < ie; ++i) {
     20     var a = document.createElement('a');
     21     a.href = data[i];
     22     a.appendChild(document.createTextNode(data[i]));
     23     a.addEventListener('click', onAnchorClick);
     24 
     25     var li = document.createElement('li');
     26     li.appendChild(a);
     27 
     28     ul.appendChild(li);
     29   }
     30 }
     31 
     32 // Search history to find up to ten links that a user has typed in,
     33 // and show those links in a popup.
     34 function buildTypedUrlList(divName) {
     35   // To look for history items visited in the last week,
     36   // subtract a week of microseconds from the current time.
     37   var microsecondsPerWeek = 1000 * 60 * 60 * 24 * 7;
     38   var oneWeekAgo = (new Date).getTime() - microsecondsPerWeek;
     39 
     40   // Track the number of callbacks from chrome.history.getVisits()
     41   // that we expect to get.  When it reaches zero, we have all results.
     42   var numRequestsOutstanding = 0;
     43 
     44   chrome.history.search({
     45       'text': '',              // Return every history item....
     46       'startTime': oneWeekAgo  // that was accessed less than one week ago.
     47     },
     48     function(historyItems) {
     49       // For each history item, get details on all visits.
     50       for (var i = 0; i < historyItems.length; ++i) {
     51         var url = historyItems[i].url;
     52         var processVisitsWithUrl = function(url) {
     53           // We need the url of the visited item to process the visit.
     54           // Use a closure to bind the  url into the callback's args.
     55           return function(visitItems) {
     56             processVisits(url, visitItems);
     57           };
     58         };
     59         chrome.history.getVisits({url: url}, processVisitsWithUrl(url));
     60         numRequestsOutstanding++;
     61       }
     62       if (!numRequestsOutstanding) {
     63         onAllVisitsProcessed();
     64       }
     65     });
     66 
     67 
     68   // Maps URLs to a count of the number of times the user typed that URL into
     69   // the omnibox.
     70   var urlToCount = {};
     71 
     72   // Callback for chrome.history.getVisits().  Counts the number of
     73   // times a user visited a URL by typing the address.
     74   var processVisits = function(url, visitItems) {
     75     for (var i = 0, ie = visitItems.length; i < ie; ++i) {
     76       // Ignore items unless the user typed the URL.
     77       if (visitItems[i].transition != 'typed') {
     78         continue;
     79       }
     80 
     81       if (!urlToCount[url]) {
     82         urlToCount[url] = 0;
     83       }
     84 
     85       urlToCount[url]++;
     86     }
     87 
     88     // If this is the final outstanding call to processVisits(),
     89     // then we have the final results.  Use them to build the list
     90     // of URLs to show in the popup.
     91     if (!--numRequestsOutstanding) {
     92       onAllVisitsProcessed();
     93     }
     94   };
     95 
     96   // This function is called when we have the final list of URls to display.
     97   var onAllVisitsProcessed = function() {
     98     // Get the top scorring urls.
     99     urlArray = [];
    100     for (var url in urlToCount) {
    101       urlArray.push(url);
    102     }
    103 
    104     // Sort the URLs by the number of times the user typed them.
    105     urlArray.sort(function(a, b) {
    106       return urlToCount[b] - urlToCount[a];
    107     });
    108 
    109     buildPopupDom(divName, urlArray.slice(0, 10));
    110   };
    111 }
    112 
    113 
    114