forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.js
140 lines (119 loc) · 4.99 KB
/
history.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*global window, document, chrome, console, gsStorage */
(function () {
"use strict";
function getFormattedDate(date, includeTime) {
var d = new Date(date),
cur_date = ("0" + d.getDate()).slice(-2),
cur_month = ("0" + (d.getMonth() + 1)).slice(-2),
cur_year = d.getFullYear(),
cur_time = d.toTimeString().match(/^([0-9]{2}:[0-9]{2})/)[0];
if (includeTime) {
return cur_time + " " + cur_date + "-" + cur_month + "-" + cur_year;
} else {
return cur_date + "-" + cur_month + "-" + cur_year;
}
}
function compareDate(a, b) {
if (a.date > b.date) {
return -1;
}
if (a.date < b.date) {
return 1;
}
return 0;
}
function fetchGsHistoryForDate(date) {
var gsHistory = gsStorage.fetchGsHistory(),
curDate = date,
historyMap = {},
historyArray = [],
groupKey,
tabProperties,
i;
for (i = 0; i < gsHistory.length; i++) {
tabProperties = gsHistory[i];
groupKey = getFormattedDate(tabProperties.date, false);
if (curDate === groupKey && !historyMap.hasOwnProperty(tabProperties.url)) {
historyMap[tabProperties.url] = true;
historyArray.push(tabProperties);
}
}
return historyArray;
}
function reloadTabs(date, suspend) {
var curDate = date;
return function () {
var gsHistory = fetchGsHistoryForDate(date),
url,
i;
gsHistory.reverse();
for (i = 0; i < gsHistory.length; i++) {
url = suspend ? gsStorage.generateSuspendedUrl(gsHistory[i].url) : gsHistory[i].url;
chrome.tabs.create({url: url});
}
};
}
window.onload = function () {
var gsHistory = gsStorage.fetchGsHistory(),
historyMap = {},
key,
groupKey,
curGroupKey,
tabProperties,
historyDiv,
historyImg,
historyLink,
historySpan,
groupHeading,
groupLinkSuspend,
groupLinkUnsuspend,
i;
try {
historyDiv = document.getElementById('gsHistory');
gsHistory.sort(compareDate);
for (i = 0; i < gsHistory.length; i++) {
tabProperties = gsHistory[i];
groupKey = getFormattedDate(tabProperties.date, false);
key = groupKey + tabProperties.url;
if (!historyMap.hasOwnProperty(key)) {
//print header for group
if (groupKey !== curGroupKey) {
curGroupKey = groupKey;
groupHeading = document.createElement("h2");
groupHeading.innerHTML = groupKey;
groupLinkSuspend = document.createElement("a");
groupLinkSuspend.className = "groupLink";
groupLinkSuspend.innerHTML = "re-suspend all tabs for this day";
groupLinkSuspend.setAttribute('href', "#");
groupLinkSuspend.onclick = reloadTabs(groupKey, true);
groupHeading.appendChild(groupLinkSuspend);
groupLinkUnsuspend = document.createElement("a");
groupLinkUnsuspend.className = "groupLink";
groupLinkUnsuspend.innerHTML = "reload all tabs for this day";
groupLinkUnsuspend.setAttribute('href', "#");
groupLinkUnsuspend.onclick = reloadTabs(groupKey, false);
groupHeading.appendChild(groupLinkUnsuspend);
historyDiv.appendChild(groupHeading);
}
historyMap[key] = true;
historyImg = document.createElement("img");
historyImg.setAttribute('src', 'chrome://favicon/' + tabProperties.url);
historyImg.setAttribute('height', '16px');
historyImg.setAttribute('width', '16px');
historyDiv.appendChild(historyImg);
historyLink = document.createElement('a');
historyLink.setAttribute('href', tabProperties.url);
historyLink.setAttribute('target', '_blank');
historyLink.innerHTML = tabProperties.title;
historyDiv.appendChild(historyLink);
historySpan = document.createElement("span");
historySpan.innerHTML = getFormattedDate(tabProperties.date, true);
historyDiv.appendChild(historySpan);
historyDiv.appendChild(document.createElement("br"));
}
}
} catch (e) {
console.log("some kind of error just happened");
}
};
}());