Javascript - HTTP Get

907 views
Skip to first unread message

Teasüti

unread,
Aug 4, 2013, 5:35:07 AM8/4/13
to tas...@googlegroups.com
Hi all,
I've have no JS experience, and have no clue how to supersede the task HTTP Get with the JS XMLHttpRequest.
So I have the following Tasker task HTTP Get action:
Server:Port - maps.googleapis.com
Path - maps/api/geocode/xml
Attributes - latlng=%LOCN
                  sensor=true
Timeout - 30
Mime Type - text/xml

So how should I do this in JS(let)?
All I have comprehended so far I wrote this, then I realized I can't find a way to include attributes:

var http = new XMLHttpRequest();
http.open('GET','maps.googleapis.com/maps/api/geocode/xml',true);
send();
return(http.responseText);

I got the path, but where do I include attributes? What does mean the async: true (asynchronous) or false (synchronous) parameter, and which is should be used?

Thank you!

Another one:
can I fit in a function in another function?
Like:
setGlobal('LastData',global('HTTPD'));

sunil kolambkar

unread,
Aug 4, 2013, 7:51:12 AM8/4/13
to tas...@googlegroups.com
Are you simply want to get a map base on geocode.

I think Tasker already have a action

Teasüti

unread,
Aug 4, 2013, 10:33:22 AM8/4/13
to tas...@googlegroups.com
Not the particular task the problem - of course I already done this within Tasker -, but the use of the javascript itself.
I need a how-to. Example. And description of usage.
I was digging up the internet in the last few days, but it is extremly difficult for a newbie like me - w/o programming skills - to acquire self-taught knowledge w/o help, tutorial, howto, etc.
Thank you!

Teasüti

unread,
Aug 4, 2013, 11:39:48 AM8/4/13
to tas...@googlegroups.com
Some strange thing I think...
Is it possible that a command in JS code does not wait for execution and jump to next command before the previous is done?
I called into local variables some global Tasker variable, made an operation of them and then flash the results.
Now is the strange thing: tha flash command gives a Toast message that is from old values, the last time I called for those with global().
Let's see:
var ok = getLocation('net',false,10);
var loc = global('LOCN'), loctime = global('LOCNTMS'), time = global('TIMES'), age = time - loctime;
flash(age);
This gives me the result from the previous test run, so age here is older that it should be. Run again quickly, and get the correct value.
What the heck?!

Also noticed JS doesn't wait for Get Location commands, HTTP commands, those that have timeout parameters, etc...

Pent

unread,
Aug 4, 2013, 2:41:16 PM8/4/13
to tas...@googlegroups.com
Which Tasker version are you using ?

Pent

Teasüti

unread,
Aug 4, 2013, 3:03:43 PM8/4/13
to tas...@googlegroups.com
1.6u2 on GB 2.3.6.

Brandon Horwath

unread,
Aug 4, 2013, 11:44:01 PM8/4/13
to tas...@googlegroups.com
Is your location for service provider disabled?

This may cause tasker to have previous faulty data available for a location check if not enabled.

Check the user guide for more detailed info. Specifically regarding variable %LOC, I believe.

Pent

unread,
Aug 5, 2013, 3:05:49 AM8/5/13
to tas...@googlegroups.com

Is it possible that a command in JS code does not wait for execution and jump to next command before the previous is done?

There's a bug in the 1.6 Tasker execution for some functions (including getLocation), they have a timeout of e.g.
10ms instead of 10s. You could do those commands via standard Tasker actions then process the results
in JS. If it's necessary to your goal to do it all in JS, let me know and I'll see how easy it is to port the bug
fixes.

Pent

Teasüti

unread,
Aug 8, 2013, 10:42:16 AM8/8/13
to tas...@googlegroups.com
I see, 
so technically getLocation() function do not work, cause it is done before able to acquire the actual location.
Also noticed that with say command (I would use this entirely in JS), for example the flash() commands do not show parallel with a say() command.
Seems it waits for the execution for say(), and for that time the flash() do not show until say() finishes, but flash() timeout is over until that time.
I would like to use everything is JS because of performance. If I know right, JS is faster and more efficient than regular Tasker actions.

Could you write me a working example for http get in JS -Tasker please?
I can't figure it out.

Thank you!

Teasüti

unread,
Aug 8, 2013, 10:49:56 AM8/8/13
to tas...@googlegroups.com
It is not about the GPS. Look again closer!

Pent: I don't understand very well yet.
For second test run the JS code gets the %LOCN from the previous run, so the previous getLocation() was able to acquire position regardless of the minimal timeout. So this timeout is not that timeout what is in Get Location action. If this is over, then go for next line in JS code, not (just) stops the positioning. Am I right?

Teasüti

unread,
Aug 11, 2013, 2:00:59 PM8/11/13
to tas...@googlegroups.com
Tried these w/o succes:

var http = new XMLHttpRequest();
http.onreadystatechange=function() {flash(http.readyState);}
http.open("GET","koponyeg.hu/idojaras_rss.php?regios=4",false);
http.send();
flash(http.responseText)

http.responseText is empty. Ready state flashes 1, 2, 4 (no 3).
Same code in a function:

