Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

createSolanaRpcSubscriptions in a browser extension throws error #3652

Closed
liepauls opened this issue Nov 29, 2024 · 3 comments · Fixed by #3658
Closed

createSolanaRpcSubscriptions in a browser extension throws error #3652

liepauls opened this issue Nov 29, 2024 · 3 comments · Fixed by #3658
Assignees
Labels
bug Something isn't working

Comments

@liepauls
Copy link

Overview

createSolanaRpcSubscriptions in a browser extension throws error Cannot read properties of undefined (reading 'addEventListener')

Steps to reproduce

  1. Create a browser extension
  2. Run something along the lines of:
const rpc = createSolanaRpc('https://api.devnet.solana.com')
const rpcSubscriptions = createSolanaRpcSubscriptions('wss://api.devnet.solana.com')

const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({
  rpc,
  rpcSubscriptions,
})

const signedTx = ...

sendAndConfirmTransaction(signedTx, { commitment: 'confirmed' })

Please, let me know if a more detailed info on how to reproduce the error is needed 🤞

Description of bug

When sendAndConfirmTransaction is called, Cannot read properties of undefined (reading 'addEventListener') is thrown.

The error probably comes from here.

@liepauls liepauls added the bug Something isn't working label Nov 29, 2024
@steveluscher
Copy link
Contributor

Nice find. Which browser is this?

I presume that in contentScripts that globalThis.window is undefined but globalThis is an EventTarget. It looks like in browser-ish environments, globalThis is always an EventTarget, so it should be safe to change this to globalThis.addEventListener everywhere.

@liepauls
Copy link
Author

liepauls commented Dec 2, 2024

Hi @steveluscher, thanks for addressing the issue 🙇

Occured in Brave, tried your fix and it looks like the problem is gone.

steveluscher added a commit that referenced this issue Dec 4, 2024
…t target (#3658)

# Summary

In the isolated Content Scripts environment, `globalThis.window` is apparently not a thing. In this PR we eliminate that nesting and presume that the event target for listening to online/offline events is `globalThis` itself.

# Test Plan

Tested that `globalThis.addEventListener('offline', () => {})` fires the callback in Brave when the network connection goes down. Presumed that the same is true for all other browsers.

Fixes #3652.
Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
2 participants