diff --git a/src/chrome/background/background.ts b/src/chrome/background/background.ts index 10af80cd..16127afd 100644 --- a/src/chrome/background/background.ts +++ b/src/chrome/background/background.ts @@ -128,7 +128,9 @@ chrome.notifications.onClicked.addListener(handleNotificationClick) chrome.notifications.onButtonClicked.addListener(handleNotificationClick) chrome.storage.onChanged.addListener(handleStorageChange) chrome.action.onClicked.addListener(openParingPopup) + chrome.runtime.onInstalled.addListener(handleOnInstallExtension) +chrome.management.onEnabled.addListener(handleOnInstallExtension) createOffscreen() diff --git a/src/chrome/content-script/content-script.ts b/src/chrome/content-script/content-script.ts index 9d8dbc5f..a319f9af 100644 --- a/src/chrome/content-script/content-script.ts +++ b/src/chrome/content-script/content-script.ts @@ -3,7 +3,7 @@ import { ContentScriptMessageHandler } from './message-handler' import { createMessage } from '../messages/create-message' import { MessageClient } from '../messages/message-client' import { ConfirmationMessageError, Message } from '../messages/_types' -import { errAsync, okAsync, ResultAsync } from 'neverthrow' +import { err, errAsync, okAsync, ResultAsync } from 'neverthrow' import { logger } from 'utils/logger' import { MessageLifeCycleEvent } from 'chrome/dapp/_types' import { sendMessage } from 'chrome/helpers/send-message' @@ -56,36 +56,30 @@ const messageHandler = MessageClient( { logger: appLogger }, ) -const handleWalletInteraction = async ( - walletInteraction: WalletInteraction, -) => { +const handleWalletInteraction = (walletInteraction: WalletInteraction) => sendMessage(createMessage.dAppRequest('contentScript', walletInteraction)) -} -const handleExtensionInteraction = async ( +const handleExtensionInteraction = ( extensionInteraction: ExtensionInteraction, ) => { switch (extensionInteraction.discriminator) { case 'openPopup': - await sendMessage(createMessage.openParingPopup()) - break + return sendMessage(createMessage.openParingPopup()) case 'extensionStatus': - await hasConnections().map((hasConnections) => { - sendMessageToDapp(createMessage.extensionStatus(hasConnections)) - }) - break + return hasConnections().map((hasConnections) => + sendMessageToDapp(createMessage.extensionStatus(hasConnections)), + ) case 'cancelWalletInteraction': - sendMessage( + return sendMessage( createMessage.cancelWalletInteraction( addOriginToCancelInteraction(extensionInteraction), ), ) - break case 'walletInteraction': - sendMessage( + return sendMessage( createMessage.walletInteraction({ ...extensionInteraction, interaction: addOriginToWalletInteraction( @@ -93,21 +87,25 @@ const handleExtensionInteraction = async ( ), }), ) - break default: - logger.error({ + return err({ reason: 'InvalidExtensionRequest', interaction: extensionInteraction, }) - break } } // incoming messages from dApps chromeDAppClient.messageListener( - handleWalletInteraction, - handleExtensionInteraction, + (p) => + handleWalletInteraction(p).mapErr((e) => { + appLogger.error('handleWalletInteraction', e) + }), + (p) => + handleExtensionInteraction(p).mapErr((e) => { + appLogger.error('handleExtensionInteraction', e) + }), ) // incoming messages from extension diff --git a/src/chrome/helpers/send-message.ts b/src/chrome/helpers/send-message.ts index 09c3c464..6026d1e9 100644 --- a/src/chrome/helpers/send-message.ts +++ b/src/chrome/helpers/send-message.ts @@ -3,6 +3,8 @@ import { ResultAsync } from 'neverthrow' export const sendMessage = (message: Message) => ResultAsync.fromPromise( - chrome.runtime.sendMessage(message), + chrome.runtime.id + ? chrome.runtime.sendMessage(message) + : Promise.reject({ reason: 'NoChromeRuntimeId' }), (error) => error as Error, ) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 0718af78..9e5fab44 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -11,5 +11,9 @@ export const logger = new Logger({ }) logger.attachTransport((logObj) => { - sendMessage(createMessage.log(logObj)) + try { + sendMessage(createMessage.log(logObj)) + } catch (error) { + console.error('Error sending log message', error) + } }) diff --git a/vite.config.ts b/vite.config.ts index 9afbfcef..564712f1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -25,6 +25,7 @@ const manifest = defineManifest(async () => { 'scripting', 'notifications', 'contextMenus', + 'management', 'idle', ] const matches = ['https://*/*', 'http://localhost/*', 'http://127.0.0.1/*']