加载 web 应用程序在 GeckoView 中

huangapple go评论83阅读模式
英文:

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”).

英文:

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”)."

答案1

得分: 1

https://github.com/truefedex/GeckoViewNativeWebApp
不幸的是,我不得不将网页文件从资源中解压到内部存储。这是一种解决方法,尽管效率不高(需要在应用程序更新时更新文件夹的内容,文件将占用两倍的空间等)。
但如果直接从资源加载文件,那么将无法从网络扩展加载内容脚本并与本机部分进行交互(因为content_scripts.matches不接受像jar: file:/data/app/*或resource://android/assets/*这样的字符串)。

英文:

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/*)

huangapple
  • 本文由 发表于 2023年4月10日 21:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定