Skip to content

Commit 8e4c612

Browse files
dmadisonAndreas Breitschopp
and
Andreas Breitschopp
authored
Add user custom header/footer functions (#1769)
* Added setCustomTopBodyElement allowing to define custom HTML to be added at the top of the "<body>" tag. * Change custom top body element to 'header' This will make more sense once a footer is added. No functional changes. * Make custom header concat conditional Matching the _bodyClass comparison above * Create getHTTPEnd function * Add custom body footer functionality * Fix <small> close tag on update page * Add missing HTTP ends to exit and close * Remove 'immediately' from header/footer desc It's not immediate, the 'wrap' div is added before/after. --------- Co-authored-by: Andreas Breitschopp <[email protected]>
1 parent 7e2fa84 commit 8e4c612

5 files changed

+60
-15
lines changed

WiFiManager.cpp

+46-10
Original file line numberDiff line numberDiff line change
@@ -1290,9 +1290,23 @@ String WiFiManager::getHTTPHead(String title, String classes){
12901290
p.replace(FPSTR(T_c), classes);
12911291
page += p;
12921292

1293+
if (_customBodyHeader) {
1294+
page += _customBodyHeader;
1295+
}
1296+
12931297
return page;
12941298
}
12951299

1300+
String WiFiManager::getHTTPEnd() {
1301+
String end = FPSTR(HTTP_END);
1302+
1303+
if (_customBodyFooter) {
1304+
end = String(_customBodyFooter) + end;
1305+
}
1306+
1307+
return end;
1308+
}
1309+
12961310
void WiFiManager::HTTPSend(const String &content){
12971311
server->send(200, FPSTR(HTTP_HEAD_CT), content);
12981312
}
@@ -1341,7 +1355,7 @@ void WiFiManager::handleRoot() {
13411355
page += FPSTR(HTTP_PORTAL_OPTIONS);
13421356
page += getMenuOut();
13431357
reportStatus(page);
1344-
page += FPSTR(HTTP_END);
1358+
page += getHTTPEnd();
13451359

13461360
HTTPSend(page);
13471361
if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async
@@ -1397,7 +1411,7 @@ void WiFiManager::handleWifi(boolean scan) {
13971411
page += FPSTR(HTTP_SCAN_LINK);
13981412
if(_showBack) page += FPSTR(HTTP_BACKBTN);
13991413
reportStatus(page);
1400-
page += FPSTR(HTTP_END);
1414+
page += getHTTPEnd();
14011415

14021416
HTTPSend(page);
14031417

@@ -1426,7 +1440,7 @@ void WiFiManager::handleParam(){
14261440
page += FPSTR(HTTP_FORM_END);
14271441
if(_showBack) page += FPSTR(HTTP_BACKBTN);
14281442
reportStatus(page);
1429-
page += FPSTR(HTTP_END);
1443+
page += getHTTPEnd();
14301444

14311445
HTTPSend(page);
14321446

@@ -1888,7 +1902,7 @@ void WiFiManager::handleWifiSave() {
18881902
}
18891903

18901904
if(_showBack) page += FPSTR(HTTP_BACKBTN);
1891-
page += FPSTR(HTTP_END);
1905+
page += getHTTPEnd();
18921906

18931907
server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL)); // @HTTPHEAD send cors
18941908
HTTPSend(page);
@@ -1915,7 +1929,7 @@ void WiFiManager::handleParamSave() {
19151929
String page = getHTTPHead(FPSTR(S_titleparamsaved), FPSTR(C_param)); // @token titleparamsaved
19161930
page += FPSTR(HTTP_PARAMSAVED);
19171931
if(_showBack) page += FPSTR(HTTP_BACKBTN);
1918-
page += FPSTR(HTTP_END);
1932+
page += getHTTPEnd();
19191933

19201934
HTTPSend(page);
19211935

@@ -2072,7 +2086,7 @@ void WiFiManager::handleInfo() {
20722086
if(_showInfoErase) page += FPSTR(HTTP_ERASEBTN);
20732087
if(_showBack) page += FPSTR(HTTP_BACKBTN);
20742088
page += FPSTR(HTTP_HELP);
2075-
page += FPSTR(HTTP_END);
2089+
page += getHTTPEnd();
20762090

20772091
HTTPSend(page);
20782092

@@ -2325,6 +2339,7 @@ void WiFiManager::handleExit() {
23252339
handleRequest();
23262340
String page = getHTTPHead(FPSTR(S_titleexit), FPSTR(C_exit)); // @token titleexit
23272341
page += FPSTR(S_exiting); // @token exiting
2342+
page += getHTTPEnd();
23282343
// ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
23292344
server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); // @HTTPHEAD send cache
23302345
HTTPSend(page);
@@ -2342,7 +2357,7 @@ void WiFiManager::handleReset() {
23422357
handleRequest();
23432358
String page = getHTTPHead(FPSTR(S_titlereset), FPSTR(C_restart)); //@token titlereset
23442359
page += FPSTR(S_resetting); //@token resetting
2345-
page += FPSTR(HTTP_END);
2360+
page += getHTTPEnd();
23462361

23472362
HTTPSend(page);
23482363

@@ -2377,7 +2392,7 @@ void WiFiManager::handleErase(boolean opt) {
23772392
#endif
23782393
}
23792394

2380-
page += FPSTR(HTTP_END);
2395+
page += getHTTPEnd();
23812396
HTTPSend(page);
23822397

23832398
if(ret){
@@ -2472,6 +2487,7 @@ void WiFiManager::handleClose(){
24722487
handleRequest();
24732488
String page = getHTTPHead(FPSTR(S_titleclose), FPSTR(C_close)); // @token titleclose
24742489
page += FPSTR(S_closing); // @token closing
2490+
page += getHTTPEnd();
24752491
HTTPSend(page);
24762492
}
24772493

@@ -2896,6 +2912,26 @@ void WiFiManager::setCustomHeadElement(const char* html) {
28962912
_customHeadElement = html;
28972913
}
28982914

2915+
/**
2916+
* set custom html at the top of the body
2917+
* custom element will be added after the body tag is opened, eg. to show a logo etc.
2918+
* @access public
2919+
* @param char element
2920+
*/
2921+
void WiFiManager::setCustomBodyHeader(const char* html) {
2922+
_customBodyHeader = html;
2923+
}
2924+
2925+
/**
2926+
* set custom html at the bottom of the body
2927+
* custom element will be added before the body tag is closed
2928+
* @access public
2929+
* @param char element
2930+
*/
2931+
void WiFiManager::setCustomBodyFooter(const char* html) {
2932+
_customBodyFooter = html;
2933+
}
2934+
28992935
/**
29002936
* set custom menu html
29012937
* custom element will be added to menu under custom menu item.
@@ -3882,7 +3918,7 @@ void WiFiManager::handleUpdate() {
38823918
page += str;
38833919

38843920
page += FPSTR(HTTP_UPDATE);
3885-
page += FPSTR(HTTP_END);
3921+
page += getHTTPEnd();
38863922

38873923
HTTPSend(page);
38883924

@@ -4004,7 +4040,7 @@ void WiFiManager::handleUpdateDone() {
40044040
page += FPSTR(HTTP_UPDATE_SUCCESS);
40054041
DEBUG_WM(F("[OTA] update ok"));
40064042
}
4007-
page += FPSTR(HTTP_END);
4043+
page += getHTTPEnd();
40084044

40094045
HTTPSend(page);
40104046

WiFiManager.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ class WiFiManager
373373
//add custom html at inside <head> for all pages
374374
void setCustomHeadElement(const char* html);
375375

376+
//add custom html at start of <body> for all pages
377+
void setCustomBodyHeader(const char* html);
378+
379+
//add custom html at end of <body> for all pages
380+
void setCustomBodyFooter(const char* html);
381+
376382
//if this is set, customise style
377383
void setCustomMenuHTML(const char* html);
378384

@@ -595,8 +601,10 @@ class WiFiManager
595601
boolean _disableConfigPortal = true; // FOR autoconnect - stop config portal if cp wifi save
596602
String _hostname = ""; // hostname for esp8266 for dhcp, and or MDNS
597603

598-
const char* _customHeadElement = ""; // store custom head element html from user isnide <head>
599-
const char* _customMenuHTML = ""; // store custom head element html from user inside <>
604+
const char* _customHeadElement = ""; // store custom head element html from user inside <head>
605+
const char* _customBodyHeader = ""; // store custom top body element html from user inside <body>
606+
const char* _customBodyFooter = ""; // store custom bottom body element html from user inside <body>
607+
const char* _customMenuHTML = ""; // store custom menu html from user
600608
String _bodyClass = ""; // class to add to body
601609
String _title = FPSTR(S_brand); // app title - default WiFiManager
602610

@@ -752,6 +760,7 @@ class WiFiManager
752760
String getScanItemOut();
753761
String getStaticOut();
754762
String getHTTPHead(String title, String classes = "");
763+
String getHTTPEnd();
755764
String getMenuOut();
756765
//helpers
757766
boolean isIp(String str);

wm_strings_en.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const char HTTP_HELP[] PROGMEM =
150150
const char HTTP_HELP[] PROGMEM = "";
151151
#endif
152152

153-
const char HTTP_UPDATE[] PROGMEM = "Upload new firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Update</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* May not function inside captive portal, open in browser http://192.168.4.1</a><small>";
153+
const char HTTP_UPDATE[] PROGMEM = "Upload new firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Update</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* May not function inside captive portal, open in browser http://192.168.4.1</a></small>";
154154
const char HTTP_UPDATE_FAIL[] PROGMEM = "<div class='msg D'><strong>Update failed!</strong><Br/>Reboot device and try again</div>";
155155
const char HTTP_UPDATE_SUCCESS[] PROGMEM = "<div class='msg S'><strong>Update successful. </strong> <br/> Device rebooting now...</div>";
156156

wm_strings_es.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const char HTTP_HELP[] PROGMEM =
157157
const char HTTP_HELP[] PROGMEM = "";
158158
#endif
159159

160-
const char HTTP_UPDATE[] PROGMEM = "Upload New Firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Update</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* May not function inside captive portal, Open in browser http://192.168.4.1</a><small>";
160+
const char HTTP_UPDATE[] PROGMEM = "Upload New Firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Update</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* May not function inside captive portal, Open in browser http://192.168.4.1</a></small>";
161161
const char HTTP_UPDATE_FAIL[] PROGMEM = "<div class='msg D'><strong>Update Failed!</strong><Br/>Reboot device and try again</div>";
162162
const char HTTP_UPDATE_SUCCESS[] PROGMEM = "<div class='msg S'><strong>Update Successful. </strong> <br/> Device Rebooting now...</div>";
163163

wm_strings_fr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const char HTTP_HELP[] PROGMEM =
150150
const char HTTP_HELP[] PROGMEM = "";
151151
#endif
152152

153-
const char HTTP_UPDATE[] PROGMEM = "Charger le nouveau firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Mise à jour</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* Peut ne pas fonctionner à l'intérieur du portail captif, ouvrir dans le navigateur http://192.168.4.1</a><small>";
153+
const char HTTP_UPDATE[] PROGMEM = "Charger le nouveau firmware<br/><form method='POST' action='u' enctype='multipart/form-data' onchange=\"(function(el){document.getElementById('uploadbin').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update' accept='.bin,application/octet-stream'><button id='uploadbin' type='submit' class='h D'>Mise à jour</button></form><small><a href='http://192.168.4.1/update' target='_blank'>* Peut ne pas fonctionner à l'intérieur du portail captif, ouvrir dans le navigateur http://192.168.4.1</a></small>";
154154
const char HTTP_UPDATE_FAIL[] PROGMEM = "<div class='msg D'><strong>Echec de la mise à jour !</strong><Br/>Redémarrer l'appareil et réessayer</div>";
155155
const char HTTP_UPDATE_SUCCESS[] PROGMEM = "<div class='msg S'><strong>Mise à jour réussie. </strong> <br/> L'appareil redémarre maintenant...</div>";
156156

0 commit comments

Comments
 (0)