"Run JavaScript" Action (already been implemented)

Quidn

Passionate Member


● Usage example​

Imagine that we can execute this through a native action like Shell Script action!
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
Oh dear I'm already so excited. 😵
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:

Quidn

Passionate Member
Didn't you wanted like this?
I have ported that macro to JavaScript to introduce as an example.
The right are actual screenshots from HTTP Shortcuts.

Please note that it's just a short simple code snippet and far from generally recommended convention.

And it also shows how MacroDroid is easy and intuitive while bringing many features! :love:

Q231009MD2JS-1.png



Result:

Q231009MD2JS-2.pngQ231009MD2JS-3.pngQ231009MD2JS-4.pngQ231009MD2JS-5.png
 

Quidn

Passionate Member
Randomizing order of the entries to iterate
This is yet another ported example. Not 100% identical for this time but the only functional difference is one "Set Variable" action was added for preserving entries by cloning the array in the macro.

Q231013MD2JS.png
 
Top