Android Tutorial for GeckoView: Getting Started | raywenderlich.com

In thus tutorial you’ll learn about GeckoView, an open source library that allows you to render web content on Android using the Gecko web engine.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1381698-android-tutorial-for-geckoview-getting-started
1 Like

Hi Arturo, Thanks for the in-depth article. Does geckoview support injecting custom JS , like Android Webview? (e.g. Inject JavaScript into Android WebView | by Filipe Batista | Medium)](

thanks in advance

Hi @weprofit,
Yes, it does. You can do it using WebExtensions, you can find more information about how to use it on GeckoView here.

If you have any additional questions please let me know :slight_smile:

Thanks for pointing me in the right directions. Content Script is a static JS file included in the build. How do I pass a parameter/variable to it?

My use-case is as follows- I want user to specify a word, and when i show the webpage in Geckoview, I want to highlight the word occurrence in the page . How to I pass this word_to_higlight variable to my content script?

thanks again in advance.

Hi @weprofit sorry for the delay,
As the documentation describe the web extension allows bidirectional communication, you can send a message from Kotlin/Java to JavaScript and viscera. Take a look at the section “Communicating with Web Content” under the web extension documentation. There you can find some examples of how to pass some parameter/variables from one side to another. Here you can find a more in-depth example of the Reader view functionality.

If you want to mimic the find in page functionality, GeckoView provides a build-in API for that take a look at GeckoSession.getFinder(), you can find some examples of usages here.

Hope this answer helps you, if you need any additional help please let me know, I’m glad to help.

I just saw this tutorial Sam Macbeth - Running Webextensions on Android with Geckoview this could be also helpful!

Hi, im getting the Secure Connection Failed, it is because bad cert or something, you know how to handle this? in firefox there is a option to continue, thanks

Hi @amejia481,
I am trying to implement Javascript using Web Extension in GeckoView.
Can you please help me out with my code.

https://github.com/ankitrawat46/GeckoJavascript

Here i need to just change the colour of some elements on the webpage using javascript.
Thanks

Hi @ismalel,
This is a work in progress.
Bypass certificate error pages → 1553265 - No way to bypass certificate error pages, such as self-signed certs
Import custom certificates → 868370 - Provide a way to import user certificates (with their private keys) from PKCS#12/PFX files (Firefox for Android)

Hi @ankitrawat46 I have to catch a flight today, but I can take a look over the weekend!

Thanks @amejia481 Will wait for your update.

Hi @ankitrawat46 I opened a PR responding to your request Using content_scripts by Amejia481 · Pull Request #1 · ankitrawat46/GeckoJavascript · GitHub

The code is functional and I added some details about why it wasn’t working, if you have any additional questions please let me know happy to help :slight_smile: !

Hi @amejia481 Thanks for the help.
I have one question, If i want to change the class name 'infobox' in JS during runtime depending on the next page it opens.

document.getElementsByClassName('$myString')

In this case, you have to send a message from the app to the JS and which contains the name of the class that you want to replace, you could follow this section where it explains how to send a message from the app to the JS Redirecting to https://firefox-source-docs.mozilla.org/mobile/android/geckoview/consumer/web-extensions.html

How do you make this work in Android 4.1.1 (API 16)? It throws all kinds of Java compile errors when I follow the steps you outlined but with a new (empty) project.

I think the minimum API requirements are 16 for ARM and 21 for ARM64. Which architecture are running on?

Presumably not 64, since it’s an old tablet running Android 4.1.1.

Could you share your code via Github or other tool, also more details about your device and the errors that you are encountering?

Hi Arturo, I have read some samples and documentation. I need to intercept all requests from a webpage inside geckoview (link, XMLHttpRequest, every resource request, requests from inside iFrames). Is that possible?

I looked at WebExtension but it looks that is not what I need. Would be great if I could inject/set a global WebExecutor to process all requests. Any hint/suggestion is highly appreciated.

Thanks
Jochen

Hi @scubainstructor, I think you could set a navigation delegate on the session and override the onLoadRequest function.

For example:

  private fun setupGeckoView() {
    geckoView = findViewById(R.id.geckoview)
    val runtime = GeckoRuntime.create(this)
    geckoSession.open(runtime)
    geckoView.setSession(geckoSession)
    geckoSession.loadUri(INITIAL_URL)

    // Setting the delegate
    geckoSession.navigationDelegate = createNavigationDelegate()
  }

  private fun createNavigationDelegate(): GeckoSession.NavigationDelegate {
    return object : GeckoSession.NavigationDelegate {

      override fun onLoadRequest(session: GeckoSession, request: GeckoSession.NavigationDelegate.LoadRequest): GeckoResult<AllowOrDeny>? {
        // Conditioning when the request should load.
        return if (request.uri.contains("pattern")) {
          GeckoResult.ALLOW
        } else {
          GeckoResult.DENY
        }
      }
    }
  }

Hope it helps :slight_smile: