1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Original code by /u/RenaKunisaki with /u/smog_alado's excelent notes 
// Spam button by /u/SRS-SRSLY. Chat Scroll Improvement by /u/schrobby
// Modified by /u/twitchyellow to add predicted button press history
// Compressed by http://javascriptcompressor.com.
// Doubt it still passes http://www.jslint.com on default settings - lol am not good prognammer 

// Identify the chat button
var chatButton = $("ul.segmented_tabs li a").first();

// Add spam button after the chat button
$("<li><a class='CommandsToggle'>Commands</a><a class='ChatToggle'>Talk</a></li>").insertAfter(chatButton);

// Reduce the width of the chat button by 71px.
// This gives enough space for a spam button width 30px with 15px margins with an extra pixel of wiggle room
chatButton.css("width", chatButton.width() - 71 + 5);

// Spam buttons make quick and dirty css rules to turn chat spam on or off. 
$(".CommandsToggle")
    .click(function () {
        "use strict";
        $("a.CommandsToggle").toggleClass("selected");
        removeAllButtonPresses();
        resetTask();

        if ($(".commandsHideCSS").length !== 0) {
            $(".commandsHideCSS").remove();
        } else {
            $("<style type='text/css' class='commandsHideCSS'>#chat_line_list li.cSpam{display:inline;}</style>").appendTo("head");
        }
    });

// Same for the Twitch Chat
$(".ChatToggle")
    .click(function () {
        "use strict";
        $("a.ChatToggle").toggleClass("selected");

        if ($(".chatHideCSS").length !== 0) {
            $(".chatHideCSS").remove();
        } else {
            $("<style type='text/css' class='chatHideCSS'>#chat_line_list li.cSafe{display:inline;}</style>").appendTo("head");
        }
    });

// Simulate a click on ChatToggle, so it starts in the "on" position.
$(".ChatToggle").click();

CurrentChat.line_buffer = 800;

//This part creates a CSS rule
//that hides all chat messages by default
var extraCSS =
    " <style type='text/css' >                                " +
    " .segmented_tabs li li a.CommandsToggle {                " +
    "     width: 50px;                                        " +
    "     padding-left: 0px;                                  " +
    "     padding-top: 0;                                     " +
    "     height: 8px;                                        " +
    "     line-height: 115%;                                  " +
    " }                                                       " +
    "                                                         " +
    " .segmented_tabs li li a.ChatToggle {                    " +
    "     width: 35px;                                        " +
    "     padding-left: 15px;                                 " +
    "     padding-top: 0;                                     " +
    "     height: 8px;                                        " +
    "     line-height: 115%;                                  " +
    " }                                                       " +
    "                                                         " +
    " #chat_line_list li {                                    " +
    "     display:none;                                       " +
    " }                                                       " +
    " </style>                                                ";

$(extraCSS)
    .appendTo("head"); // <- and adds the rule to the page

// Default milliseconds of lag we should account for
var lagMilliseconds = 26 * 1000;

// Identify the scroll content area
var scrollContent = $("#chat_lines");

// Insert the button press container before it
$("<div class='buttonPressContainer'></div>").insertBefore(scrollContent);

// The CSS for the button press bits and to move the scroll content to the right
var moreCSS = "" +
    "    <style type='text/css' >        " +
    "    #chat_line_list {               " +
    "        width:80%;                  " +
    "    }                               " +
    "                                    " +
    "    .lagCounter {                   " +
    "        z-index: 99999;             " +
    "        position:absolute;          " +
    "        overflow:hidden;            " +
    "        overflow-y:scroll;          " +
    "        right:5px;                  " +
    "        left:290px;                 " +
    "        top:0;                      " +
    "        bottom:0;                   " +
    "        text-align:center;          " +
    "    }                               " +
    "                                    " +
    "    .buttonPressContainer {         " +
    "        z-index: 99999;             " +
    "        position:absolute;          " +
    "        overflow:hidden;            " +
    "        overflow-y:scroll;          " +
    "        right:5px;                  " +
    "        left:290px;                 " +
    "        top:0;                      " +
    "        bottom:0;                   " +
    "        text-align:center;          " +
    "    }                               " +
    "                                    " +
    "    .buttonPress {                  " +
    "        -webkit-border-radius:5px;  " +
    "        -moz-border-radius:5px;     " +
    "        border-radius:5px;          " +
    "        background-color:#FDFDFD;   " +
    "        color:#121212;              " +
    "        font-size:1em;              " +
    "        text-align:center;          " +
    "        display:block;              " +
    "        margin:3px 0px;             " +
    "        padding:0px 0px;            " +
    "    }                               " +
    "                                    " +
    "    .expiredPress {                 " +
    "        opacity: 0.3;               " +
    "    }                               " +
    "    </style>                        ";

