wxt icon indicating copy to clipboard operation
wxt copied to clipboard

feat: type-safe messaging api

Open Timeraa opened this issue 1 year ago • 4 comments

[!CAUTION] Implementation and code are not done at all, feel free to test and provide feedback about changes though but note that code can change anytime

[!NOTE] Status: POC (Proof of Concept)

This is a tracking PR which aims to implement #643

Internal Messaging (via ports aka chrome.runtime.connect)

  • [x] Chromium mv3
  • [ ] Chromium mv2
  • [ ] Firefox mv3
  • [ ] Firefox mv2
  • [ ] Safari mv2

Native Messaging (via ports aka chrome.runtime.connectNative)

  • [ ] Chromium mv3
  • [ ] Chromium mv2
  • [ ] Firefox mv3
  • [ ] Firefox mv2
  • [ ] Safari mv2

Internal Messaging (via sendMessage)

  • [ ] Chromium mv3
  • [ ] Chromium mv2
  • [ ] Firefox mv3
  • [ ] Firefox mv2
  • [ ] Safari mv2

Native Messaging (via sendNativeMessage)

  • [ ] Chromium mv3
  • [ ] Chromium mv2
  • [ ] Firefox mv3
  • [ ] Firefox mv2
  • [ ] Safari mv2

UserScript Messaging (via ports)

  • [x] Chromium mv3 ~~Chromium mv2~~ - Doesn't exist
  • [ ] Firefox mv3
  • [ ] Firefox mv2 ~~Safari mv2~~ - Doesn't exist?

Injected script in root page Messaging

  • [ ] Chromium mv3
    • [x] ISOLATED World
    • [ ] MAIN World
  • [ ] Chromium mv2
  • [ ] Firefox mv3
  • [ ] Firefox mv2
  • [ ] Safari mv2

General Todos

  • [ ] Documentation
  • [ ] Tests

I am currently focusing on implementing the Background as an API approach, after this is done I will try to implement others if wanted by @aklinker1.

Questions for @aklinker1

  1. How would webpage messaging be done type safe? As for example my website would connect to it but I obviously can't just import wxt from there?
  2. How would native messaging work in a type safe way, probs same question as 1.
  3. What is an example for an injected iframe?
  4. I added a wxt/util export for a useful function imo, is that okay to keep there?
  5. How would I implement tests into this, could you maybe help with that if possible?

General Questions

  1. Should class properties be accessible on the other side too? (This would mean they'd be "streamed" to the other side but could only support basic types) - I don't think this should be done

Usage

Check the package README

Timeraa avatar Aug 04 '24 00:08 Timeraa

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
Latest commit a145829fad4564265888852a717651d4f7ef5520
Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/676f58874770bb0008d239cb
Deploy Preview https://deploy-preview-899--creative-fairy-df92c4.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Aug 04 '24 00:08 netlify[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 80.98%. Comparing base (b150a52) to head (a145829). Report is 96 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #899      +/-   ##
==========================================
- Coverage   81.12%   80.98%   -0.15%     
==========================================
  Files         128      128              
  Lines        6285     6285              
  Branches     1066     1069       +3     
==========================================
- Hits         5099     5090       -9     
- Misses       1171     1180       +9     
  Partials       15       15              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 04 '24 02:08 codecov[bot]

I like your abstraction approach, should've thought of that earlier tbh! ~~I need that brain of yours~~

I'll implement it the way you suggested it, I don't see any issues with it.

~~The only thing is that if you want to send something from the injected frame > background you'd need an in between proxy but I think that should be fine and makes it logically followable (Which is really good especially because extensions can be a huge brainfuck with messaging in different contexts), just needs to be documented.~~ I think you already did that with the message bridge, I'm half asleep already

Timeraa avatar Aug 04 '24 02:08 Timeraa

in the MessageTransport I added a async close() because for the port transport I have to close the ports I opened somehow on demand, for example if I use connect from a popup to a tab for example. What would the sentAt be used for btw?

Timeraa avatar Aug 04 '24 08:08 Timeraa

Any reason not to use birpc here? I've been using it with success and I believe Anthony would cater to our needs if any necessary features were missing.

aleclarson avatar Feb 08 '25 18:02 aleclarson

Any reason not to use birpc here? I've been using it with success and I believe Anthony would cater to our needs if any necessary features were missing.

That's an option for the RPC API, yes.

aklinker1 avatar Feb 08 '25 19:02 aklinker1

@Timeraa I'm cleaning up some old PRs, gonna close this for now. Feel free to reopen and resume work if/when you find time.

aklinker1 avatar Mar 04 '25 04:03 aklinker1

No worries, I lack a lot of time lately so that's fine... Also why I haven't done much on wxt

Timeraa avatar Mar 04 '25 09:03 Timeraa