Save value to tasker

129 views
Skip to first unread message

Mahammad Rafi

unread,
Feb 12, 2022, 8:16:38 PM2/12/22
to Tasker
Hi joao
I have downloaded below example template from www.w3schools.com. I am in a bit confusion how can I save values to tasker's global variables. 

Thanks in advance from Mahammad Rafi. 



<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  min-width: 250px;
}

/* Include the padding and border in an element's total width and height */
* {
  box-sizing: border-box;
}

/* Remove margins and padding from the list */
ul {
  margin: 0;
  padding: 0;
}

/* Style the list items */
ul li {
  cursor: pointer;
  position: relative;
  padding: 12px 8px 12px 40px;
  list-style-type: none;
  background: #eee;
  font-size: 18px;
  transition: 0.2s;
 
  /* make the list items unselectable */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Set all odd list items to a different color (zebra-stripes) */
ul li:nth-child(odd) {
  background: #f9f9f9;
}

/* Darker background-color on hover */
ul li:hover {
  background: #ddd;
}

/* When clicked on, add a background color and strike out text */
ul li.checked {
  background: #888;
  color: #fff;
  text-decoration: line-through;
}

/* Add a "checked" mark when clicked on */
ul li.checked::before {
  content: '';
  position: absolute;
  border-color: #fff;
  border-style: solid;
  border-width: 0 2px 2px 0;
  top: 10px;
  left: 16px;
  transform: rotate(45deg);
  height: 15px;
  width: 7px;
}

/* Style the close button */
.close {
  position: absolute;
  right: 0;
  top: 0;
  padding: 12px 16px 12px 16px;
}

.close:hover {
  background-color: #f44336;
  color: white;
}

/* Style the header */
.header {
  background-color: #f44336;
  padding: 30px 40px;
  color: white;
  text-align: center;
}

/* Clear floats after the header */
.header:after {
  content: "";
  display: table;
  clear: both;
}

/* Style the input */
input {
  margin: 0;
  border: none;
  border-radius: 0;
  width: 75%;
  padding: 10px;
  float: left;
  font-size: 16px;
}

/* Style the "Add" button */
.addBtn {
  padding: 10px;
  width: 25%;
  background: #d9d9d9;
  color: #555;
  float: left;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  transition: 0.3s;
  border-radius: 0;
}

.addBtn:hover {
  background-color: #bbb;
}
</style>
</head>
<body>

<div id="myDIV" class="header">
  <h2 style="margin:5px">My To Do List</h2>
  <input type="text" id="myInput" placeholder="Title...">
  <span onclick="newElement()" class="addBtn">Add</span>
</div>

<ul id="myUL">
  <li>Hit the gym</li>
  <li class="checked">Pay bills</li>
  <li>Meet George</li>
  <li>Buy eggs</li>
  <li>Read a book</li>
  <li>Organize office</li>
</ul>

<script>
// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if (ev.target.tagName === 'LI') {
    ev.target.classList.toggle('checked');
  }
}, false);

// Create a new list item when clicking on the "Add" button
function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === '') {
    alert("You must write something!");
  } else {
    document.getElementById("myUL").appendChild(li);
  }
  document.getElementById("myInput").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}
</script>

</body>
</html>

Mahammad Rafi

unread,
Feb 13, 2022, 3:01:01 AM2/13/22
to Tasker
Hi
Anyone know the solution for this?

From Mahammad Rafi. 

RSF

unread,
Feb 13, 2022, 11:06:51 AM2/13/22
to Tasker
Which value(s) from that web page do you want to save?

In general, one uses Tasker's "Variable Set" action to set a variable,. To make a variable Global vs. Local, its name should have at least one capital letter (e.g. %ToDo_Task vs. $todo_task).

Mahammad Rafi

unread,
Feb 13, 2022, 8:19:52 PM2/13/22
to Tasker
Hi
Use that in scenes in element called web view . Then you can understand what I am saying.

From Mahammad Rafi. 

RSF

