-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
74 lines (60 loc) · 2.05 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
64
65
66
67
68
69
70
71
72
73
74
function getAllUrls(sessionName) {
chrome.tabs.query({currentWindow: true}, tabs => {
let tabUrls = []
tabs.forEach(tab => tabUrls.push(tab.url))
saveSession(sessionName, tabUrls)
})
}
function saveSession(sessionName, tabs) {
chrome.storage.sync.set({[sessionName]:tabs})
}
document.addEventListener('DOMContentLoaded', () => {
const saveButton = document.getElementById('savebtn')
saveButton.addEventListener('click', () => {
const sessionName = document.getElementById('sessionname').value
sessionName ? getAllUrls(sessionName) : null
})
})
async function loadSessionList() {
const sessions = await getSavedSessions()
$('#SessionList').empty()
$.each(sessions, (key, value) => {
$('#SessionList').append(`<option value=${key}>${key}</option><`)
})
}
function getSavedSessions() {
return new Promise((resolve, reject) => {
chrome.storage.sync.get((sessions) => {
if(!chrome.runtime.lastError) {
resolve(sessions)
} else {
reject(chrome.runtime.lastError)
}
})
})
}
function restoreSavedSession() {
const session = document.getElementById('SessionList');
const sessionKey = session.options[session.selectedIndex].value;
chrome.storage.sync.get(sessionKey, (sessionObj) => {
const links = sessionObj[sessionKey]
chrome.tabs.query({currentWindow: true}, function (tabs) {
tabs.forEach(tab => chrome.tabs.remove(tab.id))
})
links.forEach(link => chrome.tabs.create({active:false, url:link}))
})
}
function removeSavedSession() {
const session = document.getElementById('SessionList');
const sessionKey = session.options[session.selectedIndex].value;
chrome.storage.sync.remove(sessionKey)
// update list
loadSessionList()
}
document.addEventListener('DOMContentLoaded', () => {
loadSessionList()
let restore = document.getElementById('restore')
let remove = document.getElementById('remove')
restore.addEventListener('click', () => restoreSavedSession())
remove.addEventListener('click', () => removeSavedSession())
})