Create utils/discohookts
Browse files- utils/discohookts +41 -0
utils/discohookts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function getJsonFromURL(url: string) {
|
| 2 |
+
// Get data from url
|
| 3 |
+
const urlObj = new URL(url);
|
| 4 |
+
const dataParam = urlObj.searchParams.get("data");
|
| 5 |
+
if (!dataParam) {
|
| 6 |
+
return undefined;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
// Convert URL-safe base64 to standard base64
|
| 10 |
+
const base64String = dataParam.replace(/-/g, "+").replace(/_/g, "/");
|
| 11 |
+
|
| 12 |
+
// Add padding if missing
|
| 13 |
+
const pad = base64String.length % 4;
|
| 14 |
+
const paddedBase64 = pad ? base64String + "=".repeat(4 - pad) : base64String;
|
| 15 |
+
|
| 16 |
+
// Decode base64 to Uint8Array
|
| 17 |
+
function base64ToUint8Array(base64: string) {
|
| 18 |
+
const binary = atob(base64);
|
| 19 |
+
const bytes = new Uint8Array(binary.length);
|
| 20 |
+
for (let i = 0; i < binary.length; i++) {
|
| 21 |
+
bytes[i] = binary.charCodeAt(i);
|
| 22 |
+
}
|
| 23 |
+
return bytes;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
const bytes = base64ToUint8Array(paddedBase64);
|
| 27 |
+
|
| 28 |
+
// Decode UTF-8 bytes to string
|
| 29 |
+
const decodedJson = new TextDecoder("utf-8").decode(bytes);
|
| 30 |
+
|
| 31 |
+
const parsedData = JSON.parse(decodedJson);
|
| 32 |
+
|
| 33 |
+
if (parsedData.messages && parsedData.messages.length > 0) {
|
| 34 |
+
const embeds = parsedData.messages[0].data.embeds;
|
| 35 |
+
const content = parsedData.messages[0].data.content;
|
| 36 |
+
return {
|
| 37 |
+
embeds: embeds,
|
| 38 |
+
content: content,
|
| 39 |
+
};
|
| 40 |
+
}
|
| 41 |
+
}
|