diff --git a/src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js b/src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js index abbb760..8fc0a18 100644 --- a/src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js +++ b/src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js @@ -9,26 +9,33 @@ class Nightscout { this.context = context; this.url = ""; this.request = { headers: {} }; - this.settings = settings; + this.setSettings(settings); nightscoutMap[context] = this; } - set settings(settings) { - this._settings = settings; + async setSettings(settings) { + this.settings = settings; if (this.settings.nightscoutUrl) { - this.url = new URL(this.settings.nightscoutUrl); - this.url.pathname += "api/v2/properties"; if (this.settings.token) { - this.request.headers["Api-Secret"] = this.settings.token; + try { + const buffer = new TextEncoder().encode(this.settings.token); + const hashBuffer = await crypto.subtle.digest("SHA-1", buffer); + const hashArray = Array.from(new Uint8Array(hashBuffer)); + this.request.headers["Api-Secret"] = hashArray + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + } catch (err) { + console.error(err); + this.request.headers["Api-Secret"] = this.settings.token; + $SD.showAlert(this.context); + } } + this.url = new URL(this.settings.nightscoutUrl); + this.url.pathname += "api/v2/properties"; this.beginTick(); } } - get settings() { - return this._settings; - } - async tick() { if (!this.url) { return; diff --git a/src/com.gabe565.nightscout.sdPlugin/js/app.js b/src/com.gabe565.nightscout.sdPlugin/js/app.js index 1caed1b..6cf96b5 100644 --- a/src/com.gabe565.nightscout.sdPlugin/js/app.js +++ b/src/com.gabe565.nightscout.sdPlugin/js/app.js @@ -4,8 +4,8 @@ const nightscoutAction = new Action("com.gabe565.nightscout.action"); -nightscoutAction.onDidReceiveSettings( - (data) => (new Nightscout(data).settings = data.payload.settings), +nightscoutAction.onDidReceiveSettings((data) => + new Nightscout(data).setSettings(data.payload.settings), ); nightscoutAction.onKeyDown((data) => new Nightscout(data).beginTick());