diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 68b1fef..d4c8d65 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -17,6 +17,15 @@ #include #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) diff --git a/src/ESPUI.h b/src/ESPUI.h index 0e7bcc4..771b401 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -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;