-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
106 lines (88 loc) · 2.91 KB
/
parse.js
File metadata and controls
106 lines (88 loc) · 2.91 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
(function () {
var root = document.documentElement;
var toggle = document.getElementById("themeToggle");
var label = document.getElementById("themeLabel");
var textarea = document.getElementById("somewhere");
var btnCopy = document.getElementById("button-link");
var toast = document.getElementById("btnInfo");
function applyTheme(mode) {
root.setAttribute("data-theme", mode);
try { localStorage.setItem("theme", mode); } catch (e) {}
var isDark = mode === "dark";
if (toggle) toggle.setAttribute("aria-checked", isDark ? "true" : "false");
if (label) label.textContent = isDark ? "Dark" : "Light";
}
applyTheme(root.getAttribute("data-theme") || "light");
if (toggle) {
toggle.addEventListener("click", function () {
var now = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
applyTheme(now);
});
}
function showCopy() {
if (btnCopy) btnCopy.classList.remove("is-hidden");
}
function hideCopy() {
if (btnCopy) btnCopy.classList.add("is-hidden");
}
function showToast() {
if (!toast) return;
toast.classList.remove("collapse");
toast.style.display = "block";
clearTimeout(showToast._t);
showToast._t = setTimeout(function () {
toast.style.display = "none";
toast.classList.add("collapse");
}, 1600);
}
window.hideToast = function () {
if (!toast) return;
toast.style.display = "none";
toast.classList.add("collapse");
};
window.convert = function () {
if (!textarea) return;
var replaced = textarea.value || "";
replaced = replaced.replace(/&/ig, "&");
replaced = replaced.replace(/</ig, "<");
replaced = replaced.replace(/>/ig, ">");
replaced = replaced.replace(/"/ig, """);
replaced = replaced.replace(/'/ig, "'");
replaced = replaced.replace(/±/ig, "±");
replaced = replaced.replace(/©/ig, "©");
replaced = replaced.replace(/®/ig, "®");
replaced = replaced.replace(/ya'll/ig, "ya'll");
textarea.value = replaced;
if ((textarea.value || "").trim().length > 0) showCopy();
};
window.cdClear = function () {
if (textarea) textarea.value = "";
window.hideToast();
hideCopy();
};
async function copyText(text) {
if (!text || !text.trim()) return false;
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return true;
}
} catch (e) {}
try {
textarea.focus();
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
var ok = document.execCommand("copy");
window.getSelection().removeAllRanges();
return !!ok;
} catch (e) {
return false;
}
}
if (btnCopy) {
btnCopy.addEventListener("click", async function () {
var ok = await copyText(textarea ? textarea.value : "");
if (ok) showToast();
});
}
})();