Search code examples
androidfirefox-addon-webextensionsgeckoview

Load web appliction in GeckoView


I investigating possibilities of GeckoView for android to load local web page with some logic. My requirements:

  • page should be loaded from android app assets with some other resources like css, js, png.
  • page should be able to communicate with kotlin-based app bidirectionally.
  • page should be able to send ajax requests to other domains (is this is not possible then at least to redirect requests to kotlin app)

I unsuccesfully tried several ways to do it. GeckoSession.Loader.data(... api seems can load only html, but it not provide possibility to load page with resources. Also communication with native app seems only possible by web extensions. But I can not find any example of this. I mean I seen an example of such communication for example there https://searchfox.org/mozilla-central/source/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging but can not understand how to connect it with local web page since there seems some kind of isolation between web pages and extensions. For example I tried to save web page in assets with my extension and load it like this: session.loadUri(extension!!.metaData.baseUrl + "test.html") and it loads html but can not run any js code. Errors printed like: JavaScript Error: "Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”)."


Solution

  • I made some example of such communication with pre-packaged web page: https://github.com/truefedex/GeckoViewNativeWebApp Unfortunately, I had to unpack the web page files from the assets to the internal storage.

    This is a workaround, albeit an inefficient one (you need to update the contents of the folder when you update the application, the files will take up twice as much space, etc.).

    But if you load files directly from assets, then you won’t be able to load the content script from the web extension and organize interaction with the native part (because content_scripts.matches does not accept strings like jar: file:/data/app/* or resource://android /assets/*)