unread,
Feb 14, 2022, 11:50:52 AM2/14/22
to Tasker
Still a bit unclear -- do you want to get the list of checked items, all items, just added items, etc.

Mahammad Rafi

unread,
Feb 14, 2022, 5:13:30 PM2/14/22
to Tasker
Hi
All items which were previously saved and currently savings. All items I want to save. I  hope I have explained better. 

From Mahammad Rafi. 

RSF

unread,
Feb 14, 2022, 7:01:10 PM2/14/22
to Tasker
You'd want to add some Tasker-compatible JavaScript to the <script> portion of your HTML file. Something like so (additions are highlighted in blue):

// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}
getAllItems();  // Save full item list to Tasker variable
getCheckedItems();  // Save checked list to Tasker variable

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if (ev.target.tagName === 'LI') {
    ev.target.classList.toggle('checked');
    getCheckedItems();  // Save revised checked list to Tasker variable

  }
}, false);

// Create a new list item when clicking on the "Add" button
function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === '') {
    alert("You must write something!");
  } else {
    document.getElementById("myUL").appendChild(li);
  }
  document.getElementById("myInput").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);
  getAllItems();  // Save new item list to Tasker variable

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}

// Get all items, and save into Tasker variable
function getAllItems() {
    var all_items = "";
    /* Get all items in current list, and save to all_items */
    document.querySelectorAll("#myUL li").forEach(function(elt) {
        all_items += elt.childNodes[0].nodeValue + "\n";
    });
    /* Send all_items' value to Tasker global variable */
    setGlobal("%All_Items", all_items);
}

// Get all checked items, and save into Tasker variable
function getCheckedItems() {
    var checked_items = "";
    document.querySelectorAll("#myUL li.checked").forEach(function(elt) {
        checked_items += elt.childNodes[0].nodeValue + "\n";
    });
    setGlobal("%Checked_Items", checked_items);
}



This page has more information on JavaScript within Tasker, including how to set and get global (and local) Tasker variables.

Of course the HTML has a static set of tasks. If you wanted to set the list from a Tasker variable, that'd require calling the Tasker global() function to get the previously saved tasks, and then using JavaScript to populate the to-do list within the HTML and script

Mahammad Rafi

unread,
Feb 15, 2022, 7:56:14 AM2/15/22
to Tasker
Hi

Thanks a lot for the info. Still I am in a bit confusion. I have tried the script. But not getting it to work. Post the full script for my understanding.
 Once again thanks a lot.

From Mahammad Rafi. 

RSF

unread,
Feb 15, 2022, 10:32:45 AM2/15/22
to Tasker
Todo_test.html

Mahammad Rafi

unread,
Feb 15, 2022, 11:00:18 AM2/15/22
to Tasker
Hi
I  am unable to save text and exit icons isn't appearing.
Before posting. Try that once. 
From Mahammad Rafi. 

RSF

unread,
Feb 15, 2022, 11:31:57 AM2/15/22
to Tasker
Works for me. I open the scene in Activity mode in the task. I can check/uncheck, add items, etc., and the changes are reflected in Tasker global vars.

Mahammad Rafi

unread,
Feb 15, 2022, 7:15:42 PM2/15/22
to Tasker
Hi
But not working in my device. I am unable to add,remove,click on add button and more.
Is there any specific reason for it. I have checked scene in several ways activity mode, dialog.

From Mahammad Rafi.  

RSF

unread,
Feb 15, 2022, 9:30:08 PM2/15/22
to Tasker
If you see something this in your scene's Webview element...
Webview.png
... but tapping on items doesn't put a checkmark in them, and tapping "Add" doesn't do anything, it sounds like JavaScript isn't active in your scene. The only thing I'm aware of to check, is to ensure "Allow Phone Access" is checked in the Webview's options:
Webview_options.png 

Aside from that, I have no idea. You could try creating an HTML file with nothing but a JavaScript alert() call in an embedded script, to test whether JavaScript is working at all.
Reply all
Reply to author
Forward
0 new messages