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);
}