From a7fe694b14636c2db278574161aa8689e01cc5e1 Mon Sep 17 00:00:00 2001 From: Radu-Cristian Popa Date: Tue, 7 Nov 2023 01:35:52 +0200 Subject: [PATCH] Trigger `load` and `monetization` events --- src/background/grantFlow.ts | 10 ++----- src/background/index.ts | 2 +- src/content/messageListener.ts | 35 ++++++++++------------ src/utils/polyfill.ts | 7 +++-- tsconfig.json | 54 +++++++++++++++++----------------- 5 files changed, 52 insertions(+), 56 deletions(-) diff --git a/src/background/grantFlow.ts b/src/background/grantFlow.ts index ff43e43a..cfc53268 100644 --- a/src/background/grantFlow.ts +++ b/src/background/grantFlow.ts @@ -111,6 +111,7 @@ export class PaymentFlowService { new URL(this.receivingPaymentPointerUrl).origin + '/incoming-payments', { walletAddress: this.receivingPaymentPointerUrl, + expiresAt: new Date(Date.now() + 6000 * 60 * 10).toISOString(), }, this.getHeaders(this.clientAuthToken), ) @@ -218,7 +219,6 @@ export class PaymentFlowService { } async getContinuationRequest() { - console.log() const continuationRequest = await this.axiosInstance.post( this.outgoingPaymentGrantData.continue.uri, { @@ -299,12 +299,8 @@ export class PaymentFlowService { } async sendPayment() { - await this.createQuote().catch(async () => { - await this.rotateToken() - }) - await this.runPayment().catch(async () => { - await this.rotateToken() - }) + await this.createQuote() + await this.runPayment() } async getCurrentActiveTabId() { diff --git a/src/background/index.ts b/src/background/index.ts index 69f25814..7c65b53a 100755 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -6,7 +6,7 @@ const iconActive34 = runtime.getURL('assets/icons/icon-active-34.png') const iconActive128 = runtime.getURL('assets/icons/icon-active-128.png') const iconInactive34 = runtime.getURL('assets/icons/icon-inactive-34.png') const iconInactive128 = runtime.getURL('assets/icons/icon-inactive-128.png') -// + // const SENDING_PAYMENT_POINTER_URL = 'https://ilp.rafiki.money/wmuser' // cel din extensie al userului // const RECEIVING_PAYMENT_POINTER_URL = 'https://ilp.rafiki.money/web-page' // cel din dom diff --git a/src/content/messageListener.ts b/src/content/messageListener.ts index eca36ae1..e61fed35 100644 --- a/src/content/messageListener.ts +++ b/src/content/messageListener.ts @@ -35,26 +35,23 @@ export const onRequest = async ( case 'PAYMENT_SUCCESS': { const { receiveAmount, incomingPayment, paymentPointer } = msg.data - const monetizationTag = document.querySelector('link[rel="monetization"]') - const eventOptions = { - bubbles: true, - composed: true, - detail: { - amount: '1000000', - assetCode: 'USD', - assetScale: '9', - receipt: null, - amountSent: { - ...receiveAmount, - }, - paymentPointer, - incomingPayment, - }, - } - const customEvent = new CustomEvent('monetization', eventOptions) - // monetizationTag?.dispatchEvent(new CustomEvent('load', eventOptions)) - monetizationTag?.dispatchEvent(customEvent) + window.dispatchEvent( + new CustomEvent('monetization-v2', { + detail: { + amount: receiveAmount.value as string, + assetCode: receiveAmount.assetCode as string, + assetScale: receiveAmount.assetScale as number, + amountSent: { + currency: receiveAmount.assetCode as string, + amount: (receiveAmount.value * 10 ** -receiveAmount.assetScale) as number, + }, + paymentPointer: paymentPointer as string, + incomingPayment: incomingPayment as string, + receipt: null, + }, + }), + ) break } diff --git a/src/utils/polyfill.ts b/src/utils/polyfill.ts index dd6a0676..6acf624e 100644 --- a/src/utils/polyfill.ts +++ b/src/utils/polyfill.ts @@ -1,5 +1,7 @@ // language=JavaScript export const wm2Polyfill = ` + var monetizationTag = document.querySelector('link[rel="monetization"]'); + monetizationTag.dispatchEvent(new Event('load')); const dbg = () => { } dbg('setonmonetization property start') @@ -80,9 +82,10 @@ export const wm2Polyfill = ` dbg( 'monetization-v2 event' ) + const monetizationEvent = new MonetizationEvent('monetization', event.detail) - event.target.dispatchEvent(monetizationEvent) - }, { capture: true }) + monetizationTag.dispatchEvent(monetizationEvent) + }, { capture: true, bubble: true }) window.addEventListener('onmonetization-attr-changed', (event) => { dbg('onmonetization-attr-changed', event.detail.attribute) const { attribute } = event.detail diff --git a/tsconfig.json b/tsconfig.json index 77860cd9..e70f0d00 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,30 +1,30 @@ { - "compilerOptions": { - "noImplicitAny": true, - "strictNullChecks": true, - "noImplicitThis": true, - "baseUrl": "./src", - "esModuleInterop": true, - "module": "commonjs", - "target": "es5", - "allowJs": true, - "jsx": "react", - "sourceMap": true, - "moduleResolution": "node", - "allowSyntheticDefaultImports": true, - "allowUmdGlobalAccess": true, - "resolveJsonModule": true, - "paths": { - "@/utils/*": ["./utils/*"], - "@/popup/*": ["./popup/*"], - "@/background/*": ["./background/*"], - "@/content/*": ["./content/*"], - "@/assets/*": ["./assets/*"], - "@/components/*": ["./components/*"], - "@/types/*": ["./types/*"], - "@/hooks/*": ["./hooks/*"] - }, - "typeRoots": ["node_modules/@types", "./src/types"] + "compilerOptions": { + "noImplicitAny": true, + "strictNullChecks": true, + "noImplicitThis": true, + "baseUrl": "./src", + "esModuleInterop": true, + "module": "commonjs", + "target": "es5", + "allowJs": true, + "jsx": "react", + "sourceMap": true, + "moduleResolution": "node", + "allowSyntheticDefaultImports": true, + "allowUmdGlobalAccess": true, + "resolveJsonModule": true, + "paths": { + "@/utils/*": ["./utils/*"], + "@/popup/*": ["./popup/*"], + "@/background/*": ["./background/*"], + "@/content/*": ["./content/*"], + "@/assets/*": ["./assets/*"], + "@/components/*": ["./components/*"], + "@/types/*": ["./types/*"], + "@/hooks/*": ["./hooks/*"] }, - "exclude": ["dist", "dev", "temp"] + "typeRoots": ["node_modules/@types", "./src/types"] + }, + "exclude": ["dist", "dev", "temp"] }