PDA

View Full Version : Retrieving and Parsing with curl and javascript


hohoman90
2006.09.05, 02:16 PM
hey,

I am a relatively new dashboard programmer, however i have had a lot of experience programming in the past. I am puzzled why some code i wrote is not working however. The code takes what the user inputs and sends to a websites api where the access code and input is examined and it returns something like this:
'1','test'
this says that it was successful and the input translates to 'test'.

here is my code (with parts left out for security reasons):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MD5 Lookup Widget</title>
<script language="javascript">

function loadhash() {
var hash = document.GetElementByID("hash").value;

if(hash.length == 30){
var result = widget.system("/usr/bin/curl http:\/\/api.something.com/?hash="+hash+"\&auth=authcodehere",null).outputString;
var length = result.length;
var hashlength = length - 6;

if(result.charAt(1) != '1') {
"Not Found" = document.GetElementByID("result").value;
}
else{
var output = result.substr(5,hashlength);
output = document.GetElementByID("result").value;
}
}
else {
"Invalid MD5 Hash" = document.GetElementByID("result").value;
}
}

</script>
<style>
body {
margin: 0;
}

.bodyText {
font: 12px "Verdana";
font-weight: bold;
color: white;
text-align: center;
position: absolute;
top: 87px;
left: 56px;
width: 180px;

}
</style>

</head>
<body>
<img src="default.png">
<form>
<div class="bodyText">Hash:
<div align="center">
<input name="hash" type="text" id="hash" size="30" onChange="loadhash()" />
</div>
<br />
<br />
<br />
<br />

Result:
<input name="result" type="text" id="result" size="20" />
</div>
</form>
</body>
</html>

Any help on why this isn't working would be greatly appreciated.

Thanks,
Nick

Taxxodium
2006.09.05, 02:30 PM
You shouldn't use widget.system() synchronously. Instead, Apple recommends you use it asynchronous for widgets you want to release.

Read this: http://developer.apple.com/documentation/AppleApplications/Reference/Dashboard_Ref/GadgetObj/chapter_2_section_3.html#//apple_ref/doc/uid/TP40001339-CH203-DontLinkElementID_24

hohoman90
2006.09.05, 04:37 PM
hmm i tried using endHandler to make it asynchronous, however nothing seems to happen. Is there something i am doing wrong with getting the form data?

Taxxodium
2006.09.06, 02:39 AM
Try changing GetElementByID to getElementByID

Other than that, use the alert function to debug and check the out in the Console.

You use alert like this:

var myNumber = 5;

alert("Entered number = "+myNumber);