How to inject JS into GeckoView in Android?

874 views Asked by At

Before migration to version 77.0.20200603183750 of GeckoView so we were using version 71.0.20191208234333 so we had to change imp. of WebExtension instance, it was taking just one parameter which is the location of JS files

from:

currentGeckoView.session?.run{

            getGeckoRuntime(this)?.run{

                val extension = WebExtension("resource://android/assets/location_search/")
                extension.setMessageDelegate(messageDelegate, "location_search")
                registerWebExtension(extension)
            }

            settings.userAgentMode = GeckoSessionSettings.USER_AGENT_MODE_MOBILE
            settings.userAgentOverride = "Mozilla/5.0 (Linux; Android ${Build.VERSION.RELEASE}; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36"
            settings.allowJavascript = true
            progressDelegate = createProgressDelegate()
        } 

to:

currentGeckoView.session?.run{
            val geckoRuntime = getGeckoRuntime(this)

            geckoRuntime?.webExtensionController?.install("resource://android/assets/location_search/")
                ?.accept({ extension ->
                    extension?.setMessageDelegate(messageDelegate, "location_search")
                    geckoRuntime.registerWebExtension(extension!!)
                }, { exception ->
                    Log.d("webExtensionController",
                        "Exception message from WebExtension: $exception"
                    )
                })

            settings.userAgentMode = GeckoSessionSettings.USER_AGENT_MODE_MOBILE
            settings.userAgentOverride = "Mozilla/5.0 (Linux; Android ${Build.VERSION.RELEASE}; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36"
            settings.allowJavascript = true
            progressDelegate = createProgressDelegate()
        }

Although JS injection is successful, this leads to prevent passing current state of web page. Due to this operation Session.loading boolean field is still true even though page is loaded successfully.

We used firefox-source-docs while migrating to new version and we cannot call ensureBuiltIn method on WebExtensionController instance in version 77 of GeckoView.

According to above doc, we need to call ensureBuiltIn method like below but we couldnt call because it became internal method in compiled WebExtension.class.

geckoRuntime.getWebExtensionController()
  .ensureBuiltIn("resource://android/assets/messaging/", "[email protected]")
  .accept(
        extension -> Log.i("MessageDelegate", "Extension installed: " + extension),
        e -> Log.e("MessageDelegate", "Error registering WebExtension", e)
  );

Does anyone help us about what we are missing while migrating to new version of WebExtension in GeckoView?

0

There are 0 answers