-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsharer.js
47 lines (39 loc) · 1.47 KB
/
sharer.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
const btn = document.getElementById("shareButton");
btn.addEventListener("click", (e) => {
console.log("====\nSaving the program====\n");
saveAssemblyCode();
});
function saveAssemblyCode() {
const assemblyCode = document.getElementById("assemblyCode").innerText;
const formData = new FormData();
formData.append("code", assemblyCode);
fetch("db.php", {method : "POST", body : formData})
.then((response) => {
if (!response.ok) {
throw new Error("The server responded with error: " + response.status);
}
return response.text();
})
.then((data) => {
// data is ?id=int
const shareURL =
`${new URL(window.location.href).origin}/PicoBlaze.html${data}`;
document.getElementById("shareURL").innerText = shareURL;
document.getElementById("uploadSuccessfulMessage").style.display =
"block";
})
.catch((error) => { alert(error); });
}
function copyShareURLToClipboard() {
const shareURL = document.getElementById("shareURL").innerText;
if (!navigator.clipboard) {
alert(
"Your browser, for some reason, doesn't let JavaScript access the clipboard. You will need to copy the URL manually.");
return;
}
navigator.clipboard.writeText(shareURL)
.then(() => { alert("The URL is successfully copied!"); })
.catch(
() => {alert(
"Copying the URL to clipboard didn't succeed. You will need to copy the URL manually.")});
}