What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Caching which windows have a command enabled

Caching which windows have a command enabled
Author: Message:
MrPickle
New Member
*


Posts: 5
Joined: Nov 2008
O.P. Caching which windows have a command enabled
Here's how I thought to do it, but as I don't have a wide knowledge of JavaScript (Only C++) there may be a better way (hence me asking).

Create an object containing the window's handle and a "command on/off" boolean when a window is created, then add the object to an array of the active windows.

When the command is called, eg; /command on, loop through the array of active windows checking the window's handle to the current array object's handle and check if "command on/off" is false, if it is, set it to true.

Same for /command off except changing true, to false.

When the window is destroyed remove which ever object in the array has the same handle.

Another way I thought to do it is using the windows handle as a string in the array but I'm not sure whether JavaScript supports this. I am unsure of the correct term so here's an example:

JScript code:
var fruit = new Array();
fruit['banana'] = "Yellow";
fruit['apple'] = "Red";
fruit['tomatoe'] = "Red"




11-18-2008 08:14 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Caching which windows have a command enabled
Sure does:

JScript code:
// Declare an object to hold our ChatWindows
var oChatWnds = {};
 
// An event is fired because a chat window was created
function OnEvent_ChatWndCreated(oChatWnd) {
    // Create an entry in the object for the chat window
    oChatWnds[oChatWnd.Handle] = {};
        // Store the chat window's object in our oChatWnds object
        oChatWnds[oChatWnd.Handle].objChatWnd = oChatWnd;
        // Create a read/write property for the on/off
        oChatWnds[oChatWnd.Handle].bCommand = true;
}
 
// An event is fired because a chat window was closed
function OnEvent_ChatWndDestroyed(oChatWnd) {
    // Remove the chat window from the object
    delete oChatWnds[oChatWnd.Handle];
}
 
// An event is fired because we sent a message
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
    // Perform a regexp function to split the command and params
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
       
        // Declare local variables for the command and the parameters
        var Command = RegExp.$1.toLowerCase();
        var Param = RegExp.$2;
       
        // Check if the command is one of ours
        if (Command === 'command') {
            // If it is then change the variable based on what the paramter is set to
            oChatWnds[oChatWnd.Handle].bCommand = (Param === 'on' ? true : false);
            // Return a null string to the function
            return '';
        }
    // Return the message if we are doing nothing with it
    return sMessage;
}
 
// Called when the user presses / in the chat window
function OnGetScriptCommands() {
    // Make sure 'command' is a valid command so Plus! doesn't bark
    return '<ScriptCommands><Command><Name>command</Name></Command></ScriptCommands>';
}


This post was edited on 11-18-2008 at 09:03 PM by matty.
11-18-2008 08:52 PM
Profile E-Mail PM Find Quote Report
MrPickle
New Member
*


Posts: 5
Joined: Nov 2008
O.P. RE: Caching which windows have a command enabled
Just for example, say that oChatWnd.Handle is 1054, when you do this: oChatWnds[oChatWnd.Handle], it's looking at the 1054th address, what happened to the other 1053? Isn't this going to waste a huge amount of memory?

So what I'm basically asking is, will JavaScript initialize all of the array's adresses, 0 - 1054, and leave any unused empty, or will it simply move 1054 to whatever's the next available slot and then create a pointer in the previous address to the new address?

This post was edited on 11-18-2008 at 09:39 PM by MrPickle.
11-18-2008 09:39 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
RE: Caching which windows have a command enabled
quote:
Originally posted by MrPickle
Just for example, say that oChatWnd.Handle is 1054, when you do this: oChatWnds[oChatWnd.Handle], it's looking at the 1054th address, what happened to the other 1053? Isn't this going to waste a huge amount of memory?

So what I'm basically asking is, will JavaScript initialize all of the array's adresses, 0 - 1054, and leave any unused empty, or will it simply move 1054 to whatever's the next available slot and then create a pointer in the previous address to the new address?

