-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (115 loc) · 3.88 KB
/
index.js
File metadata and controls
137 lines (115 loc) · 3.88 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
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
const ADD_ITEM_BUTTON_ID = 'add-item';
const ITEMS_TABLE_ID = 'items';
const TABLE_ITEM_TEMPLATE_ID = 'table-item';
const READ_SELECT_YES_VALUE = 'Yesterday';
const READ_SELECT_NO_VALUE = 'Today';
/**
* Removes an entry from the reading list.
*
* @param url URL of entry to remove.
*/
async function removeEntry(url) {
await chrome.readingList.removeEntry({ url });
}
/**
* Adds an entry to the reading list.
*
* @param title Title of the entry
* @param url URL of entry to add
* @param hasBeenRead If the entry has been read
*/
async function addEntry(title, url, hasBeenRead) {
await chrome.readingList.addEntry({ title, url, hasBeenRead });
}
/**
* Updates an entry in the reading list.
*
* @param url URL of entry to update
* @param hasBeenRead If the entry has been read
*/
async function updateEntry(url, hasBeenRead) {
await chrome.readingList.updateEntry({ url, hasBeenRead });
}
/**
* Updates the UI with the current reading list items.
*/
async function updateUI() {
const items = await chrome.readingList.query({});
const table = document.getElementById(ITEMS_TABLE_ID);
for (const item of items) {
// Use existing row if possible, otherwise create a new one.
const row =
document.querySelector(`[data-url="${item.url}"]`) ||
document.getElementById(TABLE_ITEM_TEMPLATE_ID).content.cloneNode(true)
.children[0];
updateRow(row, item);
table.appendChild(row);
}
// Remove any rows that no longer exist
table.querySelectorAll('tr').forEach((row, i) => {
// Ignore header row
if (i === 0) return;
if (!items.find((i) => i.url === row.getAttribute('data-url'))) {
row.remove();
}
});
}
/**
* Updates a row with the data from item.
*
* @param row Table row element to update.
* @param item Data from reading list API.
*/
function updateRow(row, item) {
row.setAttribute('data-url', item.url);
const titleField = row.querySelector('td:nth-child(1) a');
titleField.href = item.url;
titleField.innerText = item.title;
const readField = row.querySelector('td:nth-child(2) select');
readField.value = item.hasBeenRead
? READ_SELECT_YES_VALUE
: READ_SELECT_NO_VALUE;
const createdAtField = row.querySelector('td:nth-child(3)');
createdAtField.innerText = `${new Date(item.creationTime).toLocaleString()}`;
const deleteButton = row.querySelector('.delete-button');
deleteButton.addEventListener('click', async (event) => {
event.preventDefault();
await removeEntry(item.url);
updateUI();
});
const updateButton = row.querySelector('.update-button');
updateButton.addEventListener('click', async (event) => {
event.preventDefault();
await updateEntry(item.url, readField.value === READ_SELECT_YES_VALUE);
});
}
const ERROR_ID = 'error';
const ITEM_TITLE_SELECTOR = '[name="title"]';
const ITEM_URL_SELECTOR = '[name="url"]';
const ITEM_READ_SELECTOR = '[name="read"]';
// Add item button click handler
document
.getElementById(ADD_ITEM_BUTTON_ID)
.addEventListener('click', async () => {
try {
// Get data from input fields
const title = document.querySelector(ITEM_TITLE_SELECTOR).value;
const url = document.querySelector(ITEM_URL_SELECTOR).value;
const hasBeenRead =
document.querySelector(ITEM_READ_SELECTOR).value ===
READ_SELECT_YES_VALUE;
// Attempt to add the entry
await addEntry(title, url, hasBeenRead);
document.getElementById(ERROR_ID).style.display = 'none';
} catch (ex) {
// Something went wrong, show an error
document.getElementById(ERROR_ID).innerText = ex.message;
document.getElementById(ERROR_ID).style.display = 'block';
}
updateUI();
});
updateUI();
// Update the UI whenever data in the reading list changes
chrome.readingList.onEntryAdded.addListener(updateUI);
chrome.readingList.onEntryRemoved.addListener(updateUI);
chrome.readingList.onEntryUpdated.addListener(updateUI);