// Actually add the CSS
$(moreCSS)
    .appendTo("head"); // <- and adds the rule to the page

var buttonPressContainer = $(".buttonPressContainer").first();

// Reference to the timer task
var task;

resetTask();

var lagTimerStart = 0;

// Setup the listener for sending messages so the user can time lag if they want to
$("#chat_text_input").keyup(function(event) {
    if((event.ctrlKey || event.altKey) && event.which === 13) {
        lagTimerStart = new Date().getTime();
        $(chatButton).text("Stop lag timer");
        $(chatButton).click(onStopLagTimer);
    }
});

// Stop the timer with escape as well
$(document).keydown(function(event) {
    if(event.which === 27) {
        if($(chatButton).text() === "Stop lag timer")
            onStopLagTimer(null);
    }
});

function onStopLagTimer(event) {
    $(chatButton).unbind("click", onStopLagTimer);
    $(chatButton).text("Chat");
    lagMilliseconds = new Date().getTime() - lagTimerStart; 
}

function cancelTask() {
    if (task != null)
        clearInterval(task);
}

// setInterval makes this part of the code run periodically
function resetTask() {
    if (task != null)
        clearInterval(task);

    task = setInterval(function () {
        "use strict";

        var buttonCount = {
            up: 0,
            down: 0,
            left: 0,
            right: 0,
            a: 0,
            b: 0,
            start: -1,
            select: 0
        };

        // The `#chat_line_list` references the chat box
        // and the `li` references the individual chat items inside it.
        // Thus, run for each chat box item,
        // This part of the script is the part where it sends chat commands  
        // to release Abby and Jay Leno. Hail Dome Fossil. Hail Flareon. 
        $('#chat_line_list li:not(.cSpam):not(.cSafe)')
            .each(function () {

                // cLine is a reference a single line in chat.
                // cLine.text gets the chat text
                // split(':') breaks the text into the username and the message
                // [1] selects the chat message (rather than the username)
                // In this way cText is cLine's (trimmed) message.

                var cLine = $(this),
                    cText = cLine.find(".chat_line").text().toLowerCase();

                // Use a regular expression to see if the message matches
                // a pattern. If it's not (!) a message that 
                //  starts (^)
                //  is followed by one of the commands 
                //    (up|down|...)
                //  and immediately ends ($)
                // Then we show the message.
                // Once we get to Cinnabar Island, this section revives that fool Helix,
                // then releases it into the wild! Hail Dome Fossil! Hail Flareon!
                if (cText && !cText.match(/^(north|east|south|west|up|down|left|right|a|b|start|select?)$/i)) {
                    cLine.addClass("cSafe");
                } else {
                    cLine.addClass("cSpam");
                    if(cText === "north") cText = "up";
                    else if(cText === "east") cText = "right";
                    else if(cText === "south") cText = "down";
                    else if(cText === "west") cText = "left";
                    buttonCount[cText]++;
                }
            });

        var highest = null;

        for (var button in buttonCount) {
            if ((highest == null && buttonCount[button] > 0) || buttonCount[button] > buttonCount[highest])
                highest = button;
        }

        if (highest != null) {
            if (highest === "up")
                addButtonPress("↑");
            else if (highest === "down")
                addButtonPress("↓");
            else if (highest === "left")
                addButtonPress("←");
            else if (highest === "right")
                addButtonPress("→");
            else
                addButtonPress(highest);
        }
        // Uncomment this to show no-button presses
        //else
        //    addButtonPress("-");

        if (CurrentChat.currently_scrolling) {
            CurrentChat.scroll_chat();
        }

        updateColours();
    }, 1000); // <- run every second
}

// Add a new button press to the list
function addButtonPress(button) {

    // If we've reached the maximum button presses to show then remove the oldest one
    if (buttonPressContainer.children().length > 50) {
        buttonPressContainer.children(":first").fadeOut().remove();
    }

    // Create the new button press div
    var buttonPress = $("<div class='buttonPress' data-timestamp=" + new Date().getTime() + ">" + button + "</div>");
    $(buttonPress).hide();
    $(buttonPressContainer).append(buttonPress);
    $(buttonPress).fadeIn();

    // Scroll down to the latest button press if not already
    $(buttonPressContainer).animate({
        scrollTop: $(buttonPressContainer).height()
    }, "slow");
}

function updateColours() {
    var time = new Date().getTime();

    $(buttonPressContainer).children("div").each(function () {
        if (!$(this).hasClass("expiredPress") && (time - $(this).data("timestamp")) > lagMilliseconds) {
            $(this).addClass("expiredPress");
        }
    });
}

function removeAllButtonPresses() {
    $(buttonPressContainer).empty();
}