-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (76 loc) · 2.01 KB
/
index.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
import {ce, assertDefined, SHARE_URL} from "./util.js";
import {DB} from "./DB.js";
import {App} from "./App.js";
import {AppData} from "./AppData.js";
const root = ReactDOM.createRoot(document.getElementById('root'));
function loadShare(key) {
return new Promise((resolve, reject) => {
// get the data from aws
let reqObj = {
"action": "get",
"key": key
};
let req = new XMLHttpRequest();
req.onerror = (e) => {
reject(e);
};
req.onload = (_) => {
if (req.status == 200) {
let respObj = JSON.parse(req.responseText);
let data = respObj.data;
if (data === undefined) {
reject(new Error("Response format error"));
} else {
resolve(data); // string
}
} else {
reject(new Error(req.responseText));
}
}
req.open("POST", SHARE_URL);
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(reqObj));
});
}
async function launch() {
let db = assertDefined(await DB.open());
// see if share is defined in
let params = (new URL(window.location)).searchParams;
let shareKey = params.get("share");
let initKey = null;
if (shareKey !== null && shareKey != "") {
let shareData = await loadShare(shareKey);
let currentFiles = await db.getFiles();
let foundKey = -1;
for (let [currentKey, currentFile] of currentFiles) {
if (currentFile.data == shareData) {
foundKey = currentKey;
break;
}
}
if (foundKey == -1) {
foundKey = await db.newFile();
await db.setFile(foundKey, shareData);
}
initKey = foundKey;
}
root.render(ce(App, {db: db, data: await AppData.new(db, initKey)}));
}
async function main() {
try {
let query = await navigator.permissions.query({ name: 'clipboard-write', allowWithoutGesture: false });
if (query.state == "granted") {
await launch();
} else if (query.state == "prompt") {
query.onchange = () => {
void launch();
};
} else {
console.error("clipboard copy/cut access denied");
await launch();
}
} catch(_) {
void launch();
}
}
void main();