forked from MikuAuahDark/gws-pasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
176 lines (152 loc) · 5.59 KB
/
index.html
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Get Well Soon</title>
<meta name="description" content="Generator Copypasta Halu">
<meta name="keywords" content="copypasta, generator, stres, halu">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<script>
const TRANSFORMERS = {
"none": (input) => input,
"camelcase": (input) => {
const splitted = input.split(" ")
for (let i = 0; i < splitted.length; i++) {
const s = splitted[i]
if (s.length > 0) {
splitted[i] = s[0].toLocaleUpperCase() + s.substring(1);
}
}
return splitted.join(" ")
},
"uppercase": (input) => input.toLocaleUpperCase(),
"lowercase": (input) => input.toLocaleLowerCase()
}
function ready(fn) {
if (document.readyState !== "loading") {
fn()
} else {
document.addEventListener("DOMContentLoaded", fn)
}
}
function rebuildCopypastaText(copypastaResultElem, originalText, inputs, twolf) {
const newText = originalText.replaceAll(/{([\w_]+:?\w*)}/g, (match, p1) => {
const nameAndTransform = p1.split(":", 2)
const inputName = nameAndTransform[0]
const transformFunc = TRANSFORMERS[nameAndTransform[1] ?? "none"] ?? TRANSFORMERS.none
if (inputs[inputName] === undefined) {
return "{" + p1 + "}"
}
return transformFunc(inputs[inputName])
})
const fixedNewLineText = newText.replaceAll(/\n+/g, match => {
return match.length == 1 ? " " : match.substring(1 - twolf)
})
copypastaResultElem.value = fixedNewLineText
}
function getPreviousInput(id, def) {
console.log("Getting input", id)
return localStorage.getItem(id) ?? def
}
function setInputValue(id, val) {
console.log("Setting input", id)
localStorage.setItem(id, val)
}
function initializeCopypasta(inputRootElem, copypastaResultElem, copypastaText, inputdefs, pastaId) {
let twoLFElem = document.getElementById("copypasta_2lf")
let fragment = document.createDocumentFragment()
let inputValues = {}
twoLFElem.onchange = () => rebuildCopypastaText(copypastaResultElem, copypastaText, inputValues, twoLFElem.checked)
function inputOnChange(ev) {
inputValues[ev.target.id] = ev.target.value
setInputValue(`${pastaId}.${ev.target.id}`, ev.target.value)
rebuildCopypastaText(copypastaResultElem, copypastaText, inputValues, twoLFElem.checked)
}
inputdefs.forEach(input => {
const div = document.createElement("div")
const label = document.createElement("label")
const inputElem = document.createElement("input")
const placeholder = input.default ? ("Contoh: " + input.default) : input.description
inputValue = getPreviousInput(`${pastaId}.${input.name}`, input.default)
inputValues[input.name] = inputValue
inputElem.id = input.name
inputElem.type = "text"
inputElem.value = inputValue
inputElem.setAttribute("placeholder", placeholder)
inputElem.addEventListener("input", inputOnChange)
label.textContent = input.description
div.classList.add("field")
div.appendChild(label)
div.appendChild(inputElem)
fragment.appendChild(div)
});
inputRootElem.replaceChildren(fragment)
rebuildCopypastaText(copypastaResultElem, copypastaText, inputValues, twoLFElem.checked)
}
ready(async () => {
let copypastaList = document.getElementById("copypasta_list")
let copypastaResult = document.getElementById("copypasta_result")
let copypastaInputRoot = document.getElementById("copypasta_inputs")
let copypastaCopyButton = document.getElementById("copypasta_copy")
let jsondata = null
copypastaResult.value = ""
copypastaCopyButton.addEventListener("click", async () => {
if (copypastaResult.value) {
await navigator.clipboard.writeText(copypastaResult.value)
}
})
try {
const response = await fetch("copypasta.json")
jsondata = await response.json()
} catch (error) {
console.log(error)
}
if (jsondata !== null) {
let inverseMap = {}
jsondata.forEach(element => {
const optElem = document.createElement("option")
inverseMap[element.id] = element
optElem.value = element.id
optElem.textContent = element.name
copypastaList.appendChild(optElem)
})
copypastaList.addEventListener("change", (ev) => {
const copypasta = inverseMap[ev.target.value]
initializeCopypasta(copypastaInputRoot, copypastaResult, copypasta.text, copypasta.inputs, copypasta.id)
})
}
$('.ui.dropdown').dropdown()
})
</script>
</head>
<body style="padding:1em">
<section class="ui green segment">
<h1 class="ui header"></i><i class="paste icon"></i> Halu Generator</h1>
<select id="copypasta_list" class="ui fluid dropdown">
<option value="" default>Pilih Copypasta</option>
</select>
<div class="ui red segment">
<h2 class="ui header">Parameter</h2>
<div id="copypasta_inputs" class="ui form">
</div>
</div>
<div class="ui form">
<textarea id="copypasta_result" readonly></textarea>
</div>
<br />
<div class="ui checkbox">
<input id="copypasta_2lf" type="checkbox">
<label>Paragraf baru, 2 baris baru</label>
</div>
<br />
<br />
<button id="copypasta_copy" class="ui fluid blue button">
<i class="copy icon"></i>Salin
</button>
</section>
</body>
</html>