forked from tamtamchik/mush-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leproWatch.user.js
514 lines (434 loc) · 20.3 KB
/
leproWatch.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// ==UserScript==
// @name LeproWatch
// @namespace http://tamtamchika.net/
// @version 1.3.0
// @grant unsafeWindow
// @description Saves all logs.
// @author tamtamchik
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @require https://code.jquery.com/jquery-2.2.1.min.js
// @require https://raw.githubusercontent.com/cowboy/jquery-throttle-debounce/v1.1/jquery.ba-throttle-debounce.min.js
// @include http://mush.twinoid.com/
// @include http://mush.vg/
// @downloadURL https://github.com/tamtamchik/mush-scripts/raw/master/leproWatch.user.js
// ==/UserScript==
/* jshint ignore:start */
var inline_src = (<><![CDATA[
/* jshint ignore:end */
/* jshint esnext: false */
/* jshint esversion: 6 */
// Base variables and configurations.
//noinspection JSUnresolvedVariable
const console = unsafeWindow.console;
//noinspection JSUnresolvedVariable
const Main = unsafeWindow.Main;
//noinspection JSUnresolvedFunction, JSUnresolvedVariable
Main.LeproWatch = createObjectIn(unsafeWindow.Main, { defineAs: 'LeproWatch' });
//noinspection JSUnresolvedVariable
Main.LeproWatch.version = GM_info.script.version || "1.2.5";
//noinspection JSUnresolvedVariable
Main.LeproWatch.indexedDB = unsafeWindow.indexedDB || unsafeWindow.mozIndexedDB || unsafeWindow.webkitIndexedDB || unsafeWindow.msIndexedDB;
Main.LeproWatch.decs = 'Log collector by @tamtamchik. Leprosorium casting!';
const info = (message) => console.log(message);
const error = (message) => console.error(message);
if (window.location.href.indexOf('mush.twinoid.com') !== -1) {
Main.LeproWatch.language = 'en';
Main.LeproWatch.lang = 2;
Main.LeproWatch.urlMush = "mush.twinoid.com";
Main.LeproWatch.roomNames = ['Bridge', 'Alpha Bay', 'Bravo Bay', 'Alpha Bay 2', 'Nexus', 'Medlab', 'Laboratory', 'Refectory', 'Hydroponic Garden', 'Engine Room',
'Front Alpha Turret', 'Centre Alpha Turret', 'Rear Alpha Turret', 'Front Bravo Turret', 'Centre Bravo Turret', 'Rear Bravo Turret',
'Patrol Ship Tomorrowland', 'Patrol Ship Olive Grove', 'Patrol Ship Yasmin', 'Patrol Ship Wolf', 'Patrol Ship E-Street', 'Patrol Ship Eponine', 'Patrol Ship Carpe Diem', 'Pasiphae',
'Front Corridor', 'Central Corridor', 'Rear Corridor', 'Planet', 'Icarus Bay', 'Alpha Dorm', 'Bravo Dorm',
'Front Storage', 'Centre Alpha Storage', 'Rear Alpha Storage', 'Centre Bravo Storage', 'Rear Bravo Storage', 'Outer Space', 'Limbo'];
}
else if (window.location.href.indexOf('mush.twinoid.es') !== -1) {
Main.LeproWatch.language = 'es';
Main.LeproWatch.lang = 3;
Main.LeproWatch.urlMush = "mush.twinoid.es";
Main.LeproWatch.roomNames = ['Puente de mando', 'Plataforma Alpha', 'Plataforma Beta', 'Plataforma Alpha 2', 'Nexus', 'Enfermería', 'Laboratorio', 'Comedor', 'Jardín Hidropónico', 'Sala de motores',
'Cañón Alpha delantero', 'Cañón Alpha central', 'Cañón Alpha trasero', 'Cañón Beta delantero', 'Cañón Beta central', 'Cañón Beta trasero',
'Patrullero Longane', 'Patrullero Jujube', 'Patrullero Tamarindo', 'Patrullero Sócrates', 'Patrullero Epicuro', 'Patrullero Platón', 'Patrullero Wallis', 'Pasiphae',
'Pasillo delantero', 'Pasillo central', 'Pasillo trasero', 'Planeta', 'Icarus', 'Dormitorio Alpha', 'Dormitorio Beta',
'Almacén delantero', 'Almacén Alpha central', 'Almacén Alpha trasero', 'Almacén Beta central', 'Almacén Beta trasero', 'Espacio infinito', 'El limbo'];
}
else {
Main.LeproWatch.language = '';
Main.LeproWatch.lang = 1;
Main.LeproWatch.urlMush = "mush.vg";
Main.LeproWatch.roomNames = ['Pont', 'Baie Alpha', 'Baie Beta', 'Baie Alpha 2', 'Nexus', 'Infirmerie', 'Laboratoire', 'Réfectoire', 'Jardin Hydroponique', 'Salle des moteurs',
'Tourelle Alpha avant', 'Tourelle Alpha centre', 'Tourelle Alpha arrière', 'Tourelle Beta avant', 'Tourelle Beta centre', 'Tourelle Beta arrière',
'Patrouilleur Longane', 'Patrouilleur Jujube', 'Patrouilleur Tamarin', 'Patrouilleur Socrate', 'Patrouilleur Epicure', 'Patrouilleur Planton', 'Patrouilleur Wallis', 'Pasiphae',
'Couloir avant', 'Couloir central', 'Couloir arrière', 'Planète', 'Baie Icarus', 'Dortoir Alpha', 'Dortoir Beta',
'Stockage Avant', 'Stockage Alpha centre', 'Stockage Alpha arrière', 'Stockage Beta centre', 'Stockage Beta arrière', 'Espace infini', 'Les Limbes'];
}
// Model layer.
/**
* Connect to the database.
*/
Main.LeproWatch.connectToDatabase = () => {
info('[LeproWatch] Connecting to database...');
Main.LeproWatch.db = Main.LeproWatch.indexedDB.open(`leprowatch_${Main.LeproWatch.language}`, 3);
Main.LeproWatch.db.onerror = (e) => error(e);
Main.LeproWatch.db.onsuccess = (c) => {
Main.LeproWatch.connection = c.target.result;
Main.LeproWatch.emitEvent('lw:connected');
};
Main.LeproWatch.db.onupgradeneeded = (c) => {
//noinspection JSUnresolvedFunction
c.target.result
.createObjectStore('logs', { keyPath: 'id' })
.createIndex('room', 'room', { unique: false });
Main.LeproWatch.connectToDatabase();
};
};
Main.LeproWatch.getTimes = (time) => {
let t, c, a;
if (time == 'moments ago') {
t = 0;
c = 60000;
a = '30 sec';
} else if (time.indexOf('min') != -1) {
t = parseInt(time.substring(0, time.length - 3));
c = 60000;
a = '30 sec';
} else if (time.indexOf('h') != -1) {
t = parseInt(time.substring(1, time.length - 1));
c = 3600000;
a = '30 min';
} else if (time.indexOf('d') != -1) {
t = parseInt(time.substring(1, time.length - 1));
c = 86400000;
a = '12 hours';
} else {
// something strange
//debugger;
}
return { timestamp: t * c + (c / 2 | 0), accuracy: a };
};
Main.LeproWatch.setTimes = (time) => {
const t = Date.now() - time;
let result;
if (t < 59999) {
result = 'moments ago';
} else if (t < 3600000) {
result = (t / 60000 | 0) + 'min';
} else if (t < 86400000) {
result = '~' + (t / 3600000 | 0) + 'h';
} else {
result = '~' + (t / 86400000 | 0) + 'd';
}
return result;
};
Main.LeproWatch.formatTime = (time) => {
const d = new Date(time);
const year = d.getFullYear();
let day = d.getDate();
if (day < 10) {
day = "0" + day;
}
let month = d.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let hours = d.getHours();
if (hours < 10) {
hours = "0" + hours;
}
let minutes = d.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
let seconds = d.getSeconds();
if (seconds < 10) {
seconds = "0" + seconds;
}
return day + "." + month + "." + year + " " + hours + ":" + minutes + ":" + seconds;
};
Main.LeproWatch.collectLogs = () => {
console.log('[LeproWatch] Getting logs...');
if (!Main.LeproWatch.connection) return;
const records = Array.from(document.querySelectorAll('#localChannel .cdChatLine')).reverse();
const logs = records.map((el, index) => {
const html = el.innerHTML
.replace(/\s+/ig, ' ')
.replace(/<span class="ago">.*?<\/span>/ig, '')
.replace(/<img src="\/\/data\.mush\.twinoid\.com\/img\/icons\/ui\/recent\.png".*?>/ig, '')
.trim();
const classes = Array.from(el.classList).filter(item => item !== 'not_read');
const times = Main.LeproWatch.getTimes(el.getElementsByClassName("ago")[0].innerHTML);
//noinspection JSUnresolvedVariable
return {
'id': parseInt(el.dataset.id),
'cycle': parseInt(el.dataset.c),
'room': Main.LeproWatch.getRoomId(),
'html': html,
'time': Date.now() - times.timestamp,
'accuracy': times.accuracy,
'text': el.textContent.trim(),
'index': index,
'classes': classes.join(' ')
};
});
Main.LeproWatch.saveLogs(logs);
};
Main.LeproWatch.saveLogs = (logs) => {
console.log('[LeproWatch] Saving logs...');
//noinspection JSCheckFunctionSignatures
let transaction = Main.LeproWatch.connection.transaction(['logs'], 'readwrite');
//noinspection JSUnresolvedFunction
let logStore = transaction.objectStore('logs');
for (let i = 0; i < logs.length; i++) {
const req = logStore.get(parseInt(logs[i].id));
req.onsuccess = (e) => {
if (!e.target.result) {
const req2 = logStore.add(logs[i]);
req2.onerror = e => error(e);
}
};
req.onerror = e => error(e);
}
};
Main.LeproWatch.search = (text) => {
console.log('[LeproWatch] Searching for "' + text + '"...');
if (text === '') {
Main.LeproWatch.loadLogs();
return;
}
//noinspection JSCheckFunctionSignatures
const transaction = Main.LeproWatch.connection.transaction(['logs'], 'readonly');
//noinspection JSUnresolvedFunction
const logStore = transaction.objectStore('logs');
let rows = [];
//noinspection JSUnresolvedFunction
logStore.openCursor().onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
if (cursor.value.text.toLowerCase().includes(text.toLowerCase())) {
rows.push(cursor.value);
}
//noinspection JSUnresolvedFunction
cursor.continue();
} else {
if (rows.length === 0) {
Main.LeproWatch.clearLogView();
return;
}
Main.LeproWatch.renderLogs(rows, true);
}
};
};
// Helpers.
Main.LeproWatch.getRoomId = () => {
info('[LeproWatch] Getting room id...');
//noinspection JSUnresolvedVariable
return Main.LeproWatch.roomNames.indexOf(document.querySelector('#input').attributes.d_name.value);
};
Main.LeproWatch.getSelectedRoomId = () => {
console.log('[LeproWatch] Getting selected room id...');
return parseInt(document.querySelector('.roomChanger').value);
};
Main.LeproWatch.emitEvent = (name) => {
console.log('[LeproWatch] Emitting event ' + name + '...');
document.dispatchEvent(new Event(name));
};
Main.LeproWatch.loadLogs = (room) => {
room = (Number.isInteger(room)) ? room : Main.LeproWatch.getSelectedRoomId();
Main.LeproWatch.collectLogs();
console.log('[LeproWatch] Loading logs for room ' + Main.LeproWatch.roomNames[room] + '...');
//noinspection JSUnresolvedVariable,JSUnresolvedFunction
const roomRange = IDBKeyRange.only(room);
//noinspection JSCheckFunctionSignatures
const transaction = Main.LeproWatch.connection.transaction(['logs'], 'readonly');
//noinspection JSUnresolvedFunction
const logStore = transaction.objectStore('logs').index('room');
let rows = [];
//noinspection JSUnresolvedFunction
logStore.openCursor(roomRange).onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
rows.push(cursor.value);
//noinspection JSUnresolvedFunction
cursor.continue();
} else {
Main.LeproWatch.renderLogs(rows);
}
};
};
// UI functions
Main.LeproWatch.resetLogs = () => {
console.log('[LeproWatch] Reset logs...');
//noinspection JSCheckFunctionSignatures,JSUnresolvedFunction
Main.LeproWatch.connection.transaction(['logs'], 'readwrite').objectStore('logs').clear();
Main.LeproWatch.clearLogView();
};
Main.LeproWatch.clearLogView = () => {
console.log('[LeproWatch] Clearing log view...');
document.querySelector('#leprowatch_content .logs').innerHTML = '';
};
Main.LeproWatch.clearSearchInput = () => {
console.log('[LeproWatch] Clearing search input...');
document.querySelector('.searchBox').value = '';
};
Main.LeproWatch.renderLogs = (logs, search = false) => {
Main.LeproWatch.clearLogView();
if (!search) Main.LeproWatch.clearSearchInput();
console.log('[LeproWatch] Rendering logs...');
let lastCycle = -1;
let lastRoom = '';
const content = document.querySelector('#leprowatch_content .logs');
logs.sort((a, b) => {
if (a.cycle !== b.cycle) return a.cycle > b.cycle ? -1 : 1;
if (a.room !== b.room) return a.room > b.room ? -1 : 1;
if (a.time !== b.time) return a.time > b.time ? -1 : 1;
return a.index > b.index ? -1 : 1;
});
logs.forEach(log => {
const roomName = Main.LeproWatch.roomNames[log.room];
if (log.cycle !== lastCycle) {
const day = Math.floor((log.cycle) / 8) + 1;
const cycle = Math.ceil((log.cycle) % 8) + 1;
const row = $('<div>').addClass('day_cycle')
.append(`<strong> day ${day} Cycle ${cycle} (Cycle ${log.cycle})</strong>`);
row.appendTo(content);
lastCycle = log.cycle;
}
if (roomName !== lastRoom && search) {
const row = $('<div>').addClass('day_cycle mush').append(` ${roomName}`);
row.appendTo(content);
lastRoom = roomName;
}
// ----------------------------------------- Printing log item ----------------------------------------
const time = Main.LeproWatch.formatTime(log.time);
const timeAgo = Main.LeproWatch.setTimes(log.time);
const timestamp = `<span class="ago" title="${time} ±${log.accuracy}">${timeAgo}</span>`;
const row = $('<div>').addClass(log.classes).append(timestamp + log.html.trim());
row.appendTo(content);
});
};
Main.LeproWatch.selectTab = (el) => {
console.log('[LeproWatch] Selecting tab...');
let $cdMainChat = $("#cdMainChat");
if ($(el).attr('data-tab') !== undefined) {
$("#leprowatch").removeClass("tabon").addClass("taboff");
$("#leprowatch_content").hide();
$cdMainChat.find(".cdInputBloc").show();
return Main.selChat($(el).attr('data-tab'));
}
//Script tab
$("#cdTabsChat").find("li").removeClass("tabon").addClass("taboff");
$("#chatBlock > *").hide();
$cdMainChat.find(".cdInputBloc").hide();
$(el).removeClass("taboff").addClass("tabon");
const scriptTab = $($(el).attr('data-script-tab'));
if (scriptTab.length) {
$(scriptTab).show();
}
const functionTab = $($(el).attr('data-script-function'));
if (functionTab.length) {
functionTab();
}
};
Main.LeproWatch.addButton = (src, text, hasText, func, parent) => {
console.log('[LeproWatch] Adding button ' + text + '...');
const button = $('<a>').addClass('butmini').append((hasText ? text : ''));
button.on('click', func);
button.appendTo(parent);
$('<img>').attr({ src: src, title: text }).appendTo(button);
};
Main.LeproWatch.fill = () => {
console.log('[LeproWatch] Filling DOM...');
const mainDiv = $('<div>').css("color", "rgb(9, 10, 97)").appendTo($("#leprowatch_content").empty());
$('<div>').addClass('objtitle').html("<img src='http://twinoid.com/img/icons/archive.png'> LeproWatch archive! <img src='http://twinoid.com/img/icons/archive.png'>").appendTo(mainDiv);
const menu = $('<div>').addClass('replybuttons').css({ padding: '2px 7px 0' }).appendTo(mainDiv);
const roomSelector = $('<select>').addClass('roomChanger').appendTo(mainDiv);
const searchBox = $('<input type="text">').addClass('searchBox').appendTo(menu);
const logs = $('<div>').addClass('logs').appendTo(mainDiv);
roomSelector.css({
right: '7px',
position: 'relative',
background: '#0070dd linear-gradient(to bottom, #008ee5 0%,#008ee5 50%,#0070dd 51%,#0070dd 100%)',
border: '1px solid #000',
padding: 0,
fontSize: '10px',
float: 'right',
top: '2px',
height: '16px',
lineHeight: '16px',
width: '100px'
});
searchBox.attr({
placeholder: 'Search (all rooms)'
}).css({
width: '120px',
margin: '0 10px 0 0',
float: 'left',
border: '1px solid black',
color: 'black',
height: '12px',
fontSize: '12px',
lineHeight: '12px'
});
const current = Main.LeproWatch.getRoomId();
Main.LeproWatch.roomNames.forEach((i, index) => {
const roomName = i;
const room = index;
const selected = (index === current) ? 'selected' : '';
const option = $(`<option value="${room}" ${selected}>${roomName}</option>`);
option.appendTo(roomSelector);
});
Main.LeproWatch.addButton('http://twinoid.com/img/icons/refresh.png', 'Reload', true, Main.LeproWatch.loadLogs, menu);
Main.LeproWatch.addButton('/img/icons/ui/close.png', 'Reset', true, Main.LeproWatch.resetLogs, menu);
roomSelector.on('change', e => Main.LeproWatch.loadLogs(e.target.value));
searchBox.on('keyup', $.debounce(300, e => Main.LeproWatch.search(e.target.value)));
};
Main.LeproWatch.addArchiveTab = () => {
console.log('[LeproWatch] Activating tab...');
const tabsChat = $("#cdTabsChat");
const tab = $("<li>").addClass("tab taboff").css('margin-right', '3px').appendTo(tabsChat);
const content = $("<div>").addClass("cdLeptoWatchTab").attr("id", "leprowatch_content").appendTo($("#chatBlock"));
content.hide();
tab.attr({
id: "leprowatch",
'data-script-tab': '#leprowatch_content',
'data-script-function': 'Main.LeproWatch.loadLogs'
});
$("<img>").attr("src", "http://twinoid.com/img/icons/archive.png").appendTo(tab);
tab.on("mouseover", function () {
Main.showTip($(this)[0], `
<div class='tiptop'><div class='tipbottom'><div class='tipbg'><div class='tipcontent'>
<h1>LeproWatch</h1> <p> ${Main.LeproWatch.decs} </p>
</div></div></div></div>
`);
}).on("mouseout", function () {
Main.hideTip();
});
tabsChat.find("li").on("click", function () {
Main.LeproWatch.selectTab(this);
});
Main.LeproWatch.fill();
};
// Init functions
Main.LeproWatch.init = () => {
console.log('[LeproWatch] Init...');
Main.LeproWatch.currentRoom = Main.LeproWatch.getRoomId();
Main.LeproWatch.connectToDatabase();
Main.LeproWatch.addArchiveTab();
document.addEventListener('lw:connected', Main.LeproWatch.collectLogs);
setInterval(function () {
if (!document.querySelectorAll('#leprowatch').length) { //If the page has been updated
Main.LeproWatch.addArchiveTab();
Main.LeproWatch.collectLogs();
}
}, 1000);
};
if (!Main.LeproWatch.indexedDB) {
window.alert("LeproWatch plugin will not work on your browser! Please use modern one!");
} else {
Main.LeproWatch.init();
}
/* jshint ignore:start */
]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);
/* jshint ignore:end */