2

I'm trying to implement a noddy web extension for GeckoView in Android to simply execute some javascript on a web page, the equivalent of webView.evaluateJavascript() on a normal Android WebView, however I'm having trouble finding any useful examples or tutorials online.

I have previously tried a WebView before, however the site I am attempting to access bugs during login meaning I cannot access it. However the login works with a GeckoView

I've followed these so far:

  1. https://sammacbeth.eu/blog/2019/09/04/geckoview-extensions.html

  2. https://mozilla.github.io/geckoview/consumer/docs/web-extensions

  3. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Your_first_WebExtension

However, they seem to be a bit dated, even mozilla's own documentation, as the API I am using no longer follows the examples.

I'm using the stable release of GeckoView: 73.0.20200217142647

As per [3] above, my file structure is:

java
 |
 +-- assets
 |   |
 |   +--checkExtension
 |      |
 |      manifest.json
 |      checkExtension.js
 |
 +-- res
 |
 +-- src

The contents of manifest.json is

 {

  "manifest_version": 2,
  "name": "checkExtension",
  "version": "1.0",

  "description": "Automatically clicks the authorise button to continue the photo upload.",

  "content_scripts": [
    {
      "matches": ["*"],
      "js": ["checkExtension.js"]
    }
  ],
  "permissions": [
    "nativeMessaging",
    "geckoViewAddons"
  ]

}

'matches' is set to '*' so that it should match any and all pages navigated to

checkExtension.js just contains:

alert('hello')

My GeckoView activity is:

GeckoView view = findViewById(R.id.geckoview);
GeckoSession session = new GeckoSession();
GeckoRuntime runtime = GeckoRuntime.create(this);

session.open(runtime);

WebExtension test = new WebExtension(
    "resource://assets/extension/checkExtension/",
    runtime.getWebExtensionController());

runtime.registerWebExtension(test);

view.setSession(session);
session.loadUri("https://www.google.com");

However, no alert is showing when the page loads. I have tried a few site-specific targeted scripts besides just attempting to show an alert box, however none of them have worked either.

Is there anything obvious is am missing or any resources that might help me?

Many Thanks.

Alex
  • 95
  • 3
  • 13
  • One problem that I see is that the URL of the extension should be resource://assets/checkExtension/ instead of what you have, you should wait for the result like pocmo response's tells you, that will give you more information about why the extension is not running – Agi Sferro Feb 20 '20 at 17:54

1 Answers1

4

However, no alert is showing when the page loads

Note that by default GeckoView will not show any alerts itself and instead invoke GeckoSession.PromptDelegate.onAlertPrompt() if you register a PromptDelegate.

Instead of using alert you could log, or implement onAlertPrompt() to log or show a dialog. This is how the sample app implements it: https://searchfox.org/mozilla-central/rev/fca0be7e2cf2f922c9b927423ce28e8a04b3fd90/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/BasicGeckoViewPrompt.java#68-81

In addition to that registerWebExtension() returns a GeckoResult that you can use to detect whether the extension was successfully loaded. This may help you debug problems, e.g.:

runtime.registerWebExtension(ext.nativeExtension).then({
    Log.d(TAG, "WebExtension registered successfully")
}, { throwable ->
    Log.d(TAG, "WebExtension registration failed", throwable)
})
pocmo
  • 660
  • 6
  • 24