This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.ls
256 lines (219 loc) · 7.21 KB
/
twitch.ls
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
VERSION = "1"
log = -> console?log? "IV-Twitch: #it"
err = -> throw new Error "IV-Twitch ERROR: #it"
colors = let
colors = <[
Blue Coral SpringGreen DodgerBlue YellowGreen Green OrangeRed Red
GoldenRod HotPink CadetBlue SeaGreen Chocolate BlueViolet Firebrick
]>
color-idx = 0
enabled: false
trigger: !->
return unless @enabled
CurrentChat.user_color colors[color-idx] if can-command!
if colors[color-idx + 1]
++color-idx
else color-idx := 0
old-messages = []
old-messages-idx = 0
old-usernames = []
chat-moderator = !->
return unless $j '#chat_lines'
if !CurrentChat.show_timestamps and CurrentChat.toggle_show_timestamps
toggle_checkbox? 'toggle_chat_timestamps'
CurrentChat.toggle_show_timestamps!
tempBan = '<span> <a href="#" class="normal_button tooltip chat_menu_btn" onclick="javascript:CurrentChat.timedBan(1);" title="Purge"><span class="glyph_only"><img src="https://dl.dropboxusercontent.com/u/30070822/ttvchat/trash.gif" /></span></a></span>';
$j tempBan .insertAfter '#chat_menu_timeout'
$j '#chat_menu_tools' .insertAfter '#chat_menu_op_tools'
Chat::insert_chat_line2 = Chat::insert_chat_line
last-chatter = ""
Chat::insert_chat_line = !(info) ->
if info.nickname is 'jtv' # info messages
@insert_chat_line2 info
return
if info.nickname is PP.login
# colors
colors.trigger! unless info.nickname is last-chatter
# chat history
old-messages.unshift info.message
old-messages-idx := 0
else
# username autocomplete
old-usernames
unless info.nickname in ..
..unshift info.nickname
..length-- if ..length > 50
# anticolors
if info.color.to-lower-case! in <[springgreen]>
info.color = 'blue'
# chat history
last-chatter := info.nickname
# twitch's behavior
@insert_chat_line2 info
# add purge button (not working atm ? :d)
if can-moderate and info.nickname not in [PP.login, CurrentChat.last_sender]
@insert_with_lock do
'.mod_button.timeout:last'
"""
<span
class="inline_purge"
title="Clear (purge messages from #{info.nickname})"
onclick="CurrentChat.say('/timeout #{info.nickname} 1')">
<img src="https://dl.dropboxusercontent.com/u/30070822/ttvchat/trash.gif" />
</span>
"""
can-moderate =
PP.login is PP.channel
or PP.is_admin is "true"
or PP.is_subadmin is "true"
or CurrentChat.staff[PP.login]
or CurrentChat.admins[PP.login]
or CurrentChat.moderators[PP.login]
banlist = []
CurrentChat.handlers.clear_chat = !({target, user}) ->
switch target
| 'all'
@admin_message "Chat cleared by a moderator (Prevented by IV-Twitch)."
| 'user'
nickname = sanitize-nickname user
return if nickname in banlist
idx = banlist.push nickname
(`setTimeout` 5000ms) <| !-> banlist.slice idx-1 1
$j "\#chat_line_list .chat_from_#nickname .chat_line"
.addClass 'banned_user_line'
@admin_message "#{twitch-link nickname}'s messages have been hidden by a moderator (IV-Twitch)."
| otherwise
log "Got a #target unhandled event"
function sanitize-nickname
it = CurrentChat.real_username it
it.replace /%/g '_' .replace /[<>,]/g '' .to-lower-case!
CurrentChat.timedBan = !(time) ->
CurrentChat.say "/timeout #{$j '#user_info .nick' .html!} #time"
create-box = !->
return unless settings-menu = $j '#chat_settings_dropmenu'
check-narrow = invert-strbool localStorage.narrowchat
check-color = invert-strbool localStorage.colorswitcher
bttvdiv = document.createElement 'div'
..setAttribute 'align' 'left'
..className = 'bttvdiv'
..innerHTML = """
<ul class="dropmenu_col inline_all">
<li id="chat_section_chatroom" class="dropmenu_section">
<br />
» IV-Twitch
<a class="dropmenu_action g18_gear-00000020" href="#" onclick="bttvAction('dark'); return false;">
Darken Chat
</a>
<a class="dropmenu_action g18_trash-00000020" href="#" onclick="bttvAction('clear'); return false;">
Clear My Chat
</a>
<p onclick="bttvAction('narrowchat'); document.getElementById('narrowchat').checked=#check-narrow;" class="dropmenu_action">
<input type="checkbox" id="narrowchat" class="left"> Narrow Chat
</p>
<p onclick="bttvAction('colorswitcher'); document.getElementById('colorswitcher').checked=#check-color;" class="dropmenu_action">
<input type="checkbox" id="colorswitcher" class="left"> Enable Auto Color Switcher
</p>
</li>
</ul>
"""
settings-menu.append bttvdiv
for setting in <[narrowchat colorswitcher]>
if localStorage[setting] is 'true'
$j "##setting" .attr 'checked' true
chat-history = !->
const KEY_ARROWUP = 38
KEY_ARROWDOWN = 40
<- $j '#chat_text_input' .keyup
return if @value and @value not in old-messages
return unless old-messages.length
switch it.which
| KEY_ARROWUP
message = old-messages[old-messages-idx++]
| KEY_ARROWDOWN
message = old-messages[old-messages-idx--]
| _ => return
@value = message ? old-messages[old-messages-idx := 0]
false
autocomplete-idx = 0
username-autocomplete = !->
const KEY_TAB = 9
$j '#chat_text_input' .on 'keydown' ->
unless it.which is KEY_TAB
return not autocomplete-idx := 0
username = (@value / /\b/g)pop!to-lower-case!
len = username.length
# scan for matches
matches = for old-username in old-usernames
if username is old-username.substr 0 len .to-lower-case!
old-username
unless matches.length
# got nothing =( ?
return not autocomplete-idx := 0
# let's process with what we got
if autocomplete-idx
@value -= /[a-z0-9_-]+:\s$/i
++autocomplete-idx
for old-username, i in matches
if i + 1 is autocomplete-idx
# todo check if there are multiple possibilities?
@value .= substr 0 (@value.length - len)
@value += "#{old-username.0.to-upper-case! + old-username.substr 1}#{[@value and ': ']}"
return false
false
over18 = !->
$j '#roadblock_button' ?.click!
init-options = !->
return unless localStorage.colorswitcher is 'true'
colors.enabled = true
log "Color switcher enabled"
delayed = !->
log "CALL:delayed"
chat-moderator!
chat-history!
username-autocomplete!
create-box!
# options
document.body.classList.add if localStorage.narrowchat is 'true'
'chat-narrowed'
else 'chat-expanded'
# checks
loc = document.URL
if ~loc.search 'meebo.html'
return console.log ':( meebo'
unless $j?
return console.log ':( $j'
# let's go !
log "CALL:init (from #loc)"
over18!
init-options!
setTimeout delayed, 1_000ms
# exports and functions
export bttv-action = !->
switch it
| 'dark'
$j '#twitch_chat' .addClass 'dark-chat'
| 'clear'
$j 'chat_line_list' .html ''
CurrentChat.admin_message "IV-Twitch: Cleared chat."
| _
localStorage[it] = invert-strbool localStorage[it]
switch it
| 'narrowchat'
window.location.reload!
| 'colorswitcher'
! = colors.enabled
last-command = Date.now!
function can-command
if Date.now! > last-command + 3_000ms
last-command := Date.now!
true
else
log "Command not executed because of timeout"
false
function twitch-link
"<a href='http://twitch.tv/#it'>#it</a>"
# inverts a string boolean
function invert-strbool
if it is 'true'
'false'
else 'true'