This lets you use JS codes & libraries in your native OpenFL Android apps. The trick? An invisble webview!
Something to know:
-
Source codes are under MIT license.
-
This was created for experimental purposes and now its job was done, I probably won't be able to maintain or give support for it.
-
This can be used for the HTML5 target (so you can use the exact same Haxe codes across the targets).
How to use:
-
Clone this somewhere and add
<include path="cloned_path/openfl-jsrunner" />to yourproject.xml. -
Create a class implementing
extension.jsrunner.JSHandle. -
Call
extension.jsrunner.JSRunner.initwith your JS sources (internal or external) and an instance of the created class (see example). -
Method
onJSRunnerReadyis required and will be called when all the JS sources are loaded and started running. -
Member methods with
@jsinterfacecan be called in our JS sources throughwindow.ClassName.methodName(). -
Member methods with
@jscall(requires empty bodies) can be used in Haxe to call functions in JS sources if they are properties ofwindow.ClassName. -
Arguments and returned values of
@jsinterfaceand@jscallmethods can be of typesBool,Int,Float,StringandArrayof those types.
Example:
class Main extends openfl.display.DisplayObjectContainer implements extension.jsrunner.JSHandle {
public function new() {
super();
var js = "
window.Main.getWelcomeMsg = function() { return 'Our JS started running successfully...'; }
window.Main.signIn = function(email) {
window.JSRunner.trace('Signing in with ' + email + '...');
firebase.auth().signInWithEmailAndPassword(email, window.Main.getPassword()).then(function() {
FirebaseHandle.onSignedIn('Successfully signed in!');
}).catch(function(e) {
window.JSRunner.printE(e);
});
}
";
JSRunner.init([
EXTL("https://www.gstatic.com/firebasejs/7.19.0/firebase-app.js", false), // External source.
EXTL("https://www.gstatic.com/firebasejs/7.19.0/firebase-auth.js", false),
INTL(js) // Internal source.
], [ this ]); // Using multiple handles is supported.
}
function onJSRunnerReady():Void {
trace(getWelcomeMsg());
signIn("mail@mail.mail");
}
@jscall function getWelcomeMsg():String;
@jscall function signIn(email:String):Void;
@jsinterface function getPassword():String return "hunter2";
@jsinterface function onSignedIn(message:String):Void trace(message);
}