quote:
Originally posted by The-Phantom
That works perfectly! Huge thanks!
sorry, but what Matti posted should not be done unless you want to slow down Messenger...
--------------
quick note to Matti first:
Matti, shouldn't that be:
// Note the 3rd argument: async = false, thus send()
does not return immediately
The whole point of a synchronous call is exactly that
send() will
not return immediatly; it will wait until the whole thing is retrieved and thus it will slow down, if not lock up, Messenger!
Only when you have an asynchronous call, the
send will return immediatly (and hence the reason why he had to make (bad) use of that loop)....
--------------
So, I do very highly recommend using an
asynchronous method though! And actually advise against using a synchronous (async=false) method like Matti suggested.
By using a synchronous you will always end up slowing down and even locking up Messenger in one way or the other.
So, the problem you had is not because of the asynchronous call, but because you did not properly used the return function. Do not return (or wait to return) some text in the OnEvent_ChatWndSendMessage itself! That is not the proper way of doing things.
What you should be doing is:
1) in OnEvent_ChatWndSendMessage:
a) make the asynchronous call and assign another function to the
onreadystatechange property
b) return ""
2) And only inside that other function which got assigned to onreadystatechange (and thus
NOT outside this function like you did before with that loop), you do what you want to do.
Also note: in case you want to send some text back to the chat window in response to the outcome of the http call, check if the chat window still exists (highly important), then check if you actually can send some text. After that send the text you want with SendMessage... But all this also requires some additional scripting including keeping track of chat window objects and the contacts within the chats! So you will need to create and maintain an array on the global scope of the script too. And chats with more than one person will make this rather complicated though.
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
if (Message.substr(0, 10) === "/ajax-test") {
Debug.Trace("Retrieving " + Message.substr(11));
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("GET", encodeURI(Message.substr(11)), true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
Debug.Trace("html: " + xhr.responseText);
} else {
Debug.Trace("error: " + xhr.status);
}
}
}
xhr.send();
return ""
}
}
Although I suspect that you better assign the XMLHTTP object in the global scope though. Can't be bothered checking on how Plus! handles such stuff/objects when used asynchronously...
---------
Also:
quote:
Originally posted by The-Phantom
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
if (Message.substr(0, 10) != "/ajax-test") return "";
// your code for the command /ajax-test here
}
That is very wrong and you should never be doing this. You cripple the proper workings of command handling by that. You should be doing the opposite:
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
if (Message.substr(0, 10) === "/ajax-test") {
// your code for the command /ajax-test here
return "";
}
}
Note that there is no
Return outside the
IF statement.