From 40a6475a1c6c937f9b43e40ef44029ba15b8b093 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Mon, 10 Jun 2024 23:42:18 +0300 Subject: [PATCH] Fix WebREPL --- ViperIDE.html | 43 +++++++++++++++++++++++++++++++------------ webrepl_content.js | 2 +- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ViperIDE.html b/ViperIDE.html index bd61478..2e4a4e8 100644 --- a/ViperIDE.html +++ b/ViperIDE.html @@ -496,6 +496,10 @@

ViperIDE

this.receivedData = '' } + async requestAccess() { + throw new Error("Method 'requestAccess()' must be implemented.") + } + async connect() { throw new Error("Method 'connect()' must be implemented.") } @@ -586,8 +590,11 @@

ViperIDE

} } - async connect() { + async requestAccess() { this.port = await this.serial.requestPort() + } + + async connect() { await this.port.open({ baudRate: 115200 }) this.reader = this.port.readable.getReader() @@ -644,7 +651,7 @@

ViperIDE

} } - async connect() { + async requestAccess() { this.device = await navigator.bluetooth.requestDevice({ filters: [{ //services: [NUS_SERVICE], @@ -653,6 +660,9 @@

ViperIDE

//acceptAllDevices: true, optionalServices: [NUS_SERVICE], }) + } + + async connect() { this.server = await this.device.gatt.connect() this.service = await this.server.getPrimaryService(NUS_SERVICE) this.rx = await this.service.getCharacteristic(NUS_RX) @@ -697,16 +707,18 @@

ViperIDE

this.socket = null } - _connect(url) { - return new Promise(function(resolve, reject) { - const ws = new WebSocket(url) - ws.onopen = function() { resolve(ws) } - ws.onerror = function(err) { reject(err) } - }) + async requestAccess() { } async connect() { - this.socket = await this._connect(this.url) + function _conn(url) { + return new Promise(function(resolve, reject) { + const ws = new WebSocket(url) + ws.onopen = function() { resolve(ws) } + ws.onerror = function(err) { reject(err) } + }) + } + this.socket = await _conn(this.url) this.socket.binaryType = 'arraybuffer' this.socket.onmessage = (event) => { if (this.receiveCallback) { @@ -793,7 +805,7 @@

ViperIDE

url = prompt('WebREPL device address:', '192.168.1.123:8266') if (!url) return; - if (!url.contains("://")) { url = "ws://" + url } + if (!url.includes("://")) { url = "ws://" + url } if (window.location.protocol === "https:") { /* Navigate to device, which should automatically reload and ask for WebREPL password */ @@ -804,8 +816,9 @@

ViperIDE

url = webrepl_url } const pass = prompt('Password:') - if (!pass || pass.length < 4) { - toastr.error('Password is required') + if (pass == null) { return } + if (pass.length < 4) { + toastr.error('Password is too short') return } await disconnect() @@ -846,6 +859,12 @@

ViperIDE

return } + try { + await port.requestAccess() + } catch { + return + } + try { await port.connect() } catch(error) { diff --git a/webrepl_content.js b/webrepl_content.js index 67acbef..928c1bb 100644 --- a/webrepl_content.js +++ b/webrepl_content.js @@ -6,7 +6,7 @@ } window.webrepl_url = 'ws://' + url - fetch('ViperIDE.html') + fetch('index.html') .then(rsp => rsp.text() ) .then(data => document.write(data)) })();