WebExtensions.Net
WebExtensions.Net copied to clipboard
Is there any way post message to ContentScript.razor from Popup.razor?
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); }
Is this way can do ?
but how to write Func?
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.
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
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.
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); }
Whether it is placed in the OnInitializedAsync or OnAfterRenderAsync of ContentScript.razor or Background.razor, the result is the same.
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
Another method overload for sendMessage has been added in v2.1.0
Is there any Test project or example about new way to sendMessage?
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.
Messaging samples project has been added!