-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (132 loc) · 4.44 KB
/
script.js
File metadata and controls
143 lines (132 loc) · 4.44 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
const { createApp, ref, computed } = Vue;
const getItemValue = (url, key) => {
const getItemFromFragmentIdentifier = (url, key) => {
if (url && url.hash) {
const hash = url.hash.substr(1);
const params = new URLSearchParams(hash);
return params.get(key);
}
return null;
};
return (
url.searchParams.get(key) ||
getItemFromFragmentIdentifier(key) ||
localStorage.getItem(key)
);
};
const encodeHTMLToBase64 = (html) => {
const script =
'<script>document.querySelectorAll("a").forEach(link => link.setAttribute("target", "_blank"))<\/script>';
const index = html.indexOf("</html>");
if (index >= 0) {
html = html.slice(0, index) + script + html.slice(index);
}
return `data:text/html;charset=utf-8,${encodeURIComponent(html)}`;
};
const html2Text = (html) => {
const div = document.createElement("div");
div.innerHTML = html;
div.querySelectorAll("head, style, script").forEach((element) => element.remove());
div.querySelectorAll("br").forEach((br) => {
br.insertAdjacentHTML("afterend", "\n");
br.remove();
});
div.querySelectorAll("a").forEach((a) => {
const href = a.getAttribute("href");
const text = a.textContent;
a.insertAdjacentHTML("afterend", `${text}[${href}]`);
a.remove();
});
return div.textContent;
};
const isMobile = () => window.innerWidth <= 768;
createApp({
setup() {
const url = new URL(document.location.href);
const namespace = ref(getItemValue(url, "namespace"));
const apikey = ref(getItemValue(url, "apikey"));
const items = ref([]);
const current = ref(null);
const showAlert = ref(true);
const showHTML = ref(false);
const showContent = ref(false);
const offset = ref(0);
const encodedHTML = computed(() => {
return encodeHTMLToBase64(current.value?.html || "");
});
const selectItem = (item) => {
showHTML.value = false;
showAlert.value = true;
if (!item.text && item.html) {
item.text = html2Text(item.html);
}
current.value = item;
if (isMobile()) {
showContent.value = true;
}
};
const goBack = () => {
showContent.value = false;
};
const loadData = (_offset) => {
localStorage.setItem("namespace", namespace.value);
localStorage.setItem("apikey", apikey.value);
fetch("/api/emails", {
method: "POST",
body: btoa(
JSON.stringify({
apikey: apikey.value,
namespace: namespace.value,
limit: 10,
offset: _offset,
})
),
})
.then((response) => response.json())
.then((data) => {
if (_offset) {
items.value = items.value.concat(data.emails);
} else {
items.value = data.emails;
if (data.emails.length > 0 && !isMobile()) {
selectItem(items.value[0]);
} else if (data.emails.length > 0) {
current.value = null;
showContent.value = false;
}
}
offset.value = data.offset + data.limit;
});
};
const fileSizeToString = (size) => {
if (size < 1000) {
return `${size} B`;
}
if (size < 1000 * 1000) {
return `${(size / 1000).toFixed(2)} KB`;
}
if (size < 1000 * 1000 * 1000) {
return `${(size / 1000 / 1000).toFixed(2)} MB`;
}
return `${(size / 1000 / 1000 / 1000).toFixed(2)} GB`;
};
if (apikey.value && namespace.value) {
loadData(0);
}
return {
namespace,
apikey,
items,
offset,
current,
showAlert,
showHTML,
showContent,
encodedHTML,
loadData,
selectItem,
goBack,
fileSizeToString,
};
},
}).mount("#app");