-
Notifications
You must be signed in to change notification settings - Fork 34
/
ShowDeletedMessagesInChat.user.js
101 lines (80 loc) · 3.37 KB
/
ShowDeletedMessagesInChat.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
// ==UserScript==
// @name Show Deleted Messages in Chat
// @description Show Deleted Messages in Chat and Transcripts. Works with NoOneboxesInChat userscript
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 3.1.13
//
// @match https://chat.stackoverflow.com/rooms/*
// @match https://chat.stackexchange.com/rooms/*
// @match https://chat.meta.stackexchange.com/rooms/*
//
// @match https://chat.stackoverflow.com/transcript/*
// @match https://chat.stackexchange.com/transcript/*
// @match https://chat.meta.stackexchange.com/transcript/*
//
// @match https://chat.stackoverflow.com/rooms/*/conversation/*
// @match https://chat.stackexchange.com/rooms/*/conversation/*
// @match https://chat.meta.stackexchange.com/rooms/*/conversation/*
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
// ==/UserScript==
/* globals StackExchange */
/// <reference types="./globals" />
'use strict';
function getDeletedMessagesHistory(mid) {
const msgDiv = $(`#message-${mid}`);
const contentDiv = msgDiv.find('.content');
// Get message's history
$.get(`/messages/${mid}/history`, function (data) {
// Get message and deleted-by from history
const origMsg = $(`#message-${mid}`, data).first().find('.content').html();
const deletedBy = $('b:contains("deleted")', data).closest('.monologue').find('.username').attr('target', '_blank').html();
// Insert into message
contentDiv.append(origMsg);
contentDiv.find('.deleted').first().html(`(deleted by ${deletedBy})`);
// Add class 'cmmt-deleted' for styling purposes (background/text color)
msgDiv.addClass('cmmt-deleted');
// Hide oneboxes if userscript is installed
if (typeof hideOneboxes === 'function') { hideOneboxes(); }
// Bugfix for favicon resetting to default in Firefox on network request
// modified from https://stackoverflow.com/a/4630726
$('head link[rel="shortcut icon"]').detach().attr('href', (i, v) => v + '#' + Math.floor(Math.random() * 100000) + 1).appendTo('head');
});
}
function processNewDeletedMessages() {
// Use class 'js-history-loaded' to track which ones have been processed
$('.deleted').not('.js-history-loaded').addClass('js-history-loaded')
.parents('.message')
// Hand-off message ID to function
.each((i, el) => getDeletedMessagesHistory(el.id.replace('message-', '')));
}
// Append styles
addStylesheet(`
.message.cmmt-deleted {
background: #f5e6e6;
color: #990000;
}
.message.cmmt-deleted span.deleted {
float: right;
padding-left: 10px;
font-style: italic;
}
.message.cmmt-deleted span.deleted a {
color: #999;
}
`); // end stylesheet
// On script run
(function init() {
// Mobile chat transcript does not have this
if (typeof CHAT.RoomUsers.current === 'undefined') return;
var self = CHAT.RoomUsers.current();
var canSeeDeleted = self.is_moderator || self.is_owner;
if (canSeeDeleted || location.pathname.includes('/transcript') || location.pathname.includes('/conversation')) {
// Once on page load
processNewDeletedMessages();
// Occasionally, look for new deleted messages and load them
setInterval(processNewDeletedMessages, 5000);
}
})();