-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
102 lines (75 loc) · 2.12 KB
/
popup.js
File metadata and controls
102 lines (75 loc) · 2.12 KB
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
function onAnchorClick(event) {
chrome.tabs.create({
selected: true,
url: event.srcElement.href
});
return false;
}
function buildPopupDom(divName, data) {
let popupDiv = document.getElementById(divName);
let ul = document.createElement('ul');
popupDiv.appendChild(ul);
for (let i = 0, ie = data.length; i < ie; ++i) {
let a = document.createElement('a');
a.href = data[i];
a.appendChild(document.createTextNode(data[i]));
a.addEventListener('click', onAnchorClick);
let li = document.createElement('li');
li.appendChild(a);
ul.appendChild(li);
}
}
function buildTypedUrlList(divName) {
let millisecondsPerDay = 1000 * 60 * 60 * 24;
let oneWeekAgo = new Date().getTime() - (millisecondsPerDay * 7);
let numRequestsOutstanding = 0;
chrome.history.search(
{
text: '',
startTime: oneWeekAgo
},
function (historyItems) {
for (let i = 0; i < historyItems.length; ++i) {
let url = historyItems[i].url;
let processVisitsWithUrl = function (url) {
return function (visitItems) {
processVisits(url, visitItems);
};
};
chrome.history.getVisits({ url: url }, processVisitsWithUrl(url));
numRequestsOutstanding++;
}
if (!numRequestsOutstanding) {
onAllVisitsProcessed();
}
}
);
let urlToCount = {};
const processVisits = function (url, visitItems) {
for (let i = 0, ie = visitItems.length; i < ie; ++i) {
if (visitItems[i].transition != 'typed') {
continue;
}
if (!urlToCount[url]) {
urlToCount[url] = 0;
}
urlToCount[url]++;
}
if (!--numRequestsOutstanding) {
onAllVisitsProcessed();
}
};
const onAllVisitsProcessed = () => {
let urlArray = [];
for (let url in urlToCount) {
urlArray.push(url);
}
urlArray.sort(function (a, b) {
return urlToCount[b] - urlToCount[a];
});
buildPopupDom(divName, urlArray.slice(0, 10));
};
}
document.addEventListener('DOMContentLoaded', function () {
buildTypedUrlList('typedUrl_div');
});