Debug.Trace needs a string as parameter but you're passing in a ChatWnd object. This cannot be implicitly converted to a string and thus JScript throws a type mismatch error.
When debugging, I usually output the handle of a window to check if the variable contains a window:
Javascript code:
Debug.Trace(window.Handle)
You might actually want to use this handle to make a more versatile window storage. Also, by using a OnEvent_ChatWndSendMessage you can properly distinguish sent messages from received messages using a similar storage.
Javascript code:
// Create containers to
var ChatWnds = {}, ChatWndIsReceivedMessage = {};
// Store the chat window object in our container when it is opened
function OnEvent_ChatWndCreated(ChatWnd) {
ChatWnds[ChatWnd.Handle] = ChatWnd;
ChatWndIsReceivedMessage[ChatWnd.Handle] = true;
}
// Remove the object from the containers if the window is closed
function OnEvent_ChatWndDestroyed(ChatWnd) {
delete ChatWnds[ChatWnd.Handle];
delete ChatWndIsReceivedMessage[ChatWnd.Handle];
}
// If we send a message, mark it in the container
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
ChatWndIsReceivedMessage[ChatWnd.Handle] = false;
}
// Create a timer when a message is received
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {
if (ChatWndIsReceivedMessage[ChatWnd.Handle]) {
// If this block is reached, the message has to be
// a message received from a contact
// Set a timer and concatenate the handle in the timer ID
MsgPlus.AddTimer("DoSomethingAwesome:" + ChatWnd.Handle, 100);
}
// Mark as received in the container
ChatWndIsReceivedMessage[ChatWnd.Handle] = true;
}
// Handle the timers
function OnEvent_Timer(TimerId) {
if (TimerId.indexOf("DoSomethingAwesome:") === 0) {
// Get the ChatWnd handle out of the TimerId
var ChatWndHandle = TimerId.substr("DoSomethingAwesome:".length);
// Get the ChatWnd object associated with this handle
var ChatWnd = ChatWnds[ChatWndHandle];
// Do something awesome here
}
}
Derived from
matty's reply to Reply to any message.
One thing to note: you should generate the containers at OnEvent_Initialize to account for the case where the script is started while the current is already signed in (e.g. when it's just installed or activated). Simply enumerate Messenger.CurrentChats and add them to the objects.