Inject Javascript Into Android

Viewed 92

I'm currently working on a couple of apps. One for Android and one for iOS. I really am an iOS dev with limited experience with Android. I have some hybrid screens which utilize a Javascript bridge.

In iOS, I can inject the JS bridge code and initialize it using WKUserScript with the injection time set to .atDocumentStart. In this case, the bridge is ready to use immediately in the HTML page being loaded. For example, if I call the bridge in the head of the document, it will work.

In the case of Android, there doesn't appear there is a way to inject and initialize the JS bridge at the start of the document. I've tried to do this in a few places like onPageStarted and onLoadResource, but if I call the bridge in the head of the document, the bridge object will not yet have been created.

It seems the what I would have to do is use JavascriptInterface and make an explicit init call to instantiate the bridge. Is this the best approach or are there other ways to do this?

Ideally I'd like to have the bridge be automatically instantiated and usable in the head of the document. But if I can't, I can just create an init method which can be used to explicitly instantiate the bridge in both the Android and iOS platform cases.

0 Answers
Related