-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
110 lines (94 loc) · 2.87 KB
/
main.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
let ctx = document.getElementById('myChart').getContext("2d");
let myChart = null;
const colors = [
'#003f5c',
'#2f4b7c',
'#665191',
'#a05195',
'#d45087',
'#f95d6a',
'#ff7c43',
'#ffa600',
'#0088FE',
'#00C49F',
]
function domain_from_url(url) {
let result;
let match;
if (match = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n\?\=]+)/im)) {
result = match[1]
if (match = result.match(/^[^\.]+\.(.+\..+)$/)) {
result = match[1]
}
}
return result
}
const drawVisuals = (duration, chart_type) => {
let tracker = {};
chrome.history.search( {text: '', startTime: Date.now() - (86400000 * duration), maxResults: 10000}, (data) => {
console.log(data);
data.forEach( (element) => {
let domain_name = domain_from_url(element.url);
if (tracker[domain_name] !== undefined) {
tracker[domain_name] += 1;
}
else {
tracker[domain_name] = 0;
}
})
// Need to find out top 10 visits
let sorted_tracker = [];
for (let track in tracker) {
sorted_tracker.push([track, tracker[track]]);
}
sorted_tracker.sort( (item_a, item_b) => (item_b[1] - item_a[1]));
let label = [];
let values = [];
for (let i = 0; i < 10 && i < sorted_tracker.length; i++) {
label.push(sorted_tracker[i][0]);
values.push(sorted_tracker[i][1]);
}
console.log(label, values);
myChart = new Chart(ctx, {
type: chart_type,
data: {
labels: label,
datasets: [{
data: values,
label: 'Activity',
backgroundColor: colors,
}],
},
options: {
animation: {
animateScale: true,
},
plugins: {
legend: {
display: true,
position: 'left',
fullWidth: true,
align: 'left',
labels: {
pointStyle: "triangle",
usePointStyle: true,
color: "black",
}
}
}
},
})
})
}
const elements = document.querySelectorAll('.toggle');
elements.forEach( (item, index) => {
item.addEventListener('change', (event) => {
if (myChart != null)
myChart.destroy();
const duration = parseInt(document.getElementById('duration').value, 10);
let chart_type = document.getElementById('chart-type').value;
if (!isNaN(duration)){
drawVisuals(duration, chart_type)
}
})
});