-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
87 lines (70 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
document.addEventListener('DOMContentLoaded', function () {
var btnClick = document.getElementById('btnClick');
var sessionsList = document.getElementById('sessionsList');
var savedSessions = {};
chrome.storage.local.get('savedSessions', function (result) {
if (result.savedSessions) {
savedSessions = result.savedSessions;
renderSessions();
}
});
document.getElementById('sessionName').addEventListener('keyup', function (event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById('btnClick').click();
}
});
btnClick.addEventListener('click', function () {
var sessionName = document.getElementById('sessionName').value.trim();
if (sessionName !== '') {
chrome.tabs.query({ currentWindow: true }, function (tabs) {
var urls = tabs.map(function (tab) {
return tab.url;
});
savedSessions[sessionName] = urls;
chrome.storage.local.set({ 'savedSessions': savedSessions }, function () {
renderSessions();
});
});
}
document.getElementById('sessionName').value = '';
});
function renderSessions() {
sessionsList.innerHTML = '';
for (var sessionName in savedSessions) {
var sessionItemDiv = document.createElement('div');
sessionItemDiv.classList.add('session-item');
var sessionNameDiv = document.createElement('div');
sessionNameDiv.textContent = sessionName;
sessionNameDiv.classList.add('session-name');
var deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
(function (sessionName) {
deleteButton.addEventListener('click', function (event) {
event.stopPropagation();
delete savedSessions[sessionName];
chrome.storage.local.set({ 'savedSessions': savedSessions }, function () {
renderSessions();
});
});
})(sessionName);
sessionItemDiv.appendChild(sessionNameDiv);
sessionItemDiv.appendChild(deleteButton);
(function (sessionName) {
sessionNameDiv.addEventListener('click', function () {
var sessionUrls = savedSessions[sessionName];
openSession(sessionUrls);
});
})(sessionName);
sessionsList.appendChild(sessionItemDiv);
}
}
function openSession(urls) {
var tabs = [];
urls.forEach(function (url) {
tabs.push({ url: url });
});
chrome.windows.create({ "url": urls }, function (window) {
});
}
});