-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
94 lines (89 loc) · 2.56 KB
/
main.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
"use strict";
let app = new Vue({
el: "#app",
data: {
status: "Ready.",
url: "None.",
deleteurl: "None.",
alertType: "alert-success",
copyBtnDisable: true
},
computed: {
controls: () => {
if (navigator.platform === "MacIntel") {
return "⌘";
}
return "Ctrl";
}
},
methods: {
markdowncopyToClip: () => {
let el = document.createElement("textarea");
el.value = app.url;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
app.status = "[OK] Markdown Link Copied to system clipboard.";
},
deletecopyToClip: () => {
let el = document.createElement("textarea");
el.value = app.deleteurl;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
app.status = "[OK] Delete Link copied to system clipboard.";
}
}
});
document.addEventListener("paste", event => {
let blob;
app.status = "Processing...";
app.alertType = "alert-warning";
app.copyBtnDisable = true;
if (event.clipboardData || event.originalEvent) {
let clipboardData =
event.clipboardData || event.originalEvent.clipboardData;
if (clipboardData.items) {
for (let i = 0; i < clipboardData.items.length; i++) {
if (clipboardData.items[i].type.indexOf("image") !== -1) {
blob = clipboardData.items[i].getAsFile();
}
}
}
}
let render = new FileReader();
render.onload = event => {
let base64 = event.target.result;
document.getElementById("img").setAttribute("src", base64);
};
try {
render.readAsDataURL(blob);
} catch (error) {
app.status = "[Error] Sorry, you didn't paste an image.";
app.alertType = "alert-warning";
return;
}
let formData = new FormData();
formData.append("smfile", blob);
axios
.post("https://sm.ms/api/upload", formData)
.then(response => {
let r = response.data;
if (r.code == "success") {
app.status = "[OK] Upload finished.";
app.alertType = "alert-success";
app.url = `![](${r.data.url})`;
app.deleteurl = `${r.data.delete}`;
app.copyBtnDisable = false;
} else {
app.status = `[Error: ${r.msg}] Upload failed. `;
app.alertType = "alert-danger";
}
})
.catch(error => {
app.status = `[Error: ${error}] Upload failed. `;
app.alertType = "alert-danger";
});
});