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.
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) {
10 url: event.srcElement.href
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);
20 var ul = document.createElement('ul');
21 popupDiv.appendChild(ul);
23 for (var i = 0, ie = data.length; i < ie; ++i) {
24 var a = document.createElement('a');
26 a.appendChild(document.createTextNode(data[i]));
27 a.addEventListener('click', onAnchorClick);
29 var li = document.createElement('li');
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;
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;
48 chrome.history.search({
49 'text': '', // Return every history item....
50 'startTime': oneWeekAgo // that was accessed less than one week ago.
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);
63 chrome.history.getVisits({url: url}, processVisitsWithUrl(url));
64 numRequestsOutstanding++;
66 if (!numRequestsOutstanding) {
67 onAllVisitsProcessed();
72 // Maps URLs to a count of the number of times the user typed that URL into
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') {
85 if (!urlToCount[url]) {
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();
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.
104 for (var url in urlToCount) {
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];
113 buildPopupDom(divName, urlArray.slice(0, 10));
117 document.addEventListener('DOMContentLoaded', function () {
118 buildTypedUrlList("typedUrl_div");