What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Help with very small script

Pages: (2): « First [ 1 ] 2 » Last »
Help with very small script
Author: Message:
Barathrum
Junior Member
**

Avatar
.......................

Posts: 42
30 / Male / Flag
Joined: Nov 2009
O.P. Help with very small script
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".
03-17-2010 09:33 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Help with very small script
Something like this?
js 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 '';
        }
    }
}
03-18-2010 12:29 AM
Profile E-Mail PM Find Quote Report
Barathrum
Junior Member
**

Avatar
.......................

Posts: 42
30 / Male / Flag
Joined: Nov 2009
O.P. RE: Help with very small script
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.
03-18-2010 05:16 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Help with very small script
Remove the comma (,) at the end of this line

js code:
var oEmails = [
    >>>'matejsturm57@hotmail.com',<<<
];

It should be:

js 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.
<Eljay> "Problems encountered: shit blew up" :zippy:
03-18-2010 05:26 PM
Profile PM Find Quote Report
Barathrum
Junior Member
**

Avatar
.......................

Posts: 42
30 / Male / Flag
Joined: Nov 2009
O.P. RE: Help with very small script
It still didn't change his messege :/
03-18-2010 08:12 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Help with very small script
OOPS!!!!!!!!!!!!!!!!!!

js 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.

This post was edited on 03-18-2010 at 08:28 PM by matty.
03-18-2010 08:27 PM
Profile E-Mail PM Find Quote Report
Barathrum
Junior Member
**

Avatar
.......................

Posts: 42
30 / Male / Flag
Joined: Nov 2009
O.P. RE: Help with very small script
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 '';
        }
    }
}
03-18-2010 09:06 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Help with very small script
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:
js 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!
js 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.
js 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.)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
03-18-2010 09:16 PM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Help with very small script
* 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.



;)

This post was edited on 03-19-2010 at 11:25 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
03-19-2010 11:22 AM
Profile PM Find Quote Report
Barathrum
Junior Member
**

Avatar
.......................

Posts: 42
30 / Male / Flag
Joined: Nov 2009
O.P. RE: Help with very small script
Exactly, and the above still changed the messege for both to ' '
03-19-2010 01:31 PM
Profile E-Mail PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On