-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
63 lines (54 loc) · 2.01 KB
/
popup.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
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
suffixes = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"];
function printDay(date) {
return `${date.getDate()}${suffixes[date.getDate() % 10]}`;
}
function setAndCopy(textField, value, messageField) {
textField.value=value;
textField.select();
document.execCommand('copy');
messageField.innerHTML = 'copied!';
}
function add(tableElement, offset, date) {
let row = tableElement.insertRow(-1);
let offsetCell = row.insertCell(-1);
if (offset === 0) {
offsetCell.innerHTML = `today`;
} else {
offsetCell.innerHTML = `+${offset}d`;
}
let textField = document.getElementById('nf-copy-field');
let messageField = document.getElementById('nf-message');
let humanFriendlyDate =
`${days[date.getDay()]}, ${months[date.getMonth()]} ${printDay(date)}`;
let humanFriendlyCell = row.insertCell(-1);
let humanFriendlyButton = document.createElement('button');
let humanFriendlyLabel = document.createTextNode(humanFriendlyDate);
humanFriendlyButton.appendChild(humanFriendlyLabel);
humanFriendlyCell.appendChild(humanFriendlyButton);
humanFriendlyButton.addEventListener('click', (event) => {
setAndCopy(textField, humanFriendlyDate, messageField);
});
let isoDate = date.toISOString().split("T")[0];
let isoCell = row.insertCell(-1);
let isoButton = document.createElement('button');
let isoLabel = document.createTextNode(isoDate);
isoButton.appendChild(isoLabel);
isoCell.appendChild(isoButton);
isoButton.addEventListener('click', (event) => {
setAndCopy(textField, isoDate, messageField);
});
}
function refresh() {
let contentTable = document.getElementById('nf-table');
contentTable.innerHTML = '';
let date = new Date();
for (let i = 0; i < 14; i++) {
add(contentTable, i, date);
date.setDate(date.getDate() + 1);
}
}
document.addEventListener('DOMContentLoaded', () => {
refresh();
});