-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabs_api.js
144 lines (125 loc) · 3.82 KB
/
tabs_api.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// get tabs
let init = async () => {
let tabs = await chrome.tabs.query({})
console.log(`Tabs: ${tabs}`)
let tabGroup = {};
for (var i = tabs.length - 1; i >= 0; i--) {
let o = tabs[i];
// get group name
let groupName = ""
if(o.groupId == -1) {
groupName = "nogroup"
} else {
let group = await chrome.tabGroups.get(o.groupId)
groupName = group.title
if(groupName === undefined) {
groupName = o.groupId
}
}
// populate map
if(tabGroup[groupName] === undefined) {
tabGroup[groupName] = [o.url]
} else {
tabGroup[groupName].push(o.url)
}
}
let jsonstr = JSON.stringify(tabGroup)
console.log(jsonstr)
el = document.querySelector("#tab-group-json")
if (el === null ) {
prompt("Tab group json",jsonstr)
} else {
el.textContent = jsonstr;
}
// update storage div on init
chrome.storage.sync.get(renderStore)
let fancyDate = dateFormat(new Date(), "dd/mm/yyyy HH:MM:ss")
document.querySelector("#store-name").value = `Session ${fancyDate}`
}
init()
document.querySelector("#load").onclick = async () => {
// load tabs from json
let jsonstr = document.querySelector("#tab-group-json").value
let tabGroups = JSON.parse(jsonstr)
let window = await chrome.windows.create({focused:false})
for(group in tabGroups) {
let urls = tabGroups[group]
let tabIds = []
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
console.log(url)
let tab = await chrome.tabs.create({
url: url,
active: false,
})
if(tab.id !== chrome.tabs.TAB_ID_NONE) {
tabIds.push(tab.id)
}
}
let groupId = await chrome.tabs.group({
tabIds: tabIds
})
chrome.tabGroups.update(groupId, {title: group})
chrome.tabGroups.move(groupId, {
index: -1,
windowId: window.id
})
}
}
document.querySelector("#store-button").onclick = async () => {
let jsonstr = document.querySelector("#tab-group-json").value
let storename = document.querySelector("#store-name").value
let o = {}
o[storename] = jsonstr
let v = await chrome.storage.sync.set(o)
}
document.querySelector("#store-clear-button").onclick = async () => {
chrome.storage.sync.clear(()=>{console.log("storage cleared")})
}
chrome.storage.sync.onChanged.addListener( async (changes) => {
console.log(`store changed`)
chrome.storage.sync.get(renderStore)
})
function renderStore(store) {
let parentEl = document.querySelector("#stored-json")
removeAllChildNodes(parentEl)
for(k in store) {
let e = genEl(k,store[k])
parentEl.appendChild(e)
}
}
// parse json to elements
function genEl(name, jsonstr ) {
let parentEl = document.createElement("tr")
let loadEl = document.createElement("td")
let loadButtonEl = document.createElement("button")
loadButtonEl.textContent = "Load"
loadButtonEl.jsonstr = jsonstr
loadButtonEl.onclick = async(e) => {
document.querySelector("#tab-group-json").value = e.target.jsonstr
document.querySelector("#load").click()
}
loadEl.appendChild(loadButtonEl)
let delButtonEl = document.createElement("button")
delButtonEl.textContent = "del"
delButtonEl.nm = name
delButtonEl.onclick = async(e) => {
chrome.storage.sync.remove(e.target.nm, ()=>{console.log(`storage entry deleted: ${e.target.nm}`)})
}
loadEl.appendChild(delButtonEl)
parentEl.appendChild(loadEl)
let detailsEl = document.createElement("details")
let nameEl = document.createElement("summary")
nameEl.textContent = name
detailsEl.appendChild(nameEl)
let jsonstrEl = document.createElement("p")
jsonstrEl.textContent = jsonstr
detailsEl.appendChild(jsonstrEl)
parentEl.appendChild(detailsEl)
return parentEl
}
function removeAllChildNodes(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}