Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the .Net browser extension Apis in a zero UI plugin #85

Closed
sodiqyekeen opened this issue Jan 29, 2024 · 9 comments
Closed

Use the .Net browser extension Apis in a zero UI plugin #85

sodiqyekeen opened this issue Jan 29, 2024 · 9 comments

Comments

@sodiqyekeen
Copy link

I want to use this package mainly because of the extension APIs available in C#. However, I can't seem to find an entry point into my C# code without loading a page.
For example, I want to add an event listener for the onclick of the action and also register some other listeners from the onistalled event handler.

@mingyaulee
Copy link
Owner

In this case I am assuming that you are writing these event handlers in background worker JS. This page might help you get started with interop to WASM from JS and you can then use the WebExtensions.Net library to interop with the extensions API without Blazor. It's a lot of work to be done, but it looks like it's achievable.

@sodiqyekeen
Copy link
Author

Thank you for getting back to me so quickly. I am considering using Blazor because I would still have some modals displayed from the plugin. Just that I want to invoke .Net from JS when the plugin is loaded (onistalled). Anyways, I will check the documentation.

@mingyaulee
Copy link
Owner

In the background worker, Blazor is not loaded at all, therefore it would need to be initialized separately but the initialization is very heavy and depends on DOM which is not available in background worker. Currently the only way to use C# in background worker is to initialize the dotnet wasm separately just like how Blazor initializes it.

@sodiqyekeen
Copy link
Author

I am not able to create dotnet instance from the service worker. Getting this error:
MONO_WASM: import() is disallowed on ServiceWorkerGlobalScope by the HTML specification.

@mingyaulee
Copy link
Owner

Dynamic import is not allowed but static import should be allowed in the background worker. You can try to move the import to the top, does that work or is the error coming from the dotnet.js file?

@sodiqyekeen
Copy link
Author

The error is from dotnet.js

@mingyaulee
Copy link
Owner

In that case, unfortunately it is a lot harder to use WASM in background worker unless the Chrome extensions team allow dynamic import or dotnet team creates a js file with static imports.

@LostBeard
Copy link

LostBeard commented Feb 8, 2024

dynamic imports in the service worker not being supported does make it a lot harder. It does not seem likely that they will enable it any time soon.

import() is disallowed on ServiceWorkerGlobalScope by the HTML specification.See w3c/ServiceWorker#1356.

I wrote code to modify the Blazor _framework javascript files via a post-build event so that they use dynamic import when in a window and importScripts in workers and service worker. Working in a Chrome extension service worker, workers, etc. 👍👍

@mingyaulee
Copy link
Owner

Using C# in background is now supported with the version v1.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants