So this is sort of hard to word but I am going to try my best to make this understandable :)
I am making a command driven personal assistant (in extremly early stages of development) which basically the user can type in different commands or questions and the assistant will answer the question or complete the task. For example, if a user types into the textbox "What is the time?" a dialog will appear and it will contain the time within the dialog. So what I want is when a user types in (for example) "what" it will suggest "what is the time" because that is one of the attributes within the function (you will understand when I post the code).
Here is the javascript (sorry for the mess):
// JavaScript Document
function searchKeyPress(e){
e = e || window.event;
if (e.keyCode == 13){
document.getElementById('btn').click();
}
}
function command() {
var srchVar = document.getElementById("srch");
var srch = srchVar.value;
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var t = srch;
if(srch == '') { alert('Please do not leave the field empty!'); }
else if(srch.indexOf('about') != -1) { alert('The function of this project is to complete simple tasks and sometimes answer simple questions. \n\nMade by Omar Latreche. \n\n(c) Copyright Omar Latreche 2015'); }
else if(srch.indexOf('commands') != -1) { window.location = "commands.html"; }
else if(srch.indexOf('time') != -1) { alert('The current time according to your computer is' + ShowTime(new Date())); }
else if(srch.indexOf('what') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('when') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('where') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('why') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('how') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('who') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch.indexOf('?') != -1) { if (confirm('I can see that is a question. Would you like to search Google for the answer?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }; }
else if(srch === 'okay assistant') { alert('Hello! How can I help you?'); }
else if(srch.indexOf('weather') != -1) { window.open('http://ift.tt/1ezzQ1d', '_blank'); }
else if(t.match(regex)) { window.open(srch, '_blank'); }
else { if (confirm('I am sorry but I do not understand that command. Would you like to search Google for that command?') == true) { window.open('http://ift.tt/1j936uB' + srch, '_blank'); }
else { /* Nothing */ }
}
}
//Show time in 12hour format
var ShowTime = (function () {
function addZero(num) {
return (num >= 0 && num < 10) ? "0" + num : num + "";
}
return function (dt) {
var formatted = '';
if (dt) {
var hours24 = dt.getHours();
var hours = ((hours24 + 11) % 12) + 1;
formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "PM" : "AM"].join(" ");
}
return formatted;
}
})();
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Tiny Assistant</title>
<script type="text/javascript" src="script.js"></script>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="cont_title">
<span class="title">Tiny</span><span class="title2"> Assistant</span>
</div>
<div class="cont">
<input name="srch" id="srch" class="search" spellcheck="false" onkeypress="searchKeyPress(event);" placeholder="Type "Okay Assistant"" type="text" />
</div>
<div style="margin-top:10px" class="cont">
<!--<input type="submit" onClick="command();" class="button" value="Done" id="btn" />-->
<button type="submit" id="btn" aria-label="Done" class="button" onClick="command();">
<span class="btn_txt">Done</span>
</button>
</div>
<div style="margin-top:10px" class="cont">
<span class="info">© Copyright Omar Latreche 2015. All rights reserved.</span>
</div>
</body>
</html>
I am sorry for the essay but that is the only way I could think to word it. I hope you understand what I'm trying to say.
Aucun commentaire:
Enregistrer un commentaire