Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Userscript that enables the [mcve] shortcut in the StackOverflow chat
// ==UserScript==
// @name SOchat [mcve] shortcut
// @description Enables the [mcve] shortcut in StackOverflow chat
// @version 1.1.3
// @author Paul Pinterits
// @include *://chat.stackoverflow.com/rooms/*
// @grant none
// @updateURL https://gist.github.com/Aran-Fey/e5a3fbd5de70d093e7b490ad46117819/raw/SOchat_mcve_shortcut.user.js
// @downloadURL https://gist.github.com/Aran-Fey/e5a3fbd5de70d093e7b490ad46117819/raw/SOchat_mcve_shortcut.user.js
// ==/UserScript==
(function(){
'use strict';
const REPLACEMENTS = {
mcve: 'Minimal, Complete, and Verifiable Example',
mvce: 'Minimal, Complete, and Verifiable Example',
reprex: 'Minimal, Reproducible Example',
repro: 'Minimal, Reproducible Example',
ruprecht: 'Minimal, Reproducible Example', // https://chat.stackoverflow.com/transcript/6?m=46410725#46410725
mre: 'Minimal, Reproducible Example',
mwe: 'Minimal, Reproducible Example',
example: 'Minimal, Reproducible Example'
};
Object.keys(REPLACEMENTS).forEach(function(shorthand){
const title = REPLACEMENTS[shorthand];
const link = '[' + title + '](https://stackoverflow.com/help/mcve)';
REPLACEMENTS[shorthand] = link;
});
function fix_message(textbox){
var text = textbox.value;
Object.keys(REPLACEMENTS).forEach(function(shorthand){
const link = REPLACEMENTS[shorthand];
shorthand = '[' + shorthand + ']';
text = text.replace(shorthand, link);
});
textbox.value = text;
}
function run(){
const textbox = document.getElementById('input');
const send_button = document.getElementById('sayit-button');
function button_maybe_clicked(event){
if (event.target == send_button)
fix_message(textbox);
}
function on_key_press(event){
const key = event.keyCode ? event.keyCode : event.which;
if (key == 13 && !event.shiftKey) // Enter and not Shift
fix_message(textbox);
}
send_button.parentElement.addEventListener('click', button_maybe_clicked, true);
window.addEventListener('keydown', on_key_press, true);
}
run();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment