WebExtensions.Net icon indicating copy to clipboard operation
WebExtensions.Net copied to clipboard

Is there any way post message to ContentScript.razor from Popup.razor?

Open hanxiao123 opened this issue 10 months ago • 7 comments

in v1.3,Is there any simple way to post message to ContentScript.razor from Popup.razor? My ideal way is below,but it's not work. in Popup.razor WebExtensions.Runtime.SendMessage("",obj,null)

in ContentScript.razor:

protected override async Task OnInitializedAsync() { await WebExtensions.Runtime.OnMessage.AddListener(HandMsg); }

hanxiao123 avatar Apr 22 '24 03:04 hanxiao123

Is this way can do ? image but how to write Func? image

hanxiao123 avatar Apr 22 '24 03:04 hanxiao123

If you're sending message from popup to content script, the content script should be listening to message event and the popup should be the one sending it. Note that the message listener should be registered before the message is sent. Script injection is not for messaging, it is meant to run a script dynamically into a page, even if there is no content script.

mingyaulee avatar Apr 22 '24 07:04 mingyaulee

but the HandMsg can not be trigger.I've melted my brain for 5 days. in Popup.razor private async Task ButtonClick() { WebExtensions.Runtime.SendMessage("","msg",null) } in ContentScript.razor:

protected override async Task OnInitializedAsync() { await WebExtensions.Runtime.OnMessage.AddListener(HandMsg); } private async Task HandMsg(object obj, MessageSender sender, Action act) { } ContentScript.razor's OnInitializedAsync is before the button click

hanxiao123 avatar Apr 22 '24 07:04 hanxiao123

I see what might be missing here. According to the API spec, the first parameter for sendMessage is optional, which is illegal in C#, therefore the only method overload you see here is the one with extensionId, which means you have to explicitly provide your extension ID to send the message to.

I will move this issue to WebExtensions.Net and add another method overload that doesn't require the extension ID to be passed in.

For now as a workaround, you can try to pass in WebExtensions.Runtime.Id as the first parameter.

mingyaulee avatar Apr 22 '24 08:04 mingyaulee

My WebExtensions.Runtime.Id has provided and has a value, but HandMsg is still not work. in popup.razor: private async Task ButtonClick(string username) { await WebExtensions.Runtime.SendMessage(WebExtensions.Runtime.Id, "msg", null); } in ContentScript.razor: protected override async Task OnInitializedAsync() { await WebExtensions.Runtime.OnMessage.AddListener(HandMsg); }

image

hanxiao123 avatar Apr 23 '24 02:04 hanxiao123

Whether it is placed in the OnInitializedAsync or OnAfterRenderAsync of ContentScript.razor or Background.razor, the result is the same.

hanxiao123 avatar Apr 23 '24 06:04 hanxiao123

After some searching I found in the documentation about this

Sends a single message to event listeners within your extension or a different extension/app. Similar to runtime.connect but only sends a single message, with an optional response. If sending to your extension, the runtime.onMessage event will be fired in every frame of your extension (except for the sender's frame), or runtime.onMessageExternal, if a different extension. Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

Here is an example of how this is achieved. https://stackoverflow.com/questions/29926598/sendmessage-from-popup-to-content-js-not-working-in-chrome-extension

mingyaulee avatar Apr 23 '24 14:04 mingyaulee

Another method overload for sendMessage has been added in v2.1.0

mingyaulee avatar May 12 '24 15:05 mingyaulee

Is there any Test project or example about new way to sendMessage?

hanxiao123 avatar May 18 '24 08:05 hanxiao123

There is no sample showing it unfortunately. I am thinking of creating a sample project for messaging or maybe even implementing a simpler messaging broker so that it can be more intuitive. I will look more into it when I get some free time.

You can try out the approach suggested in the stack overflow answer by using Tabs.SendMessage API from the popup to send a message to the content script.

mingyaulee avatar May 18 '24 22:05 mingyaulee

Messaging samples project has been added!

mingyaulee avatar May 27 '24 22:05 mingyaulee