esp-web-tools
Version:
Web tools for ESP devices
2 lines (1 loc) • 3.13 kB
JavaScript
const e=()=>"usb"in navigator&&(!("serial"in navigator)||/Android/.test(navigator.userAgent));let t;const o=()=>null!=t?t:t=import("./index-rScOpSrP.js").then((function(e){return e.i})),n=async t=>{let r;import("./install-dialog-ChRWE6GU.js");try{r=await(async()=>{if(!e())return navigator.serial.requestPort();const{requestUsbSerialPort:t}=await o();return t()})()}catch(e){return"NotFoundError"===e.name?void import("./no-port-picked-DPUnbpty.js").then((e=>e.openNoPortPickedDialog((()=>n(t))))):void alert(`Error: ${e.message}`)}if(!r)return;try{await r.open({baudRate:115200,bufferSize:8192})}catch(e){return void alert(e.message)}const s=document.createElement("ewt-install-dialog");s.port=r,s.manifestPath=t.manifest||t.getAttribute("manifest"),s.overrides=t.overrides,s.addEventListener("closed",(()=>{r.close()}),{once:!0}),document.body.appendChild(s)};class r extends HTMLElement{connectedCallback(){if(this.renderRoot)return;if(this.renderRoot=this.attachShadow({mode:"open"}),!r.isSupported||!r.isAllowed)return this.toggleAttribute("install-unsupported",!0),void(this.renderRoot.innerHTML=r.isAllowed?"<slot name='unsupported'>Your browser does not support installing things on ESP devices. Use Mozilla Firefox, Google Chrome, Microsoft Edge or Google Chrome on Android.</slot>":"<slot name='not-allowed'>You can only install ESP devices on HTTPS websites or on the localhost.</slot>");this.toggleAttribute("install-supported",!0),e()&&o().catch((()=>{t=void 0}));const s=document.createElement("slot");s.addEventListener("click",(async e=>{e.preventDefault(),n(this)})),s.name="activate";const i=document.createElement("button");if(i.innerText="Connect",s.append(i),"adoptedStyleSheets"in Document.prototype&&"replaceSync"in CSSStyleSheet.prototype){const e=new CSSStyleSheet;e.replaceSync(r.style),this.renderRoot.adoptedStyleSheets=[e]}else{const e=document.createElement("style");e.innerText=r.style,this.renderRoot.append(e)}this.renderRoot.append(s)}}r.isSupported="serial"in navigator||"usb"in navigator,r.isAllowed=window.isSecureContext,r.style='\n button {\n position: relative;\n cursor: pointer;\n font-size: 14px;\n font-weight: 500;\n padding: 10px 24px;\n color: var(--esp-tools-button-text-color, #fff);\n background-color: var(--esp-tools-button-color, #03a9f4);\n border: none;\n border-radius: var(--esp-tools-button-border-radius, 9999px);\n }\n button::before {\n content: " ";\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n opacity: 0.2;\n border-radius: var(--esp-tools-button-border-radius, 9999px);\n }\n button:hover::before {\n background-color: rgba(255,255,255,.8);\n }\n button:focus {\n outline: none;\n }\n button:focus::before {\n background-color: white;\n }\n button:active::before {\n background-color: grey;\n }\n :host([active]) button {\n color: rgba(0, 0, 0, 0.38);\n background-color: rgba(0, 0, 0, 0.12);\n box-shadow: none;\n cursor: unset;\n pointer-events: none;\n }\n .hidden {\n display: none;\n }',customElements.define("esp-web-install-button",r);