function foo()
{var http = new XMLHttpRequest();
http.onreadystatechange=function() {flash(http.readyState);}
http.open("GET","koponyeg.hu/idojaras_rss.php?regios=4",false);
http.send();
return(http.responseText)
}
flash(foo());
Same results, flash is empty, no ready state 3 (don't know this counts at all or not).

The phone don't use the internet at all when run this.
Please give me a working solution for http get is JS!!!!!

Kurt Milam

unread,
Aug 11, 2013, 7:05:53 PM8/11/13
to tas...@googlegroups.com
Your JavaScript is the problem. Ajax operations are asynchronous, but you're treating yours as if they're synchronous.

I have no trouble successfully sending ajax requests (POST, GET, HEAD) from JavaScript to outside servers via Tasker, and also getting the results back into Tasker.

Also, if you're trying to run this from an app, you need to make sure the app has the INTERNET permission. If the only internet connections you make from the app are via JavaScript, Tasker won't add the INTERNET permission on its own.

Teasüti

unread,
Aug 12, 2013, 3:33:27 AM8/12/13
to tas...@googlegroups.com
Wrong, if I use "true" in the httt.open, then JS don't even wait for the response.
Write me a working example - I can copy and paste - please rather than further description!
I think here is a bug in Tasker, the same code on the ICS+ version gives me the exception 101.

Kurt Milam

unread,
Aug 12, 2013, 4:52:58 AM8/12/13
to tas...@googlegroups.com
You may wish to consider being more humble and polite if you're asking other people to write code for you, especially since, four days ago, you said the following:

Teasüti said: I have no JS experience, and have no clue how to supersede the task HTTP Get with the JS XMLHttpRequest

I, on the other hand, have more than 15 years of JS experience and have worked with Ajax since at least as early as 1998, working with MSXML 1.0, which was released with Internet Expolrer 4.0. Additionally, I have told you that I have working examples. The working examples have been tested on at least 4 devices - Nexus 7, HTC One X, HTC One and HTC Sensation 4G, all running ICS, on two continents, Europe and North America.

Now, I'm going to pretend that you asked nicely for my help, and write your code for you, especially since it might be helpful for others who are running into the same problem. I wrote this on my HTC One and tested it just now. It works.

One last thing before the code - you should be doing Ajax operations asynchronously if you want to do something with the return values. In Tasker, that means you'll need to uncheck the 'Auto Exit' box on your action that performs the Ajax request, and you'll need to close the action yourself, in your JavaScript, after the Ajax request completes. Please see here to learn more about synchronous and asynchronous ajax requests.

// oversimplified (but working) code:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www.example.com', true);

// What do we want to do after the response loads asynchronously?
xhr.onload = function () {
  tk.flash(xhr.responseText + ' || ' + xhr.statusText);
  // Tell the action to close
  tk.exit();
}

xhr.send();

Et voila

I accept your forthcoming apology in advance.

TomL

unread,
Aug 12, 2013, 6:12:39 AM8/12/13
to tas...@googlegroups.com
Oh snap! Burn, with style, and code example, too.

I tip my hat to you, good sir. Well done. :)

Message has been deleted

Teasüti

unread,
Aug 12, 2013, 8:59:13 AM8/12/13
to tas...@googlegroups.com
I asked kindly and polite for first I think - maybe a bit shirt-sleeve -, and feel sorry if did not seemed like.
It is confusing and tiresome for me after several days of continuous trying w/o succes and relevant help.
After several asking finally got the right answer for the right question I took. Every other answer before was useless to me regarding to the original question. I had nothing to do with like location providers or permissions or sync/async connection while I wasn't even able to download data at all.
I ask for pardon, but therefore if I had to turn everybody against me to get answers, I happily go again. ;)
Thank you, finally figured out the problem myself before I could read your reply, I simply had to include the protocol in the url, that is avoided my attention so far.
Give me some time to examine every aspect of your kindly answer, I'm sure I can learn a lot from it too.
Thank you all again and have a nice day! ^^

Adam W

unread,
Feb 16, 2015, 7:50:47 PM2/16/15
to tas...@googlegroups.com
Thanks to the asker, for being persistent. Pent, Tasker is positively amazing, but it could use a little more documentation. I understand it is beyond scope to teach people JS, but just one, teensy little example of any single specific use of it would work wonders. The brain is a recipe center, a person will use a hammer and a screwdriver when they see creative applications for them. Tasker's XMLHTTPJS is a heavy toolbox on the top shelf. Put a label on there that says, check this out, you can ____ blank with a ____. Pent, your genius is... just plain scary. We muggles need a little more attention in the documentation lol. When people see x, they can then create w+x. I searched high and low for this answer, so thanks to the answerer too!

Marta Hintz

unread,
Feb 17, 2015, 1:21:59 AM2/17/15
to tas...@googlegroups.com
Aren't there tons of online and books on JS that users can review on their own?  Personally, I would love what you suggest, but I think given just one man's time, I would rather he just keep on making tasker useful for us with added actions and then stamping out all the google bugs that creep in with each new updates.  - give us the tools and then we can figure out how to use them.  

M

Pent

unread,
Feb 17, 2015, 3:22:22 AM2/17/15
to tas...@googlegroups.com
Marta, you've been giving out loads of help in the forum and I feel indebted, do you have any
particular feature request(s) ?

Pent

Pent

unread,
Feb 17, 2015, 3:26:02 AM2/17/15
to tas...@googlegroups.com
@Adam: ever since I started with Tasker the plan has been 'put it out there and they'll find something to use it for'. For
health reasons, I have very little keyboard time these days and so it's the only actual option open to me now.

I hate to leave posts unanswered and not be able to provide as much documentation and support materials as
is desirable for such a complex product, but Tasker will grind to a standstill otherwise.

Pent

Marta Hintz

unread,
Feb 17, 2015, 6:10:02 PM2/17/15
to tas...@googlegroups.com
@Pent  - Thanks for noticing!  :-)   I have no feature request.  thank you though!

Adam

unread,
Feb 17, 2015, 6:13:00 PM2/17/15
to tas...@googlegroups.com

Well we cannot have tasked grinding to a halt. Do what you have to do, and I will keep loving it.

--
You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/a6a_X-4Yipc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages