-
Notifications
You must be signed in to change notification settings - Fork 34
/
ChatMoreMagicLinks.user.js
93 lines (74 loc) · 3.38 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
// ==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 Samuel Liew
// @version 3.0.13
//
// @match https://chat.stackoverflow.com/rooms/*
//
// @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==
'use strict';
/* globals fkey */
/// <reference types="./globals" />
const soDomain = 'https://stackoverflow.com';
function updateMessage(mid, message) {
// Fire and forget
$.post(`/messages/${mid}`, {
fkey: fkey,
text: 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|mre)\](?!\()/gi, `[Minimal, Reproducible Example](${soDomain}/help/mcve)`);
msg = msg.replace(/\[help\](?!\()/gi, `[help center](${soDomain}/help)`);
msg = msg.replace(/\[help\/on-topic\](?!\()/gi, `[help center](${soDomain}/help/on-topic)`);
msg = msg.replace(/\[help\/dont-ask\](?!\()/gi, `[help center](${soDomain}/help/dont-ask)`);
msg = msg.replace(/\[help\/behavior\](?!\()/gi, `[help center](${soDomain}/help/behavior)`);
msg = msg.replace(/\[meta-help\](?!\()/gi, `[help center](${soDomain}/help/whats-meta)`);
msg = msg.replace(/\[tour\](?!\()/gi, `[tour](${soDomain}/tour)`);
msg = msg.replace(/\[co(nduct|c)\](?!\()/gi, `[Code of Conduct](${soDomain}/conduct)`);
msg = msg.replace(/\[chat\](?!\()/gi, `[Stack Overflow Chat](${location.origin})`);
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);
}
});
}
// On script run
(function init() {
// Occasionally, check last message and parse it
setInterval(checkLastMessage, 2000);
})();