Shoutbox

Help with very small script - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Help with very small script (/showthread.php?tid=94126)

Help with very small script by Barathrum on 03-17-2010 at 09:33 PM

Hello, I'm in need of a small script:

Whenever the owner of script recieves a messege, then there should be a condition, if the email of the writing guy is equal to something I set in the script, then it replaces his messege with "none".


RE: Help with very small script by matty on 03-18-2010 at 12:29 AM

Something like this?

Javascript code:
var oEmails = [
    'email1@hotmail.com',
    'email2@hotmail.com'
];
 
function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    for (var oEmail in oEmails) {
        if (pChatWnd.Contacts.GetContact(oEmail) && pChatWnd.Contacts.Count === 1) {
            return '';
        }
    }
}


RE: Help with very small script by Barathrum on 03-18-2010 at 05:16 PM

Well I typed this

var oEmails = [
    'matejsturm57@hotmail.com',
];

function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    for (var oEmail in oEmails) {
        if (pChatWnd.Contacts.GetContact(oEmail) && pChatWnd.Contacts.Count === 1) {
            return '';
        }
    }
}


and it didn't work, his messege was the same.


RE: Help with very small script by Spunky on 03-18-2010 at 05:26 PM

Remove the comma (,) at the end of this line

Javascript code:
var oEmails = [
    'matejsturm57@hotmail.com',];


It should be:

Javascript code:
var oEmails = [
    'matejsturm57@hotmail.com'];


It is a symbol used to separate multiple array items. As it is there, the array expects another item and fails when it doesn't find it. As that line of code fails, it would either cause an error and stop, or the code that does run would never find the array item.
RE: Help with very small script by Barathrum on 03-18-2010 at 08:12 PM

It still didn't change his messege :/


RE: Help with very small script by matty on 03-18-2010 at 08:27 PM

OOPS!!!!!!!!!!!!!!!!!!

Javascript code:
var oEmails = [
    'email1@hotmail.com',
    'email2@hotmail.com'
];
 
function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    for (var oEmail in oEmails) {
        if (pChatWnd.Contacts.GetContact(oEmails[oEmail]) && pChatWnd.Contacts.Count === 1) {            return '';
        }
    }
}


By using oEmail alone it is returning the position in the Array (in other words 0). Need to use oEmails[oEmail] to get the email address from the array.
RE: Help with very small script by Barathrum on 03-18-2010 at 09:06 PM

Now it works, but it sets messege for all to ' ' instead for only the email I set.



var oEmails = [
    'matejsturm57@hotmail.com'
];

function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    for (var oEmail in oEmails) {

        if (pChatWnd.Contacts.GetContact(oEmails[oEmail]) && pChatWnd.Contacts.Count === 1) {
            return '';
        }
    }
}


RE: Help with very small script by Matti on 03-18-2010 at 09:16 PM

quote:
Originally posted by matty
By using oEmail alone it is returning the position in the Array (in other words 0). Need to use oEmails[oEmail] to get the email address from the array.
That's exactly why I'd suggest you to stop using such variable names in loops. By naming your iterating variable "oEmail", you'd think that it contains the e-mail address as a string while actually it's just an index. A much more logical name would be something like "nEmail" or "iEmail" or just something as simple as "i".

Also, when you're dealing with arrays, it's much more effective to iterate with an incrementing number over the array length instead of using property enumeration. Looping over properties is fine for plain objects, but for arrays a generic for-loop from 0 to length is much faster.

Another thing I noticed is that you're checking the amount of contacts on every iteration. This is completely unnecessary and can easily be moved to the top of the function as preliminary check before the actual loop.

My suggestions:
Javascript code:
var oEmails = [
    'email1@hotmail.com',
    'email2@hotmail.com'
];
 
function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    // Don't do anything when this is not a one-to-one chat.
    if ( pChatWnd.Contacts.Count !== 1 ) return sMessage;
    // Loop through the e-mails array, the fast way.
    for ( var i=0, len = oEmails.length; i < len; ++i ) {
        if ( pChatWnd.Contacts.GetContact(oEmails[i]) ) {
            return '';
        }
    }
}

If you really care about speed and performance, you could even use the high-speed reverse while-loop!
Javascript code:
    // Loop through the e-mails array, the fastest way.
    var i = oEmails.length;
    while ( i-- ) {
        if (pChatWnd.Contacts.GetContact(oEmails[i])) {
            return '';
        }
    }

Now, this was already pretty fast, but we can go even faster. Instead of bothering the script engine with a call to GetContact() every time, we can first retrieve the e-mail address we're looking for and then compare the e-mail addresses as a string.
Javascript code:
function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    // Don't do anything when this is not a one-to-one chat.
    if ( pChatWnd.Contacts.Count !== 1 ) return sMessage;
    // Get the e-mail address to look for.
    var sEmail = new Enumerator(pChatWnd.Contacts).item().Email;
    // Loop through the e-mails array and look for this e-mail.
    var i = oEmails.length;
    while ( i-- ) {
        if ( sEmail === oEmails[i] ) {
            return '';
        }
    }
}

For more information about array loops and performance in JavaScript/JScript, have a look at this article on Ajaxian.

quote:
Originally posted by Barathrum
Now it works, but it sets messege for all to ' ' instead for only the email I set.
Hmm, that's pretty odd. Could you try the code snippets above and see if those do any better? (I don't have time to test these myself at the moment.)
RE: Help with very small script by CookieRevised on 03-19-2010 at 11:22 AM

* CookieRevised slaps both matti and matty :p

You both forget the most crucial thing though: compare the sOrigin parameter with the screenname of the user!

By not doing this you also remove the text from your own send messages.

Which is what is happening and what Barathrum means with:

quote:
Originally posted by Barathrum
Now it works, but it sets messege for all to ' ' instead for only the email I set.



;)
RE: Help with very small script by Barathrum on 03-19-2010 at 01:31 PM

Exactly, and the above still changed the messege for both to ' '


RE: Help with very small script by markee on 05-21-2010 at 01:59 PM

So that you have a working script...

JScript code:
var oEmails = [
    'email1@hotmail.com',
    'email2@hotmail.com'
];
 
function OnEvent_ChatWndReceiveMessage (pChatWnd, sOrigin, sMessage, nMessageKind) {
    if ( pChatWnd.Contacts.Count !== 1 || sOrigin === Messenger.MyName) return sMessage;
    for (i in oEmails) {
        if ( pChatWnd.Contacts.GetContact(oEmails[i]) ) {
            return '';
        }
    }
}