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