-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatstack.js
326 lines (256 loc) · 9.65 KB
/
chatstack.js
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var ChatStack = {
pushUrl: ['http://push.st/', 'http://push.st'],
popUrl: ['http://pop.st/', 'http://pop.st'],
isOTR: false,
install: function() {
if(document.body.className == 'xE') {
// single window mode
var chat = document.getElementsByClassName('kf');
if(chat.length == 0) {
setTimeout(ChatStack.install, 1000);
return;
}
ChatStack.register(chat[0]);
} else {
// this is the root element for all the chat windows, for normal mode
var chats = document.getElementsByClassName('no');
if(chats.length == 0) {
setTimeout(ChatStack.install, 1000);
return;
}
chats[0].addEventListener('DOMNodeInserted', ChatStack.detectChats, true);
}
},
detectChats: function(e) {
// only chat logs have the role attribute set to 'log'
if(e.target.attributes && e.target.attributes.role && e.target.attributes.role.value == 'log')
ChatStack.register(e.target.firstChild);
},
register: function(e) {
var content = document.createElement('div');
e.stack = [];
e.labelStack = [];
var rootLabel = {label: 'root', contents: e, chat: e, link: document.createElement('div')};
e.labelStack.push(rootLabel);
var title = e.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByClassName('NI')[0];
var stackSelector = document.createElement('span');
stackSelector.className = 'stackSelector NE';
stackSelector.appendChild(document.createTextNode('root ▼'));
var selectorWindow = document.createElement('div');
selectorWindow.className = 'selectorWindow';
selectorWindow.style.display = 'none';
rootLabel.link.className = 'stackElement selected';
rootLabel.link.appendChild(document.createTextNode('root'));
rootLabel.link.addEventListener('click', function() {
ChatStack.selectLabel(rootLabel, false);
}, true);
selectorWindow.appendChild(rootLabel.link);
stackSelector.addEventListener('click', function(e) {
e.stopPropagation();
e.preventDefault();
if(selectorWindow.style.display == 'none') {
selectorWindow.style.display = '';
} else {
selectorWindow.style.display = 'none';
}
}, false);
stackSelector.appendChild(selectorWindow);
e.selector = stackSelector;
e.selectorWindow = selectorWindow;
title.appendChild(stackSelector);
title.appendChild(selectorWindow);
// acting upon the complete chat history didn't go as well as expected, so
// let's move all the nodes from the chat log to a temporary container, in
// order to add them back, message by message.
while(e.firstChild) {
var child = e.firstChild;
e.removeChild(child);
content.appendChild(child);
}
// we have to traverse the chat window's history to detect old links
while(content.firstChild) {
var child = content.firstChild;
content.removeChild(child);
e.appendChild(child);
ChatStack.interceptLinks(child);
}
// we have to let the DOM finish inserting all the node's children to be
// able to detect the other person's links correctly. Also avoids some
// timing issues where the magic links are linkefied before displaying them
// on the screen.
e.addEventListener('DOMNodeInserted', function(x) {
setTimeout(function() { ChatStack.interceptLinks(x.target); }, 100);
}, true);
},
interceptLinks: function(link) {
if(link.nodeName == 'A') {
// avoids double action on links. haven't seen it, but it's possible
// under some scenarios.
if(link.chatStackMangled) return;
link.chatStackMangled = true;
if(ChatStack.pushUrl.indexOf(link.href) != -1) {
ChatStack.push(link);
} else if(ChatStack.popUrl.indexOf(link.href) != -1) {
ChatStack.pop(link);
}
} else if (link.getElementsByTagName) {
// first check if it is an OTR message
if(link.className == 'kq') {
// this is a "system message"
if(link.innerHTML.indexOf('answer=29291') != -1) {
// OTR
ChatStack.isOTR = true;
ChatStack.push(link);
} else if(link.innerHTML.indexOf('no longer') != -1) {
// not OTR
ChatStack.pop(link);
ChatStack.isOTR = false;
}
} else {
// if gtalk was too quick and linkefied all the urls BEFORE adding them
// to the DOM tree, let's detect links within messages.
var links = link.getElementsByTagName('a');
for(i = 0; i < links.length; i++) {
ChatStack.interceptLinks(links[i]);
}
}
}
},
getChat: function(node) {
while(node && !node.stack) node = node.parentNode;
return node;
},
getMessage: function(node) {
// fixed a bug that only happens in *some* computers. weird.
while(node && !(node.attributes && node.attributes.role && node.attributes.role.nodeValue == 'chatMessage'))
node = node.parentNode;
return node;
},
addClass: function(node, className) {
if(node.className.indexOf(className) == -1) {
if(node.className.length == 0) node.className = className;
else node.className += ' ' + className;
}
},
removeClass: function(node, className) {
if(node.className.indexOf(className) != -1) {
node.className = node.className
.replace(className, '')
.replace(/ +/g, " ")
.replace(/^ /, "")
.replace(/ $/, "");
}
},
replaceClass: function(node, before, after) {
ChatStack.removeClass(node, before);
ChatStack.addClass(node, after);
},
selectLabel: function(label, force) {
if(force || label.contents.style.display != '') {
for(i = 0; i < label.chat.labelStack.length; i++) {
ChatStack.removeClass(label.chat.labelStack[i].link, 'selected');
label.chat.labelStack[i].contents.style.display = 'none';
}
ChatStack.addClass(label.link, 'selected');
label.contents.style.display = '';
label.chat.selector.firstChild.nodeValue = label.label + ' ▼';
}
label.chat.selectorWindow.style.display = 'none';
},
push: function(node) {
var chat = ChatStack.getChat(node);
var context = "";
// deactivate interception while we are mangling with the DOM. don't want
// a link to activate while we are popping it back.
if(!chat || chat.cancelInterception) return;
chat.cancelInterception = true;
if(node.previousSibling && node.previousSibling.nodeValue) {
context = node.previousSibling.nodeValue.replace(/^\s+|\s+$/g,"");
}
if(ChatStack.isOTR) {
context += ' (OTR)';
}
var oldContents = document.createElement('div');
var msg = ChatStack.getMessage(node);
while(chat.firstChild && chat.firstChild != msg) {
var child = chat.firstChild;
chat.removeChild(child);
oldContents.appendChild(child);
}
oldContents.style.display = 'none';
oldContents.className = 'ko oldContent';
chat.stack.push(oldContents);
var label = {label: context, contents: chat, chat: chat, link: document.createElement('div')};
chat.labelStack[chat.labelStack.length - 1].contents = oldContents;
chat.labelStack.push(label);
label.link.className = 'stackElement';
label.link.appendChild(document.createTextNode(context));
label.link.addEventListener('click', function() {
ChatStack.selectLabel(label, false);
}, true);
chat.selectorWindow.appendChild(label.link);
chat.parentNode.insertBefore(oldContents, chat);
ChatStack.selectLabel(label, true);
chat.cancelInterception = false;
},
pop: function(node) {
var chat = ChatStack.getChat(node);
// deactivate interception while we are mangling with the DOM. don't want
// a link to activate while we are popping it back.
if(!chat || chat.stack.length == 0 || chat.cancelInterception) return;
chat.cancelInterception = true;
var oldContents = chat.stack.pop();
var newContents = document.createElement('div');
var msg = ChatStack.getMessage(node);
while(chat.firstChild && chat.firstChild != msg) {
var child = chat.firstChild;
chat.removeChild(child);
newContents.appendChild(child);
}
while(oldContents.firstChild) {
var child = oldContents.firstChild;
oldContents.removeChild(child);
chat.insertBefore(child, msg);
}
var label = chat.labelStack.pop();
if(newContents.children.length != 0) {
// only insert an awesome-looking collapsed version of the pushed context
// if it actually has anything. i.e. if the push and pop commands are on
// the same chat block, it isn't worth collapsing an empty block.
var stackContainer = document.createElement('div');
stackContainer.className = 'stackContainer';
var toggleLink = document.createElement('div');
toggleLink.appendChild(document.createTextNode(label.label));
toggleLink.isOTR = ChatStack.isOTR;
toggleLink.className = 'toggle expand';
if(toggleLink.isOTR) {
ChatStack.addClass(toggleLink, 'otr');
}
toggleLink.addEventListener('click', function() {
if(newContents.style.display == 'none') {
newContents.style.display = '';
ChatStack.replaceClass(toggleLink, 'expand', 'collapse');
} else {
newContents.style.display = 'none';
ChatStack.replaceClass(toggleLink, 'collapse', 'expand');
}
}, true);
stackContainer.appendChild(toggleLink);
newContents.className = 'nested';
newContents.style.display = 'none';
stackContainer.appendChild(newContents);
chat.insertBefore(stackContainer, msg);
}
label.link.parentNode.removeChild(label.link);
var oldContents = chat.labelStack[chat.labelStack.length - 1].contents;
oldContents.parentNode.removeChild(oldContents);
chat.labelStack[chat.labelStack.length - 1].contents = chat;
ChatStack.selectLabel(chat.labelStack[chat.labelStack.length - 1], true);
chat.parentNode.parentNode.scrollTop = chat.parentNode.parentNode.scrollHeight;
setTimeout(function() {
chat.parentNode.parentNode.scrollTop = chat.parentNode.parentNode.scrollHeight;
}, 2000);
chat.cancelInterception = false;
}
};
ChatStack.install();