-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoption.js
164 lines (146 loc) · 4.56 KB
/
option.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
import { Options } from './module.js';
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
async function getTexts() {
return await chrome.storage.local.get();
}
async function clearTexts() {
return await chrome.storage.local.clear();
}
async function removeTexts(id) {
return await chrome.storage.local.remove(id);
}
async function textStats() {
if (isFirefox) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1385832#c20
return new TextEncoder().encode(
Object.entries(await browser.storage.local.get())
.map(([key, value]) => key + JSON.stringify(value))
.join('')
).length;
}
return await chrome.storage.local.getBytesInUse(null);
}
function humanSize(size) {
const units = ['B', 'KB', 'MB', 'GB'];
let i = 0;
while (i < units.length - 1 && size >= 1024) {
size /= 1024;
i += 1;
}
return `${size.toFixed(2)} ${units[i]}`;
}
async function refresh() {
let rows = [];
for (const [id, { text, url, createdAt }] of Object.entries(
await getTexts()
)) {
rows.push([id, text, url, createdAt]);
}
// sort by createdAt desc
rows.sort((a, b) => b[3] - a[3]);
let table = [];
for (const row of rows) {
let [id, text, url, createdAt] = row;
table.push(`<tr>
<td id="text-${id}"></td>
<td>${new Date(createdAt).toLocaleString('en-GB')}</td>
<td>
<div class="btn-group" role="group">
<button type="button" class="btn btn-secondary btn-sm" id="btn-copy-${id}">Copy</button>
<a role="button" class="btn btn-secondary btn-sm" href="${url}">Goto</a>
<button type="button" class="btn btn-danger btn-sm" id="btn-delete-${id}">Delete</button>
</div>
</td>
</tr>`);
}
document.getElementById('text-list').innerHTML = table.join('');
for (const row of rows) {
let [id, text] = row;
// use `textContent` to prevent XSS
document.getElementById(`text-${id}`).textContent = text;
document.getElementById(`btn-delete-${id}`).onclick = async function () {
if (confirm('Are you sure?')) {
await removeTexts(id);
refresh();
}
};
const copyButton = document.getElementById(`btn-copy-${id}`);
copyButton.onclick = function () {
navigator.clipboard.writeText(
document.getElementById(`text-${id}`).innerHTML
);
copyButton.innerHTML = 'Copied';
setTimeout(function () {
copyButton.innerHTML = 'Copy';
}, 1000);
};
}
}
function applyColorScheme(scheme) {
if (
scheme === 'system' &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
document.documentElement.setAttribute('data-bs-theme', 'dark');
} else {
document.documentElement.setAttribute('data-bs-theme', scheme);
}
}
window.onload = async function () {
let opt = new Options(chrome.storage.sync);
// console.log(await opt.dump());
const selectColor = document.getElementById('select-color');
selectColor.value = await opt.getColorScheme();
applyColorScheme(selectColor.value);
selectColor.onchange = async function () {
await opt.setColorScheme(selectColor.value);
applyColorScheme(selectColor.value);
};
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', async (e) => {
const colorScheme = await opt.getColorScheme();
if (colorScheme === 'system') {
if (e.matches) {
// set to dark
applyColorScheme('dark');
} else {
// set to light
applyColorScheme('light');
}
}
});
document.getElementById('btn-clear').onclick = async function () {
if (confirm('Are you sure to clear all saved texts?')) {
await clearTexts();
await refresh();
}
};
document.getElementById('btn-export').onclick = async function () {
const { version, author, homepage_url } =
await chrome.runtime.getManifest();
const payload = {
homepage: homepage_url,
version: version,
author: author,
createdAt: new Date().toLocaleString(),
texts: await getTexts(),
};
const blob = new Blob([JSON.stringify(payload, null, 2)], {
type: 'application/json',
});
chrome.downloads.download({
url: URL.createObjectURL(blob),
saveAs: true,
filename: 'saved-texts.json',
});
};
document.getElementById('input-size').value = humanSize(await textStats());
const inputNotification = document.getElementById('input-notification');
inputNotification.checked = await opt.getNotification();
inputNotification.onclick = async function () {
opt.setNotification(inputNotification.checked);
};
await refresh();
};