From 98825d1d72fbd6db58eba7b066b510e949d75c21 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 4 May 2022 15:00:42 +0200 Subject: [PATCH 1/4] feat: dispatch JS events when webln is ready and a payment for a lightning link succeeded --- src/extension/inpage-script/index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/extension/inpage-script/index.js b/src/extension/inpage-script/index.js index 4a5046500e..0443d04218 100644 --- a/src/extension/inpage-script/index.js +++ b/src/extension/inpage-script/index.js @@ -3,6 +3,9 @@ import WebLNProvider from "../ln/webln"; if (document) { window.webln = new WebLNProvider(); + const readyEvent = new Event("webln:ready"); + document.dispatchEvent(readyEvent); + // Intercept any `lightning:` requests window.addEventListener("click", (ev) => { const target = ev.target; @@ -15,6 +18,7 @@ if (document) { let href; let paymentRequest; let lnurl; + let link; // used to dispatch a succcess event if (!lightningLink && !bitcoinLinkWithLighting && !lnurlLink) { return; @@ -24,13 +28,16 @@ if (document) { if (lightningLink) { href = lightningLink.getAttribute("href").toLowerCase(); paymentRequest = href.replace("lightning:", ""); + link = lightningLink; } else if (bitcoinLinkWithLighting) { href = bitcoinLinkWithLighting.getAttribute("href"); + link = bitcoinLinkWithLighting; const url = new URL(href); const query = new URLSearchParams(url.search); paymentRequest = query.get("lightning"); } else if (lnurlLink) { href = lnurlLink.getAttribute("href").toLowerCase(); + link = lnurlLink; lnurl = href.replace(/^lnurl[pwc]:/i, ""); } @@ -53,6 +60,7 @@ if (document) { return; } if (lnurl) { + // TODO: dispatch success event return window.webln.lnurl(lnurl).catch((e) => { console.log(e); alert(`Error: ${e.message}`); @@ -61,8 +69,12 @@ if (document) { return window.webln .sendPayment(paymentRequest) .then((r) => { - console.log(r); - //alert(JSON.stringify(r)); + const responseEvent = new CustomEvent("lightning:success", { + paymentRequest: paymentRequest, + response: r, + }); + document.dispatchEvent(responseEvent); + link.dispatchEvent(responseEvent); }) .catch((e) => { console.log(e); From 24bb378f4b693af51b483d7b63a47d56b7bcbfc5 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 10 May 2022 15:39:13 +0200 Subject: [PATCH 2/4] feat: bubble lightning:success events --- src/extension/inpage-script/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/extension/inpage-script/index.js b/src/extension/inpage-script/index.js index 0443d04218..0206484f05 100644 --- a/src/extension/inpage-script/index.js +++ b/src/extension/inpage-script/index.js @@ -70,10 +70,12 @@ if (document) { .sendPayment(paymentRequest) .then((r) => { const responseEvent = new CustomEvent("lightning:success", { - paymentRequest: paymentRequest, - response: r, + bubbles: true, + detail: { + paymentRequest: paymentRequest, + response: r, + }, }); - document.dispatchEvent(responseEvent); link.dispatchEvent(responseEvent); }) .catch((e) => { From 1a98be1864bb6291a273f8a4a7592d83787f205d Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 5 Jun 2022 18:55:52 +0200 Subject: [PATCH 3/4] feat: publish success events for lightning links --- src/extension/inpage-script/index.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/extension/inpage-script/index.js b/src/extension/inpage-script/index.js index 511b2a23f4..063c8ebd96 100644 --- a/src/extension/inpage-script/index.js +++ b/src/extension/inpage-script/index.js @@ -63,19 +63,31 @@ if (document) { } if (lnurl) { // TODO: dispatch success event - return window.webln.lnurl(lnurl).catch((e) => { - console.log(e); - alert(`Error: ${e.message}`); - }); + return window.webln + .lnurl(lnurl) + .catch((e) => { + console.log(e); + alert(`Error: ${e.message}`); + }) + .then((response) => { + const responseEvent = new CustomEvent("lightning:success", { + bubbles: true, + detail: { + lnurl, + response, + }, + }); + link.dispatchEvent(responseEvent); + }); } return window.webln .sendPayment(paymentRequest) - .then((r) => { + .then((response) => { const responseEvent = new CustomEvent("lightning:success", { bubbles: true, detail: { - paymentRequest: paymentRequest, - response: r, + paymentRequest, + response, }, }); link.dispatchEvent(responseEvent); From d2f93ef9262f52f4503675e93124ab4c1b94dbd6 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Mon, 6 Jun 2022 22:06:49 +0200 Subject: [PATCH 4/4] chore: use console.error to log errors --- src/extension/inpage-script/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/extension/inpage-script/index.js b/src/extension/inpage-script/index.js index 063c8ebd96..1b13adb640 100644 --- a/src/extension/inpage-script/index.js +++ b/src/extension/inpage-script/index.js @@ -66,8 +66,12 @@ if (document) { return window.webln .lnurl(lnurl) .catch((e) => { - console.log(e); - alert(`Error: ${e.message}`); + console.error(e); + if ( + ![ABORT_PROMPT_ERROR, USER_REJECTED_ERROR].includes(e.message) + ) { + alert(`Error: ${e.message}`); + } }) .then((response) => { const responseEvent = new CustomEvent("lightning:success", { @@ -93,7 +97,7 @@ if (document) { link.dispatchEvent(responseEvent); }) .catch((e) => { - console.log(e); + console.error(e); if (![ABORT_PROMPT_ERROR, USER_REJECTED_ERROR].includes(e.message)) { alert(`Error: ${e.message}`); }