-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCurrent.js
33 lines (27 loc) · 975 Bytes
/
addCurrent.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
//TODO Find a way to edit the json file and push the new site in it.
// Follow up: its bad, just use single object
var myJson = JSON.parse(localStorage.getItem('sites'));
document.addEventListener('DOMContentLoaded', function() {
var button = document.getElementById('AddCurrentBtn');
button.addEventListener('click', function() {
// onClick's logic below:
getCurrentTab().then(tab => {
var url = tab.url;
var icon = tab.favIconUrl;
var name = new URL(url).hostname;
let newSite = {
"name": name,
"url": url,
"icon": icon
}
myJson.push(newSite)
localStorage.setItem('sites', JSON.stringify(myJson));
location.reload();
});
});
});
async function getCurrentTab() {
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true });
return tab;
}