Daddoon / Xamarin.Android.GeckoView

A NuGet package and an example project to embed a GeckoView in your Xamarin Android App
MIT License
4 stars 0 forks source link

inject Javascript to GeckoView #27

Open Theeyn opened 2 years ago

Theeyn commented 2 years ago

hi, How to call with this script? "javascript:(function() { document.getElementsByClassName('sqdOP')[1].click(); })()"

I have this solution: (But I do not understand) You can use the WebExtension API to inject javascript into pages, please follow this guide: mozilla.github.io/geckoview/consumer/docs/web-extensions

Daddoon commented 2 years ago

I'm not sure you can do this on the current GeckoView bindings. The current GeckoView API is pretty old, and i'm not sure theses kind of things were available. I may be wrong. Can you try to find a documentation of GeckoView 67 API ?

Theeyn commented 2 years ago

Thank you for your answer Unfortunately I didn't find anything and completely gave up. I continue using webview.

Daddoon commented 2 years ago

On my, now legacy project BlazorMobile, as i was unable to directly call Javascript code through a read-to-use helper, i used two kinds of mecanism with GeckoView, but the usage depend if you are in control or partially in control the app you are rendering.

One way was to expose a little webserver locally on the phone through embed.io package (https://unosquare.github.io/embedio/) , and then, when your web app load , manage it to connect through Websocket to a Websocket endpoint you would have exposed on the phone. This way you would have a bidirectional communication available to do whatever you want, and so, implement your own behavior logic on web-end / internal end.

That was the way i used in order to call code from Blazor Web to native device, and then return datas when things finished processing.

There was also another tricky way to catch some Web page events i used: You can actually load WebExtensions in GeckoView ! For my needs, i developped a mini WebExtension that fire an event when a Navigation or iFrame Navigation occur (so a possible page change or page load), respectively, frame and sub_frame from a webextension point of view.

When i needed to validate if the navigation should occur on the page from the native side logic, i juste forwarded the event through a Http endpoint on the device (still thanks to embed.io) , and when the result returned, i just validated or denied the behavior of the page from the WebExtension event i was still in.

But yes, in the current stage, there is no "direct" way to call the web end.