copy to clipboard enhancement

If clipboard is a table it converts the json string to lua table format
This commit is contained in:
gononono64 committed 2025-05-24 18:31:36 -05:00
1 parent d4908042be
commit ee955c9b79
2 files changed
+33 -25

No files matched your search

+11 -19
View File
@@ -15,22 +15,8 @@
function isTableJSON(jsonString) {
try {
const parsed = JSON.parse(jsonString);
if (!Array.isArray(parsed)) {
return false;
}
if (parsed.length > 0) {
const firstKeys = Object.keys(parsed[0]);
for (let i = 0; i < parsed.length; i++) {
if (typeof parsed[i] !== 'object' || parsed[i] === null || Array.isArray(parsed[i])) {
return false;
}
if (Object.keys(parsed[i]).length !== firstKeys.length || !Object.keys(parsed[i]).every(key => firstKeys.includes(key))) {
return false
}
}
}
return true;
const parsed = JSON.parse(jsonString);
return parsed;
} catch (e) {
return false;
}
@@ -47,9 +33,15 @@
window.addEventListener("message", (event) => {
if (event?.data?.type === "copytoclipboard") {
var str = event.data.text;
// if (isTableJSON(str)) {
// str = JSON.stringify(JSON.parse(str), null, 2);
// }
// str = JSON.parse(str);
if (isTableJSON(str)) {
str = str.replace(/\[/g, '{');
str = str.replace(/\]/g, '}');
// replace : with =
str = str.replace(/:/g, '=');
// I need all indexes with quotes to be replaced with nothing but not affect any string values
str = str.replace(/"([^"]+)"=/g, '$1=');
}
copyToClipboard(str);
}
+22 -6
View File
@@ -9,12 +9,21 @@
<div id="root"></div>
<script type="module" src="./src/main.jsx"></script>
</body>
<script>
<script>
// dont judge me
function isTableJSON(jsonString) {
try {
const parsed = JSON.parse(jsonString);
return parsed;
} catch (e) {
return false;
}
}
const copyToClipboard = (str) => {
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
@@ -23,9 +32,16 @@
window.addEventListener("message", (event) => {
if (event?.data?.type === "copytoclipboard") {
var str = event.data.text;
// str = JSON.parse(str);
console.log("copying to clipboard", isTableJSON(str));
if (isTableJSON(str)) {
str = str.replace(/\[/g, '{');
str = str.replace(/\]/g, '}');
str = str.replace(/:/g, '=');
str = str.replace(/"([^"]+)"=/g, '$1=');
}
copyToClipboard(str);
}
}
});
</script>
</html>