-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
241 lines (201 loc) · 6 KB
/
popup.js
File metadata and controls
241 lines (201 loc) · 6 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* global chrome */
const GIST_NAME = 'chrome-context-sync.json'
//
// Input handling.
//
async function loadFromStorage () {
const savedToken = (await chrome.storage.sync.get('githubToken')).githubToken
if (savedToken) {
document.getElementById('github-token').value = savedToken
}
const savedUrls = (await chrome.storage.sync.get('urls')).urls
if (savedUrls) {
document.getElementById('urls').value = savedUrls
}
}
loadFromStorage()
async function getHeaders () {
const token = document.getElementById('github-token').value
const headers = {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
'X-GitHub-Api-Version': '2022-11-28'
}
return headers
}
async function getUrls () {
const urls = document.getElementById('urls').value.trim().split('\n').map(url => url.trim())
return urls
}
getUrls()
//
// Cookie management.
//
function buildUrl (secure, domain, path) {
if (domain.startsWith('.')) {
domain = domain.substr(1)
}
return `http${secure ? 's' : ''}://${domain}${path}`
}
async function getCookies (domain) {
const cookies = await chrome.cookies.getAll({ domain })
return cookies.map(cookie => {
const cookieDetails = {
name: cookie.name,
value: cookie.value,
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
storeId: cookie.storeId,
url: buildUrl(cookie.secure, cookie.domain, cookie.path)
}
if (!cookie.hostOnly) {
cookieDetails.domain = cookie.domain
}
if (!cookie.session) {
cookieDetails.expirationDate = cookie.expirationDate
}
return cookieDetails
})
}
//
// Enabling and disabling JS.
//
async function disableScripts () {
return chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1],
addRules: [{
id: 1,
priority: 1,
action: { type: 'block' },
condition: {
urlFilter: '*',
resourceTypes: ['script']
}
}]
})
}
async function enableScripts () {
return chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [1] })
}
//
// GitHub Gist API
//
async function getGist () {
const headers = await getHeaders()
const res = await fetch('https://api.github.com/gists', { headers })
const gists = await res.json()
return gists.find(gist => gist.files[GIST_NAME])
}
async function saveData (data) {
const headers = await getHeaders()
const gist = await getGist()
if (!gist) {
// Create new gist.
await fetch('https://api.github.com/gists', {
method: 'POST',
headers,
body: JSON.stringify({
description: `Data from ${data.domain}`,
public: false,
files: {
[GIST_NAME]: {
content: JSON.stringify(data)
}
}
})
})
} else {
// Update existing gist.
await fetch(`https://api.github.com/gists/${gist.id}`, {
method: 'PATCH',
headers,
body: JSON.stringify({
description: `Data from ${data.domain}`,
public: false,
files: {
[GIST_NAME]: {
content: JSON.stringify(data)
}
}
})
})
}
}
// -----------------------------------------------------------------------------
// Event handlers
// -----------------------------------------------------------------------------
document.getElementById('save').addEventListener('click', async event => {
const data = []
const urls = await getUrls()
await disableScripts()
for (const url of urls) {
const entry = { url, hostname: new URL(url).hostname }
// Try to get cookies for the root domain.
entry.domain = entry.hostname.split('.').slice(-2).join('.')
entry.cookies = await getCookies(entry.domain)
// If not possible, get cookies for the full domain.
if (entry.cookies.length === 0) {
entry.domain = entry.hostname
entry.cookies = await getCookies(entry.domain)
}
// Visit the URL in order to obtain storage data.
const tab = await chrome.tabs.create({ url, active: false })
const response = await new Promise(resolve => {
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
if (tabId === tab.id && changeInfo.status === 'complete') {
resolve(await chrome.tabs.sendMessage(tab.id, { cmd: 'save' }))
}
})
})
await chrome.tabs.remove(tab.id)
entry.ss = response.ss
entry.ls = response.ls
data.push(entry)
}
await enableScripts()
await saveData(data)
})
document.getElementById('load').addEventListener('click', async event => {
const gist = await getGist()
const res = await fetch(gist.files[GIST_NAME].raw_url)
const data = await res.json()
await disableScripts()
for (const entry of data) {
// Restore cookies.
for (const cookie of entry.cookies) {
try {
await chrome.cookies.set(cookie)
} catch (err) {
console.log('Unable to set cookie:', cookie)
}
}
// Resotre storage data.
const tab = await chrome.tabs.create({ url: entry.url, active: false })
await new Promise(resolve => {
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
if (tabId === tab.id && changeInfo.status === 'complete') {
resolve(await chrome.tabs.sendMessage(tab.id, { cmd: 'load', ss: entry.ss, ls: entry.ls }))
}
})
})
await chrome.tabs.remove(tab.id)
}
await enableScripts()
// Open tabs for each URL.
for (const entry of data) {
await chrome.tabs.create({ url: entry.url, active: false })
}
})
document.getElementById('github-token').addEventListener('input', async event => {
await chrome.storage.sync.set({ githubToken: event.target.value.trim() })
})
document.getElementById('urls').addEventListener('input', async event => {
await chrome.storage.sync.set({ urls: event.target.value.trim() })
})
document.getElementById('enable-scripts').addEventListener('click', async event => {
await enableScripts()
})
document.getElementById('disable-scripts').addEventListener('click', async event => {
await disableScripts()
})