quote:
Originally posted by matty
One thing I noticed
You cannot obtain the Contact object by passing the origin parameter to the GetContact function.
He extended the GetContact implementation so that you can search by name as well.
js code:
$.wrappers.Contacts.prototype.extend({
GetContact: function (str) {
if (str.toLowerCase() === Messenger.MyEmail.toLowerCase()) {
return this[0];
} else if (this.original.GetContact(str)) {
return $(this.original.GetContact(str));
} else {
for (var i = 0; i < this.length; i++) {
if (this[i].Name === str) {
return this[i]; // Only returns the first match
}
}
}
return null;
}
});
Still, it's not a good idea to do this. The value of origin may not resolve to the right contact, for example when a contact has a nickname assigned. Contact.Name always returns the name as published by the contact, whereas origin may contain the nickname of that contact. I think it's better to simply use origin - if the developer wants to resolve this to a contact, he can just implement this in the callback or override the event himself.
Also, the local command parsing should be a bit more sophisticated. For example, script commands should be escapable by adding an extra leading slash, such as "//mycommand". Also, parameters can be separated by many sorts of whitespace characters such as \n, \r,... Therefore, you'll need a regular expression to properly detect whether a given message is a script command and correctly separate the command from the parameter. Luckily, Cookie has written an excellent regular expression for this:
CookieRevised's reply to Gettin data from "/" commands.
The way I parse local commands is based upon Cookie's regular expression but instead of using the global RegExp object to retrieve the data, I use the result from exec() itself. It's better to use your local data rather than having to rely on global side effects.
js code:
var match = /^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)(?:[ \n\r\v\xA0]([\s\S]*))?$/.exec(message);
if ( match ) {
var command = match[1]; // note: command will have no leading slash
var parameters = match[2];
var tmp = $.triggerCommand($(chatWnd), $.MyContact, command, parameters, message, false);
return (tmp === undefined ? message : tmp);
}
This principle can't be easily ported to external commands though, since you don't know what the leading character will be (! or @ or ...). However, you won't have to deal with escaped commands either. I'd recommend to simply look for the command until the first encountered whitespace character and use the remaining part as parameters.
js code:
var match = /^([^ \n\r\v\xA0]*)[ \n\r\v\xA0]?([\s\S]*)$/.exec(sMessage);
if ( match ) {
var command = m[1]; // note: command will retain its leading character
var parameters = m[2];
$.triggerCommand($(chatWnd), $.MyContact, command, parameters, message, true);
}
quote:
Originally posted by Eljay
JScript does have a global scope. In regular JScript (WSH) you can just do "var global = this" in global scope then access it anywhere.
I know, I tried that but it's inaccessible. You can't read global variables from it or add variables to it. Heck, you can't even iterate over its contents. Very sad indeed.
Long post is long indeed.