Skip to content

Commit

Permalink
Embed esp_web_flasher (tasmota#965)
Browse files Browse the repository at this point in the history
* add anchor points for web flasher

* Create web_flasher.js
  • Loading branch information
Staars authored Apr 5, 2022
1 parent 1274557 commit 7658da1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
17 changes: 8 additions & 9 deletions docs/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,17 @@ If everything went well, you are now in Programming Mode and ready to continue w

If you have followed [Hardware preparation](#hardware-preparation), your device should be in _Programming Mode_ and ready for a Tasmota firmware binary to be installed.

!!! tip "You may want to back up the device manufacturer's firmware on the one in a million chance you don't like Tasmota."
!!! tip "You may want to back up the device manufacturer's firmware on the one in a million chance you don't like Tasmota."

<script src="../extra_javascript/web_flasher.js"></script>
<!-- Hard coded hack to get the postion in tabbed set after that. Must be placed before the tabbed set. Tabbed set MUST not be altered in its structure!! -->
<span id='web_installer'>


Choose an installation method:

=== "Web Installer :material-google-chrome:"
With a Chrome based browser open [https://tasmota.github.io/install](https://tasmota.github.io/install) and follow the instructions.

![Web Installer](_media/web_installer.jpg)

Tasmota Web Installer will install an appropriate build for your device.

![Web install in progress](_media/web_installer_3.jpg)![Web install done](_media/web_installer_4.jpg)
Embedded Web app:


=== "Tasmotizer! :material-linux: :material-apple: :material-microsoft-windows:"
Expand Down Expand Up @@ -246,7 +245,7 @@ Choose an installation method:
- [**Sonoff DIY**](Sonoff-DIY) - OTA flash for select Sonoff devices **Does not work anymore**
- [**esp2ino**](https://github.com/elahd/esp2ino) - OTA flash for select Wyze devices. **Does not work anymore**

You've successfully flashed your device with Tasmota but now you need to connect the freshly tasmotised device to your Wi-Fi network.


## Initial Configuration

Expand Down
103 changes: 103 additions & 0 deletions docs/extra_javascript/web_flasher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
ewt_s=document.createElement('script');ewt_s.type='module';
ewt_s.src="https://unpkg.com/[email protected]/dist/web/install-button.js?module";
document.body.append(ewt_s);

ewt_b=document.createElement("esp-web-install-button");ewt_b.manifest="https://tasmota.github.io/install/manifest/release.tasmota.manifest.json";

fw_sel=document.createElement('select');
fw_sel.id ='pick-variant';

fetch('https://tasmota.github.io/install/manifests_new.json')
.then(response => response.json())
.then(data => make_select(data));


function make_select(data){
// console.log(data);
for (opt_group in data){
var og=document.createElement('optgroup');
og.label = opt_group;
fw_sel.appendChild(og);
console.log(opt_group,data[opt_group]);
for (fw in data[opt_group]){
var opt=document.createElement('option');
opt.label = data[opt_group][fw]["name"];
opt.value = data[opt_group][fw]["path"];
console.log( opt.value);
og.appendChild(opt);
}
}
appendButtonInTable();
}


function appendButtonInTable(){
//Step1: add web flasher button to DOM
const anchor_point =document.getElementById("web_installer").parentElement.nextElementSibling.nextElementSibling.children[5].firstElementChild.firstElementChild;
anchor_point.parentNode.append(ewt_b);
let bNode = document.getElementById("web_installer").parentElement.nextElementSibling.nextElementSibling.children[5].firstElementChild.firstElementChild.nextElementSibling
console.log(bNode);
let observer = new IntersectionObserver(function (entries, observer) {
if(entries[0].target==ewt_b);
appendSelectorInTable(1); //first try, may fail in some cases
observer.disconnect();
});
observer.observe(bNode, {
childList: true,
subtree: false
// characterDataOldValue: true
});
}

var waitForDialogTimer;
function waitForDialog(){
//I apologize for this solution...
var _ewt = document.querySelector("ewt-install-dialog");
if(_ewt){
_ewt.style.cssText = "--mdc-dialog-max-width:900px;";
clearInterval(waitForDialogTimer);
}
}

var secondTryTimer;
function secondTry(){
console.log("One more time ...");
clearInterval(secondTryTimer);
appendSelectorInTable(2);
}

function appendSelectorInTable(attempt) {
//step2: check result of attempt to add button to DOM
try{
const button = document.querySelector("esp-web-install-button");
console.log("Button status:",button.shadowRoot.firstChild.name);
if(button.shadowRoot.firstChild.name == 'activate'){
//success: add the select picker and some info
button.insertAdjacentHTML('beforebegin','<p>1. Connect the ESP device to your computer using USB or serial-to-USB adapter</p><p>2. Select the firmware variant suitable for your device</p>');
button.insertAdjacentElement('beforebegin',fw_sel);
button.insertAdjacentHTML('beforebegin','<p>3. Hit "CONNECT" and select the correct port or find help if no device found</p>');
const selectEl = document.querySelector("#pick-variant");
button.manifest = selectEl.value;
selectEl.addEventListener("change", () => {
button.manifest = selectEl.value;
console.log(button.manifest);
});
button.addEventListener("click", () => {
console.log("Wait for dialog to set max-width ...");
waitForDialogTimer = setInterval(waitForDialog, 500);
});
// stop mkdocs keyboard shortcuts
document.addEventListener('keydown', (e) => {
e.stopPropagation();
});
}
}
catch(e){
if(attempt==1){
secondTryTimer = setInterval(secondTry, 250);
}
const anchor_point =document.getElementById("web_installer").parentElement.nextElementSibling.nextElementSibling.children[5].firstElementChild.firstElementChild;
anchor_point.insertAdjacentHTML('afterbegin','<p>Unsupported platform/browser. Use another flashing method.</p>');
console.log(e);
}
}

0 comments on commit 7658da1

Please sign in to comment.