diegogurpegui / nos2x-fox

nostr signer extension for Firefox

Home Page:https://diegogurpegui.com/nos2x-fox/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to determine when window.nostr is available

donpdonp opened this issue · comments

My index.html is listening for a load event but even then window.nostr is undefined. If I manually open the browser console, window.nostr exists. I believe this is a timing issue, but I'm not sure what to look for or wait on to signify when nos2x-fox has been loaded into the page. I've also logged any 'message' events but nothing seems applicable.

By default, for the way the addon is develop at this moment, the scripts that load the addon are loaded after the page completed to load.
Specifically, the file that creates the window.nostr object is this nostr-provider.js, which is injected here content-script.js. According to https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts we could configure the manifest.json so that the addon content scripts file loads while the page is loading (that is, earlier) by specifying run_at: 'document_start'. But the behavior should be properly tested.

Having said that, what you want is some kind of notification or event once the window.nostr is ready to use, right? As you detected, there's nothing like that yet because the addon behaves mainly as a reactive extenstion.
You can try and modifiy this extension so after having everything ready it fires some event. Otherwise I would suggest you implement some timed logic after the page loads. For example, after the page loads you wait a few seconds and keep looking for window.nostr until it's there.

I'll keep this in mind to add something in the future. But happy to accept a PR if you want to add it.

Did you make progress on this @donpdonp ?