HTTP Request - search with DOM and getElementById

135 views
Skip to first unread message

francwalter

unread,
Nov 22, 2022, 5:42:52 AM11/22/22
to Tasker
Hallo
I want to get a value from a website (http) which I could get with JavaScript.
I get the website with HTTP Request (192.168.0.221/index - it is a BL602 tasmotalike own flashed switch) and in the variable %http_data I have a <div> with id=state.
Inside that div there is a table where its first td-field is my searched value (contains ON or OFF, depending on the state of the switch).
JSON in Tasker doesnt work obviously, but Javascript does in Firefox Konsole:

document.getElementById("state").firstChild.firstChild.firstChild.firstChild.innerHTML

This gets me ON or OFF.
But how is that done in Tasker, a Javascriptlet on a variable (http_data)?

Thank for hints.
frank

PS.:
Yes, it seems the easiest way for me at the moment, to get the value of that switch, because I would need a firmware update to get a real answer for a status request (would be much easier).
But this means soldering again, want to avoid that, see that forum.

João Dias

unread,
Nov 22, 2022, 6:46:36 AM11/22/22
to tas...@googlegroups.com
Hi. Tasker can read HTML :) Check here: https://tasker.joaoapps.com/userguide/en/variables.html#html

Thank you for your contact.

   Join: connect multiple devices (send pushes, remote SMS, notifications) on Android, Windows, Mac, Linux
   Tasker: customize/automate anything on your phone!
   AutoApps: add advanced functionality to Tasker via plugins

     

João Dias




francwalter

unread,
Nov 22, 2022, 7:49:54 AM11/22/22
to Tasker
joaomgcd schrieb am Dienstag, 22. November 2022 um 12:46:36 UTC+1:
Hi. Tasker can read HTML :) Check here: https://tasker.joaoapps.com/userguide/en/variables.html#html

 Yes, but this doesnt work:

%http_data[div{id="state"}.firstChild.firstChild.firstChild.firstChild.innerHTML]

I could grab the whole DIV (with id="state") and then cut it with Variable Split (or such) but this is a bit ugly.
But just this works already:

%http_data[td]

It gives me ON or OFF, that is the first TD-element in my http-response.
Thanks!

frank



João Dias

unread,
Nov 22, 2022, 8:05:52 AM11/22/22
to tas...@googlegroups.com
%http_data[div{id="state"}.firstChild.firstChild.firstChild.firstChild.innerHTML] doesn't work because it's not a CSS query :) You can only use CSS queries there.

You can try out your CSS queries here: https://try.jsoup.org/

Hope this clarifies it!


francwalter

unread,
Nov 22, 2022, 9:07:12 AM11/22/22
to Tasker
joaomgcd schrieb am Dienstag, 22. November 2022 um 14:05:52 UTC+1:
%http_data[div{id="state"}.firstChild.firstChild.firstChild.firstChild.innerHTML] doesn't work because it's not a CSS query :) You can only use CSS queries there.


 so to use things like getElementById I still need to use e.g. JavaScriptlet and something like:

var el = document.createElement('html');
el.innerHtml = http_data;
var state = el.getElementById("state").innerHTML;

and then I can use it in Tasker as variable %state
Is that still working?
 

João Dias

unread,
Nov 22, 2022, 9:24:04 AM11/22/22
to tas...@googlegroups.com
You can get an element by ID with a CSS Query: element[#id] for example :)

Check all the possible selectors here: https://jsoup.org/cookbook/extracting-data/selector-syntax

francwalter

unread,
Nov 22, 2022, 10:58:58 AM11/22/22
to Tasker
joaomgcd schrieb am Dienstag, 22. November 2022 um 15:24:04 UTC+1:
You can get an element by ID with a CSS Query: element[#id] for example :)

Check all the possible selectors here: https://jsoup.org/cookbook/extracting-data/selector-syntax

I dont get it. Is Tasker using the jsoup library? 
So what I can achieve with jsoup, I have in Tasker?


João Dias

unread,
Nov 22, 2022, 11:02:04 AM11/22/22
to tas...@googlegroups.com
Yes, that's correct. It's basically the same as using this in a Javascriptlet: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

:)
Reply all
Reply to author
Forward
0 new messages