JScript uses sparse arrays...

JScript code:
for(var s in oChatWnds){
    Debug.Trace(oChatWnds[s])
}


will only display 1054... it will not loop through all of them. It wouldn't really be a speed issue that you'd notice if it wasn't this way tbh.



EDIT:

quote:
Originally posted by Spunky
what happened to the other 1053?

You mean 1054; JScript arrays are zero-based :p

This post was edited on 11-18-2008 at 10:41 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
11-18-2008 10:37 PM
Profile PM Find Quote Report
MrPickle
New Member
*


Posts: 5
Joined: Nov 2008
O.P. RE: Caching which windows have a command enabled
I did mean 1053, because I was excluding 1054 :)

If it's sparse arrays then it is wasting memory I think. Well if I'm understanding Wiki correctly then it is wasting memory. http://en.wikipedia.org/wiki/Sparse_array
11-18-2008 10:48 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Caching which windows have a command enabled
It isn't an array so you aren't wasting memory.
11-18-2008 11:07 PM
Profile E-Mail PM Find Quote Report
MrPickle
New Member
*


Posts: 5
Joined: Nov 2008
O.P. RE: Caching which windows have a command enabled
What is it then, if it's not an array?

Every langauge I've ever worked in oChatWnds[oChatWnd.Handle] would be an array. :P

Would this be acceptable.
JScript code:
var Windows = new Array();
 
function OnEvent_ChatWndCreated(ChatWnd) {
    wnd = {};
    wnd.Handle = ChatWnd.Handle;
    wnd.bCommand = false;
    Windows.push_back(wnd);
}


This would mean you wouldn't have massive gaps but you'd have to loop through the chats.

Going back to one of my first questions, can the value in between the square brackets be a string?
11-18-2008 11:19 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
RE: Caching which windows have a command enabled
quote:
Originally posted by MrPickle
can the value in between the square brackets be a string?

Yes.

quote:
Originally posted by MrPickle
What is it then, if it's not an array?

I think they're technically objects. The code you posted should work fine (except for the push_back method... I don't think it exists in JScript)
<Eljay> "Problems encountered: shit blew up" :zippy:
11-19-2008 01:17 AM
Profile PM Find Quote Report
MrPickle
New Member
*


Posts: 5
Joined: Nov 2008
O.P. RE: Caching which windows have a command enabled
It's ment to be push, not push_back, sorry.
http://www.w3schools.com/jsref/jsref_push.asp

I'm unsure how it will arrange the elements as strings, if it does it nicely I have an alternative method. :)

Thanks for you helps.
11-19-2008 08:25 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Caching which windows have a command enabled
It's not an array, it's a regular object.
quote:
Originally posted by matty
JScript code:
// Declare an object to hold our ChatWindows
var oChatWnds = {};


Basically, you're not defining an element in a zero-based numerical indexed array, you're setting an object property of a regular object. If you were going to use:
JScript code:
var oChatWnds = [];

and then set and delete the 1053th element, yes, then you'd be wasting memory and processing time. But here, we only define one object property which simply happens to have a numeric property name - no problems at all.

JavaScript is very lax at such kind of stuff, unlike C++ where you can't make arrays with mixed element types. Compare JavaScript's object creation (like in this example) with extending an empty class: you start with nothing and add stuff in it. You won't create more properties in the class than you want (compared to JavaScript arrays), there's no length property or not-enumerable methods like push and slice. You simply extend an empty object. :)

quote:
Originally posted by MrPickle
Would this be acceptable.
...
This would mean you wouldn't have massive gaps but you'd have to loop through the chats.
Correct, but it's much faster and efficient to have them indexed by handle in an object than looping through an array and checking the Handle property. Also, in your example code, there's no way to get access to the ChatWnd object itself through the array, however it could be you simply forgot that. :P
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
11-19-2008 12:44 PM
Profile E-Mail PM Web Find Quote Report
« 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