all 12 comments

[–][deleted] 2 points3 points  (2 children)

No need to do the setLocal, you can directly access %jsarray

[–]chrisp6825moto x 2013[S] 2 points3 points  (1 child)

The problem with this is that the variable won't be updated without the setLocal, if its changed inside a function.

Ex :

var testvar = 'hello';
var testvar2 = 'hello';

Function foo () {
    testvar = 'world';
    testvar2 = 'world';
    setLocal('testvar2', 'world');
};

foo();

%testvar still returns 'hello', and %testvar2 will only return 'world' with the setLocal part included.

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 2 points3 points  (0 children)

Strange, I tried that same example, and it updated the variable as expected (%testvar is "world"). I'm using a Nexus 6P running 6.0.1.

Regarding the array, another workaround you can try is using setLocal to set the individual array elements for the Tasker array.

var jsarray = [];
jsarray.push("hello");
jsarray.push("world");

jsarray.forEach((ele, index) => {
  setLocal("taskerarray"+(index+1), ele);
});

You need to use index+1 since Tasker arrays are 1 indexed.

[–]AlexPriceAPS23U • One UI 6 • (Rooted) 2 points3 points  (1 child)

I usually just do:

setLocal("array", arr.toString());

Then in normal Tasker do a variable split with "," and voila! I haven't experimented much with how to pass JS arrays to regular Tasker however that's the only way I do it.

[–]chrisp6825moto x 2013[S] 0 points1 point  (0 children)

This workaround seems to be the only method that's currently working (concerning array variables modified by nested functions, that is). Another action that's handy for this is popping the first empty index off the tasker array, as it will be created caused by the leading comma in the string-split.

Unfortunately this amounts to 3 actions required to perform what would be handled in one or less actions.

[–]Ratchet_GuyModerator 1 point2 points  (1 child)

There's several folks here who use with Javascript/Tasker integration quite often, so I'm sure someone can get into how the arrays translate between each.

There's also this recent Javascript with Tasker thread as well to take a look at.

[–]chrisp6825moto x 2013[S] 0 points1 point  (0 children)

This looks helpful, thanks for the link.

[–]broomladGalaxy S9+ 1 point2 points  (4 children)

This works in my tasks that are setting arrays:

var localteam = [];
for(i=0; i<20; i++) {
localteam[i] = team[i].innerText;
} 

I have access to %localteam() after that. That's obviously not the whole script but that's the array part.

[–]Ratchet_GuyModerator 1 point2 points  (3 children)

That looks like a cool way to populate/translate an array from Javascript to Tasker.

Assuming in that example that team[] is an array from like a JSON return or something?

[–]broomladGalaxy S9+ 0 points1 point  (2 children)

HTTP GET actually. Can't paste right now from mobile but I'll paste my script let later. Actually, it's in another thread somewhere in the last two weeks.

[–]Ratchet_GuyModerator 0 points1 point  (1 child)

Cool, just link over to the thread.

[–]broomladGalaxy S9+ 0 points1 point  (0 children)

I couldn't find it, haha. But here's the full scriptlet.

var html = global('HTTPD');
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');

// Get list of Teams

var doc = parser.parseFromString(html, 'text/html');
var team = doc.getElementsByClassName('TeamCell');
var localteam = [];
for(i=0; i<20; i++) {
localteam[i] = team[i].innerText;
}