● Usage example
Imagine that we can execute this through a native action like Shell Script action!
Oh dear I'm already so excited.JavaScript:'use strict'; const { someArray } = md.lv(['someArray']); // Get local variables as an object, by variable names in the array of first parameter if (! Array.isArray(someArray)) { md.throw('Retrieving array failed'); // Return the string as an error } const result = md.lv({ someArray: someArray.sort(), }); // Set local variables by the object of first parameter md.return(result); // Return the value as result
I'll do happily implement anything requested on this forum on behalf of @MacroDroidDev!
● Expected effect
It's so clear that it will be very powerful and useful for many different purposes that there may no need to explain how convinient will it be.
It may take relatively more work to implement than average features, but I believe JavaScript action could solve quite a lot of problems and meet various needs of power users and corporate customers, while maintaining the ease and simplicity which has been MacroDroid's original philosophy.
I also believe this will allow @MacroDroidDev to focus more on core/native functionalities and planning. If this were a Pro only feature, many users wouldn't bear not to upgrade even in order to enable useful macros created by power users.
In the future, if JavaScript and native functions are organically integrated, I'm pretty sure many corporate partners would be interested in thanks to its flexible extendability. - But again, this is all in the future.
● Technical considerations
LiquidCore(MIT License) claims that it provides a convenient way for Android developers to execute raw JavaScript. Personally it seems more stable and developer-friendlier than the new Jetpack's one.
I don't want to recommend relying on WebView instance for various reasons. Maybe I don't have to explain why, but let me comment a little for everyone. It can have some advantages like optionally showing visible UI like hybrid apps, but there are caveats and personally I didn't feel very stable especially for background running.
I have many ideas about bridge interface between JavaScript and MacroDroid. I'll happily documenting the draft spec if mine could be helpful. Also I could provide some Kotlin code too but I doubt it will be helpful because I have no idea about the source code of MacroDroid and its patterns/conventions.
● Other references
- HTTP Shortcuts is an open-source app and LiquidCore was used. It supports scripting feature for three purposes.
• Preparing before request
• Handling response
• Standalone execution - While this app looks so simple, this "scripting shortcut" opened potential to being a basic general-purpose automation app. - Tasker supports running JavaScript by two different ways and both supports native features by the global context object named "tk".
• JavaScript action runs external file or inline code in tasks
• WebView scene element is configurable to whether allow bridge object be accessible from JavaScript in HTML - like hybrid wrappers do
● EDIT
It seems that LiquidCore which superseded AndroidJSCore is no longer maintained too. Even it's not so staled because it supports most of ES6 features, but I can't recomment it anymore.
Hermes seems viable to be an alternative. It's open source(MIT license) too and made by the Meta(Facebook) so it's likely not to be discontinued near future. Android-Hermes shows how Hermes can be used as a JavaScript interpreter in Android. I don't know yet whether Hermes also supports Node.js features like LiquidCore, but i don't think that would be a deal-breaker.
WebView still doesn't seem like an priority option since its background lifetime is now just 5 minutes only.
Last edited: