-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
52 lines (42 loc) · 1.61 KB
/
options.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
document.addEventListener('DOMContentLoaded', function() {
const urlList = document.getElementById('url-list');
chrome.storage.sync.get(['urls'], function(result) {
const urls = result.urls || [];
urls.forEach(({url, time}, index) => {
addUrlToList(url, time, index);
});
});
document.getElementById('add').addEventListener('click', function() {
const url = document.getElementById('url').value;
const time = document.getElementById('time').value;
chrome.storage.sync.get(['urls'], function(result) {
const urls = result.urls || [];
urls.push({url, time});
chrome.storage.sync.set({urls: urls}, function() {
addUrlToList(url, time, urls.length - 1);
});
});
});
function addUrlToList(url, time, index) {
const div = document.createElement('div');
div.className = 'url-item';
div.innerHTML = `
<span>${url} => ${time}</span>
<button data-index='${index}'>Remove</button>
`;
div.querySelector('button').addEventListener('click', function() {
removeUrl(index);
})
urlList.appendChild(div);
}
function removeUrl(index) {
chrome.storage.sync.get(['urls'], function(result) {
let urls = result.urls || [];
urls.splice(index, 1);
chrome.storage.sync.set({urls: urls}, function() {
urlList.innerHTML = '';
urls.forEach(({url, time}, index) => addUrlToList(url, time, index));
});
});
}
})