forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatMoreMagicLinks.user.js
98 lines (75 loc) · 3.72 KB
/
ChatMoreMagicLinks.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
// ==UserScript==
// @name Chat More Magic Links
// @description Some magic links are not parsed in Stack Overflow Chat. This script parses and submit expanded magic links via an edit to your latest message.
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author @samliew
// @version 1.4.1
//
// @include https://chat.stackoverflow.com/rooms/*
// ==/UserScript==
(function() {
'use strict';
const mainDomain = 'https://' + location.hostname.replace('chat.', '');
const _fkey = $('#fkey').val();
const _uid = CHAT.CURRENT_USER_ID;
function updateMessage(mid, message) {
// Fire and forget
$.post(`/messages/${mid}`, {
fkey: _fkey,
text: message
});
//console.log('updateMessage', mid, message);
}
function getMessageRawString(mid) {
return new Promise(function(resolve, reject) {
$.get(`/messages/${mid}/history`)
.done(function(data) {
const str = $('.monologue b', data).filter((i,el) => el.innerText == 'said:').next('.message-source').text().trim();
resolve(str);
})
.fail(reject);
});
}
function convertMagicLinksInMessage(msg) {
msg = msg.replace(/\[mcve\](?!\()/gi, `[Minimal, Complete, and Verifiable example](${mainDomain}/help/mcve)`);
msg = msg.replace(/\[help\](?!\()/gi, `[help center](${mainDomain}/help)`);
msg = msg.replace(/\[help\/on-topic\](?!\()/gi, `[help center](${mainDomain}/help/on-topic)`);
msg = msg.replace(/\[help\/dont-ask\](?!\()/gi, `[help center](${mainDomain}/help/dont-ask)`);
msg = msg.replace(/\[help\/behavior\](?!\()/gi, `[help center](${mainDomain}/help/behavior)`);
msg = msg.replace(/\[meta-help\](?!\()/gi, `[help center](${mainDomain}/help/whats-meta)`);
msg = msg.replace(/\[tour\](?!\()/gi, `[tour](${mainDomain}/tour)`);
msg = msg.replace(/\[co(nduct|c)\](?!\()/gi, `[Code of Conduct](${mainDomain}/conduct)`);
msg = msg.replace(/\[chat\](?!\()/gi, `[Stack Overflow Chat](https://${location.hostname})`);
msg = msg.replace(/\[socvr\](?!\()/gi, `[SOCVR](https://chat.stackoverflow.com/rooms/41570/so-close-vote-reviewers)`);
msg = msg.replace(/\[somu\](?!\()/gi, `[Stack Overflow Moderation Userscripts *by Samuel Liew*](https://github.com/samliew/SO-mod-userscripts/blob/master/README.md)`);
return msg;
}
function checkLastMessage() {
const lastMessage = $('.user-container.mine .message').last();
// If last message has already been handled, ignore
if(lastMessage.hasClass('js-magiclinks')) return;
lastMessage.addClass('js-magiclinks');
// If last message has already been deleted, ignore
if(lastMessage.hasClass('cmmt-deleted')) return;
// If last message has already been edited, ignore
if(lastMessage.find('.edits').length > 0) return;
// Required
if(typeof lastMessage.attr('id') === 'undefined') return;
const mid = Number(lastMessage.attr('id').replace(/[^\d]/g, ''));
if(isNaN(mid)) return;
getMessageRawString(mid)
.then(function(rawMessage) {
const parsedMessage = convertMagicLinksInMessage(rawMessage);
//console.log(mid, rawMessage, parsedMessage);
if(rawMessage.length !== parsedMessage.length && rawMessage !== parsedMessage) {
updateMessage(mid, parsedMessage);
}
});
}
function doPageload() {
// Occasionally, check last message and parse it
setInterval(checkLastMessage, 2000);
}
// On page load
doPageload();
})();