-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-block-maker.html
More file actions
294 lines (263 loc) · 9.1 KB
/
custom-block-maker.html
File metadata and controls
294 lines (263 loc) · 9.1 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Custom Blocks - CatWeb Tools</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="./static/images/favicon.ico" />
<link rel="stylesheet" href="stylesheet.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
</head>
<body>
<header>
<h1>Custom Block Maker</h1>
<div class="description">Make your own custom Script-Blocks.</div>
</header>
<main>
<div id="block">
<img src="./static/images/8.png" width="32" height="32" />
<div class="input-group">
<input class="label-input" type="text" placeholder="Type text here..." />
<button class="delete-button">
<img src="./static/images/trash_icon.svg" />
</button>
</div>
<div class="input-group">
<input type="text" placeholder="<variable>" class="block-input" />
<button class="delete-button">
<img src="./static/images/trash_icon.svg" />
</button>
</div>
<div class="input-group">
<input class="label-input" type="text" placeholder="Type text here..." />
<button class="delete-button">
<img src="./static/images/trash_icon.svg" />
</button>
</div>
<div class="input-group">
<input type="text" placeholder="<any>" class="block-input" />
<button class="delete-button">
<img src="./static/images/trash_icon.svg" />
</button>
</div>
<span id="placeholderMeasure" aria-hidden="true"></span>
</div>
<div id="icon-selector">
<div>Select Block Icon:</div>
<div id="icon-buttons"></div>
</div>
<div id="buttons">
<button id="add-label" onclick="addLabel()">Add Label</button>
<button id="add-input" onclick="addInput()">Add Input</button>
<button id="generate-json" onclick="generateJson()">Generate JSON</button>
</div>
<div id="json-actions" style="display: none; margin-top: 10px;">
<button id="copy-json" onclick="copyJson()">Copy JSON</button>
<button id="download-json" onclick="downloadJson()">Download JSON</button>
</div>
</main>
<footer>
This site is open source.
<a href="https://github.com/TRC-Loop/CatWebTools" target="_blank">Improve this page</a>.
This page is not affiliated with Catweb. | Version:
<script src="./static/version.js"></script>
<a href="changelog" target="_blank"> View Changelog</a>.
</footer>
<script>
let inputs = document.getElementsByClassName("block-input");
let labelInputs = document.getElementsByClassName("label-input");
const measure = document.getElementById("placeholderMeasure");
const block = document.getElementById("block");
// Global variable to store generated JSON
let generatedJson = "";
// Function to adjust input widths
function adjustInputWidths() {
Array.from(inputs).forEach((input) => {
const value = input.value || input.placeholder;
measure.textContent = value;
const width = measure.offsetWidth;
input.style.width = width + "px";
});
Array.from(labelInputs).forEach((input) => {
const value = input.value || input.placeholder;
measure.textContent = value;
const width = measure.offsetWidth;
input.style.width = width + 5 + "px";
});
}
// Function to add delete button functionality
function addDeleteButtonFunctionality(button) {
button.addEventListener("click", function () {
const group = button.closest(".input-group");
if (group) {
group.remove();
adjustInputWidths();
}
});
}
// Attach delete functionality to existing buttons
document.querySelectorAll(".delete-button").forEach(addDeleteButtonFunctionality);
// Function to add new input
function addInput() {
const placeholderText = prompt(
"Enter placeholder text for the new input. There will be <>'s around your input:",
""
);
if (placeholderText === null) return;
const group = document.createElement("div");
group.className = "input-group";
const newInput = document.createElement("input");
newInput.className = "block-input";
newInput.type = "text";
newInput.placeholder = "<" + placeholderText + ">";
newInput.addEventListener("input", adjustInputWidths);
const delBtn = document.createElement("button");
delBtn.className = "delete-button";
delBtn.innerHTML = `<img src="./static/images/trash_icon.svg">`;
addDeleteButtonFunctionality(delBtn);
group.appendChild(newInput);
group.appendChild(delBtn);
block.appendChild(group);
adjustInputWidths();
}
// Function to add new label
function addLabel() {
const group = document.createElement("div");
group.className = "input-group";
const newInput = document.createElement("input");
newInput.className = "label-input";
newInput.type = "text";
newInput.placeholder = "Type text here...";
newInput.addEventListener("input", adjustInputWidths);
const delBtn = document.createElement("button");
delBtn.className = "delete-button";
delBtn.innerHTML = `<img src="./static/images/trash_icon.svg">`;
addDeleteButtonFunctionality(delBtn);
group.appendChild(newInput);
group.appendChild(delBtn);
block.appendChild(group);
adjustInputWidths();
}
// Function to generate JSON
function generateJson() {
const blockChildren = block.querySelectorAll(".input-group");
// Map for icon filenames to IDs
const iconMap = {
"1.png": "0",
"2.png": "3",
"3.png": "24",
"4.png": "8",
"5.png": "58",
"6.png": "4",
"7.png": "5",
"8.png": "11",
"9.png": "32",
"10.png": "68",
"11.png": "84",
"12.png": "34",
"13.png": "42",
"14.png": "54",
"15.png": "63",
};
// Get current icon filename
const currentIconImg = block.querySelector("img");
const srcParts = currentIconImg.src.split("/");
const iconFilename = srcParts[srcParts.length - 1];
const iconId = iconMap[iconFilename] || null;
const blockText = [];
// Loop through input groups
blockChildren.forEach((group) => {
const input = group.querySelector("input");
if (!input) return;
if (input.classList.contains("label-input")) {
const value = input.value || input.placeholder;
blockText.push(value);
} else if (input.classList.contains("block-input")) {
const placeholder = input.placeholder || "";
const stripped = placeholder.substring(1, placeholder.length - 1);
blockText.push({ l: stripped, t: "string" });
}
});
// Build the action object
const blockObject = {
id: iconId,
text: blockText,
globalid: "custom",
t: 0,
};
// Final output structure
const finalOutput = [
{
class: "script",
content: [
{
y: "4838",
x: "4816.9921875",
globalid: "mx",
id: "0",
text: ["When website loaded..."],
actions: [blockObject],
},
],
globalid: "custom",
},
];
// Convert to JSON string
generatedJson = JSON.stringify(finalOutput, null, 2);
console.log(generatedJson);
// Show the copy/download buttons container
const jsonActionsDiv = document.getElementById("json-actions");
jsonActionsDiv.style.display = "block";
}
// Function to copy JSON to clipboard
function copyJson() {
if (!generatedJson) {
alert("Please generate JSON first!");
return;
}
navigator.clipboard
.writeText(generatedJson)
.then(() => alert("JSON copied to clipboard!"))
.catch((err) => console.error("Failed to copy JSON:", err));
}
// Function to download JSON as file
function downloadJson() {
if (!generatedJson) {
alert("Please generate JSON first!");
return;
}
const blob = new Blob([generatedJson], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "custom-script.json";
a.click();
URL.revokeObjectURL(url);
}
// Initial adjustment on page load
adjustInputWidths();
// Live update on input
Array.from(inputs).forEach((input) => {
input.addEventListener("input", adjustInputWidths);
});
Array.from(labelInputs).forEach((input) => {
input.addEventListener("input", adjustInputWidths);
});
// Function to create icon buttons
function createIconButtons() {
const iconButtonsContainer = document.getElementById("icon-buttons");
for (let i = 1; i <= 15; i++) {
const button = document.createElement("button");
button.className = "icon-button";
button.innerHTML = `<img src="./static/images/${i}.png" width="24" height="24" alt="Icon ${i}">`;
button.addEventListener("click", () => {
const blockImg = block.querySelector("img");
blockImg.src = `./static/images/${i}.png`;
});
iconButtonsContainer.appendChild(button);
}
}
createIconButtons();
</script>
</body>
</html>