Skip to content

Commit 3bb646b

Browse files
committed
Internal - Now webui_script() supports Binary
Example: "return new Uint8Array([0xAA, 0x00, 0xBB]);"
1 parent 0212787 commit 3bb646b

File tree

4 files changed

+196
-112
lines changed

4 files changed

+196
-112
lines changed

bridge/js2c.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function jsToCHeader(inputFilename, outputFilename) {
2020
// Convert each character in JS content to its hexadecimal value
2121
const hexValues = [...newContent].map(char => `0x${char.charCodeAt(0).toString(16).padStart(2, '0')}`);
2222
// Prepare the content for the C header file
23-
let headerContent = `${comment}// --- PLEASE DO NOT EDIT THIS FILE -------\n// --- THIS FILE IS GENERATED BY JS2C.PY --\n\n`;
23+
let headerContent = `${comment}// --- PLEASE DO NOT EDIT THIS FILE -------\n// --- THIS FILE IS GENERATED BY JS2C.JS --\n\n`;
2424
headerContent += `#ifndef WEBUI_BRIDGE_H\n#define WEBUI_BRIDGE_H\n`;
2525
headerContent += `unsigned char webui_javascript_bridge[] = { `;
2626
// Split the hexadecimal values to make the output more readable, adding a new line every 10 values

bridge/webui.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,17 @@ class WebuiBridge {
676676
const scriptSanitize = script.replace(/(?:\r\n|\r|\n)/g, '\n');
677677
if (this.#log) console.log(`WebUI -> CMD -> JS [${scriptSanitize}]`);
678678
// Get callback result
679-
let FunReturn = 'undefined';
679+
let FunReturn: string | Uint8Array = 'undefined';
680680
let FunError = false;
681+
let isBinaryReturn = false;
681682
try {
682-
FunReturn = await AsyncFunction(scriptSanitize)();
683+
const result = await AsyncFunction(scriptSanitize)();
684+
if (result instanceof Uint8Array) {
685+
FunReturn = result;
686+
isBinaryReturn = true;
687+
} else {
688+
FunReturn = String(result);
689+
}
683690
} catch (e) {
684691
FunError = true;
685692
FunReturn = e.message;
@@ -691,8 +698,20 @@ class WebuiBridge {
691698
FunReturn = 'undefined';
692699
}
693700
// Logging
694-
if (this.#log && !FunError) console.log(`WebUI -> CMD -> JS -> Return Success [${FunReturn}]`);
695-
if (this.#log && FunError) console.log(`WebUI -> CMD -> JS -> Return Error [${FunReturn}]`);
701+
if (this.#log && !FunError) {
702+
if (isBinaryReturn) {
703+
const binaryData = FunReturn as Uint8Array;
704+
const hexPreview = Array.from(binaryData.slice(0, 64)).map(b => `0x${b.toString(16).padStart(2, '0')}`).join(', ');
705+
console.log(`WebUI -> CMD -> JS -> Return Success (${binaryData.length} Bytes) [${hexPreview}${binaryData.length > 64 ? '...' : ''}]`);
706+
} else {
707+
const stringData = String(FunReturn);
708+
console.log(`WebUI -> CMD -> JS -> Return Success (${new TextEncoder().encode(stringData).length} Bytes) [${stringData.substring(0, 64)}${stringData.length > 64 ? '...' : ''}]`);
709+
}
710+
}
711+
else if (this.#log && FunError) {
712+
const errorString = String(FunReturn);
713+
console.log(`WebUI -> CMD -> JS -> Return Error [${errorString.substring(0, 64)}${errorString.length > 64 ? '...' : ''}]`);
714+
}
696715
// Protocol
697716
// 0: [SIGNATURE]
698717
// 1: [TOKEN]
@@ -724,7 +743,11 @@ class WebuiBridge {
724743
packetPush(new Uint8Array([0, 0])); // ID (2 Bytes)
725744
packetPush(new Uint8Array([this.#CMD_JS]));
726745
packetPush(new Uint8Array(FunError ? [1] : [0]));
727-
packetPushStr(FunReturn);
746+
if (isBinaryReturn) {
747+
packetPush(FunReturn as Uint8Array);
748+
} else {
749+
packetPushStr(FunReturn as string);
750+
}
728751
packetPush(new Uint8Array([0]));
729752
this.#addToken(packet, this.#token, this.#PROTOCOL_TOKEN);
730753
this.#addID(packet, callId, this.#PROTOCOL_ID);

0 commit comments

Comments
 (0)