-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaretreply.user.js
223 lines (195 loc) · 7.37 KB
/
caretreply.user.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
// ==UserScript==
// @name Caret Reply
// @namespace http://tampermonkey.net/
// @version 1.2.0
// @description Automatically reply to messages by using ^Message
// @author The Flamin' Taco
// @include *://chat.meta.stackoverflow.com/rooms/*
// @include *://chat.meta.stackexchange.com/rooms/*
// @include *://chat.stackexchange.com/rooms/*
// @include *://chat.stackoverflow.com/rooms/*
// @include *://chat.askubuntu.com/rooms/*
// @include *://chat.serverfault.com/rooms/*
// @run-at document-end
// @grant none
// ==/UserScript==
/* global $ */
(function() {
'use strict';
var taco = window.taco = window.taco || {
installedScripts: []
};
taco.installedScripts.push("caretReply");
var caretReply = taco.caretReply = {};
caretReply.storedMessages = false;
caretReply.caretRegex = /^(\^\*(\d+)|\^\?(.*?\?+)|(\^+))(.*)/;
caretReply.indexers = []; // getMessage, which returns a text, uses indexers to find the reply. If match 2 is valid, indexer 0 is used. If 3 is valid, than 1. And so on.
caretReply.indexers[0] = function(txt) {
return caretReply.storedMessages.length - Number(txt);
};
caretReply.indexers[1] = function(txt) {
// I stole this from Stack Overflow.
// Thanks Bobince.
// http://stackoverflow.com/a/3561711/7170955
txt = txt.match(/(.*?)(\?+)/);
var newRegex = txt[1].toLowerCase()
.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
.split(/\\\*/)
.join(".*");
var c_count = txt[2].length;
var i = 0;
for (var i = caretReply.storedMessages.length - 1; i >= 0; i--) {
if (caretReply.storedMessages[i].textContent.toLowerCase().match(newRegex) || $(caretReply.storedMessages[i]).parent().parent().find(".tiny-signature").find(".username").text().toLowerCase().match(newRegex)) {
c_count--;
if (c_count <= 0)
return i;
}
}
return 0;
};
caretReply.indexers[2] = function(txt) {
return caretReply.storedMessages.length - txt.length;
};
caretReply.getMessageText = function(inp) {
var matches = inp.match(caretReply.caretRegex);
if (matches) {
return matches[matches.length - 1];
}
return inp;
};
caretReply.getMessage = function(inp) {
var matches = inp.match(caretReply.caretRegex);
if (matches)
for (var c = 2; c < matches.length - 1; c++)
if (matches[c])
return caretReply.storedMessages[caretReply.indexers[c - 2](matches[c])];
return false;
};
// You cannot even believe how difficult it was for me to find out how this works.
var nullFnc = ()=>{};
caretReply.setMessageText = function(id, text){
$.ajax({'type' : 'POST',
'url' : `/messages/${id}`,
'data' : fkey({'text' : text}),
'success': nullFnc,
'dataType' : 'json',
'error' : nullFnc})
}
caretReply.sedMatch = function(text){
text = text.split("");
if(text[0]!="s")
return false;
text.splice(0,1);
var key = text.splice(0,1)[0];
if(!key || !key.match(/[^a-zA-Z0-9 \\]/))
return false;
var matchID = -1;
for (matchID = 0; matchID < text.length; matchID++){
var s = text[matchID];
if(s == "\\"){ // If escape, skip the next character.
matchID++;
continue;
}
if(s == key){ // We have found it! The legendary MatchEnd!
break;
}
}
var match = text.splice(0, matchID).join("");
if(!match || matchID == text.length)
return;
text.splice(0,1);
var replaceID = -1;
for (replaceID = 0; replaceID < text.length; replaceID++){
var s = text[replaceID];
if(s == "\\"){ // If escape, skip the next character.
replaceID++;
continue;
}
if(s == key){ // Having a replacement is actually optional here.
break;
}
}
var hasFlags = replaceID < text.length;
var replace = text.splice(0, replaceID).join("");
if(!replace)
return false;
if(hasFlags){
text.splice(0,1);
return [match, replace, text.join("")]
}
return [match, replace, "gmi"];
}
$.fn.extend({
bindAs: function(nth, type, data, fn) {
if (type.indexOf(' ') > -1) {
var s = type.split(' ');
for (var i = 0; i < s.length; ++i)
this.bindAs(nth, s[i], data, fn);
return this;
}
if ($.isFunction(data) || data === false) {
fn = data;
data = undefined;
}
if (nth < 0)
nth = 0;
for (var i = 0; i < this.length; ++i) {
var elem = this[i];
$.event.add(elem, type, fn, data);
var events = $(elem).data(elem.nodeType ? 'events' : '__events__');
if (events && typeof events === 'function')
events = events.events;
if (events) {
var handlers = events[type],
offset = 0;
if (handlers.delegateCount)
offset = handlers.delegateCount;
if (handlers && handlers.length > offset + nth + 1)
handlers.splice(offset + nth, 0, handlers.splice(handlers.length - 1, 1)[0]);
}
}
return this;
}
});
var input = $("#input");
input.keyup(function(event) {
if (input.val().match(/^$/))
caretReply.storedMessages = false;
else if (!caretReply.storedMessages)
caretReply.storedMessages = $("[id^=message]");
});
//input.bindAs(0, 'keydown', function(event) {
var onKey = function(event){
if (event.which == 13) {
var text = input.val();
var olMessage = caretReply.getMessage(text);
if(!olMessage)
return;
var msgId = olMessage.getAttribute("id").replace(/message-/, "");
var msg = $("#message-"+msgId)[0];
if (!msg)
return;
if (caretReply.getMessageText(text) == "*") {
$(msg).find(".meta").find(".stars").find(".img.vote").click();
input.val("");
return;
}
var txt = caretReply.getMessageText(text);
// Custom function because JS Regex is evil and can't escape to save itself.
var notSed = caretReply.sedMatch(txt);
if (notSed){
$.get("/message/" + msgId + "?plain=true", function(e) {
caretReply.setMessageText(msgId, e.replace(new RegExp(notSed[0], notSed[2]), notSed[1]));
});
input.val("");
return;
}
input.val(":" + msgId + " " + txt);
}
};
input.bind("keydown", onKey);
var inpEvents = $._data(input[0],'events').keydown
//var evnt = inpEvents.splice(-1,1)[0]
//$._data(input[0],'events').keydown = [evnt].concat(inpEvents);
inpEvents.unshift(inpEvents.pop());
})();