Synchronize Time with Browser

Posted by jacky95 
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.
Now, this forum is in read-only mode. You find details Details hereContinue on /r/PirateBox
Synchronize Time with Browser
November 07, 2015 07:28AM
I would like to update Piratebox's time with browser's time.
In LUCI , there is a 'Sync With Browser' button that when pressed it will synchronize the router's time with the browser's time.
How to add that button to Piratebox's webpage?

Thanks
Re: Synchronize Time with Browser
November 09, 2015 02:55AM
I am trying to update date using webpage, but failed to update the date/time of router :

my cgi-bin file :

#!/bin/sh

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Form Example</title>'
echo '</head>'
echo '<body>'

echo "<form method=GET action=\"${SCRIPT}\">"\
'<table nowrap>'\
'<tr><td>Input(YYYYMMDDHHMM):</TD><TD><input type="text" name="val_x" size=12></td></tr>'\
'<tr>'\
'</tr></table>'



echo '<br><input type="submit" value="Set Date/Time">'


# Make sure we have been invoked properly.

if [ "$REQUEST_METHOD" != "GET" ]; then
echo "<hr>Script Error:"\
"<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."\
"<br>Check your FORM declaration and be sure to use METHOD=\"GET\".<hr>"
exit 1
fi

# If no search arguments, exit gracefully now.

if [ -z "$QUERY_STRING" ]; then
exit 0
else
# No looping this time, just extract the data you are looking for with sed:
XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

ZZ=`echo "$QUERY_STRING" | sed -n 's/^.*val_z=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
echo '<br>'
echo '<br>'
echo 'Input Example: 201409080706 ---> 8 September 2014 07:06'
echo '<br>'
echo '<br>'
echo '=============================='
echo '<br>'
echo "Input: " $XX
echo '<br>'

echo '<br>'
echo "date: " $ZZ

fi
date "$XX"
echo '<br>'
echo '=============================='
echo '</body>'
echo '</html>'

exit 0
Re: Synchronize Time with Browser
November 09, 2015 02:57AM
I also have tried making the 'Sync With Browser" button like this cgi-bin file :


#!/bin/sh
_body(){
cat <<EOF
<html>
<head>
<script>
function AddZero(num) {
return (num >= 0 && num < 10) ? "0" + num : num + "";
}
function get_jam_browser(){
var now = new Date();
var strDateTime = [[now.getFullYear(), AddZero(now.getMonth() + 1),AddZero(now.getDate())].join("-"), [AddZero(now.getHours()), AddZero(now.getMinutes()),AddZero(now.getSeconds())].join(":")].join(" ")
return strDateTime
}
function send_datetime()
{
var xmlhttpp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText)
}
}
xmlhttp.open("POST","$(basename $0)",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("update="+get_jam_browser());
}
</script>
</head>
<body>
<button type="button" onclick="send_datetime()">Sync With Browser</button>
</body>
</html>
EOF
}

echo "Content-type: text/html"
echo ""
if [ "$REQUEST_METHOD" = POST ]; then
read -t 3 QUERY_STRING
CMD=$(echo $QUERY_STRING | sed 's/^.*=//; s/&/ /g; s/%3A/:/g')
if [ ${#CMD} -gt 10 ];then
date -s "$CMD"
echo "Sync With Browser OK..!!"
exit 0
else
echo $CMD
fi
else
_body
fi
#end of file
#mikodemos 2015


=====================

But It also failed although there is no error messages, is it have to do with the permission?
Re: Synchronize Time with Browser
November 09, 2015 05:28PM
At last, I have solved it . I have to edit the lighttpd configuration in
server.username = "nobody"
server.groupname = "nobody"

become
#server.username = "nobody"
#server.groupname = "nobody"

And add # in piratebox.conf at additional Lighttpd configuration part