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