1
0
mirror of https://github.com/s00500/ESPUI.git synced 2025-11-27 23:03:16 +00:00

Add ability to set custom js at /js/custom.js

This commit is contained in:
Mike Burton
2025-11-18 08:01:10 -08:00
parent 4e32746159
commit 47c4430a86
2 changed files with 21 additions and 0 deletions

View File

@@ -17,6 +17,15 @@
#include <umm_malloc/umm_heap_select.h>
#endif
static const char* customJS = nullptr;
// Set custom JavaScript to be included in the UI.
// js: JavaScript code as a String or C-string. Must remain valid for the lifetime of the ESPUIClass instance.
void ESPUIClass::setCustomJS(const char* js)
{
customJS = js;
}
static String heapInfo(const __FlashStringHelper* mode)
{
String result;
@@ -1272,6 +1281,15 @@ void ESPUIClass::begin(const char* _title, const char* username, const char* pas
yield();
});
server->on("/js/custom.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
request->send(200, "application/javascript", customJS ? customJS : "");
});
server->begin();
#if defined(DEBUG_ESPUI)

View File

@@ -201,6 +201,9 @@ public:
void updateVisibility(uint16_t id, bool visibility, int clientId = -1);
// Set optional global custom JavaScript to be included in the UI.
void setCustomJS(const char* js);
// Variables
const char* ui_title = "ESPUI"; // Store UI Title and Header Name
Control* controls = nullptr;