-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
117 lines (93 loc) · 3.46 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
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
import Mustache from "./libs/mustache.js";
// import dayjs from 'dayjs' // ES 2015
import { restoreTabs, clickSaveTabs } from "./shared.js";
async function renderHello(sessionData) {
const template = document.getElementById('template').innerHTML;
const rendered = Mustache.render(template, { sessions: sessionData });
document.getElementById('target').innerHTML = rendered;
addHandlers();
}
document.addEventListener("DOMContentLoaded", async (event) => {
dayjs.extend(window.dayjs_plugin_relativeTime)
document.querySelector("#save-session-btn")
.addEventListener('click', (event) => {
clickSaveTabs();
});
var autoSavesData = await getStoredTabs("auto");
var manualSavesData = await getStoredTabs("manual");
console.log({manualSavesData})
const template = document.getElementById('template').innerHTML;
const autoSavesRendered = Mustache.render(template, { sessions: autoSavesData });
const manualSavesRendered = Mustache.render(template, { sessions: manualSavesData });
document.getElementById('auto-saves').innerHTML = autoSavesRendered;
document.getElementById('manual-saves').innerHTML = manualSavesRendered;
await addHandlers();
});
async function getStoredTabs(type) {
const storeKey = `tabs_${type}`
const storedTabs = await chrome.storage.local.get([storeKey]);
const tabs = storedTabs[storeKey];
console.debug({storedTabs});
// const procData = [];
const sessionData = [];
for (const t in tabs) {
const session = tabs[t];
const winTabsData = [];
let i = 1;
for (const win in session) {
const winTabs = session[win];
winTabsData.push({window: i++, win_tabs: session[win]})
}
sessionData.push({type: type, timestamp: t, displayTimestamp: dayjs(parseInt(t)).fromNow(), windows: winTabsData})
}
sessionData.reverse();
return sessionData
}
async function addHandlers() {
const buttons = document.querySelectorAll(".restore-sesh-btn");
console.log("wof");
console.log({buttons});
buttons.forEach ( b => {
b.addEventListener('click', async (event) => {
const winIds = await getCurrentWins();
console.log(event.target.getAttribute("timestamp"));
const type = event.target.getAttribute("type");
console.log({type})
// const currentTabs = await tabDump();
const currentTabs = {};
const storeKey = `tabs_${type}`;
const storedTabs = await chrome.storage.local.get([storeKey]);
const tabsToRestore = storedTabs[storeKey][event.target.getAttribute("timestamp")]
await restoreTabs(tabsToRestore, currentTabs);
closeWindows(winIds);
});
});
const window_titles = document.querySelectorAll(".windows-title");
window_titles.forEach ( w => {
w.addEventListener('click', async (event) => {
console.log('clickeed');
console.log(w);
console.log(w.parentNode);
w.parentNode.querySelector(".tab-list").classList.toggle('hide');
});
});
const timestamp_titles = document.querySelectorAll(".timestamp-title");
timestamp_titles.forEach ( w => {
w.addEventListener('click', async (event) => {
w.parentNode.querySelectorAll(".tab-list").forEach( t => {
t.classList.toggle('hide');
});
});
});
}
async function getCurrentWins() {
const wins = await chrome.windows.getAll();
const winIds = wins.map(x => x.id);
return winIds
}
async function closeWindows(winIds) {
console.log('closing', winIds);
for (const w in winIds) {
await chrome.windows.remove(winIds[w]);
}
}