Okay, I now see where your problem is.
It is not possible to replace your sent message with a response text retrieved through an asynchronous XMLHTTP request. This is because if you return something in the onreadystatechange event, it'll be returned in the onreadystatechange function, not in the OnEvent_ChatWndSendMessage!
Therefore, there are 2 ways to solve this:
- Make the call synchronous by doing:
jscript code:
xmlhttp.async = false;
after "var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')". This way, you can drop the onreadystatechange event and retrieve the response text immediately after you call xmlhttp.send(). However, this may lag your Messenger for a small period of time and is therefore not recommended.
- Keep your asynchronous call by returning an empty string and use ChatWnd.SendMessage in the ready state event function.
More shocking thing I noticed: you try to access xmlhttp.responseText
outside your onreadystatechange function! This is why you get the error: basically, you try to access xmlhttp.responseText immediately after defining the event. It's not because you place it after the function definition that it'll only be executed after the function has been executed, no, that's not how asynchronous calls work. Look at the code in the thread and in my last post: the DoSomethingUseful is
inside the if-block in the function!
I think I can only help you by fixing this code myself for you... so here we go:
jscript code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
for(var e = new Enumerator(ChatWnd.Contacts); !e.atEnd(); e.moveNext()){
rEmail = (e.item().Email);
}
// return Email;
// ^ use to echo contacts email address
// return Messenger.MyName;
// ^ use to echo current users name
if(Message.substr(0, 2) === "+1") {
var name = Messenger.MyName;
var gEmail = Messenger.MyEmail;
var rMessage = (name + ' has rewarded you with a +1!');
// Database call
var url = "http://plusone.samryan.co.uk/test.php";
var data = ('rEmail= ' + rEmail + '&gEmail=' + gEmail);
Interop.Call("wininet.dll", "DeleteUrlCacheEntryW", url);
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", data.length);
// Declare a ready state change event
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
// Data from URL was retrieved successfully
// Now we can send the response text
ChatWnd.SendMessage(xmlhttp.responseText);
// Any other code for when the request was succesful
// should go here
} else {
// The server returned an HTTP code other than 200
}
}
};
// After we declared a ready state change event,
// we can start the request. It won't get started
// any earlier than this!
xmlhttp.send(data);
// The script won't wait for the request to finish.
// Therefore we'll return an empty string so the
// declared event can send a message later.
return "";
}
}