Skip to content

Instantly share code, notes, and snippets.

@sputnick-dev
Last active March 5, 2018 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sputnick-dev/c6cbcc061d8257cbbd67 to your computer and use it in GitHub Desktop.
Save sputnick-dev/c6cbcc061d8257cbbd67 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name SO_like_keyboards_shortcuts
// @namespace sputnick
// @include https://github.com/*
// @include https://gist.github.com/*
// @include https://*.slack.com/*
// @include https://stackoverflow.com/*
// @include hhttps://serverfault.com/*
// @include https://*stackexchange.com/*
// @include https://askubuntu.com/*
// @include https://superuser.com/*
// @version 1
// @grant none
// ==/UserScript==
// http://meta.stackexchange.com/a/272565/203970
// TODO need to work with gist
var id, name;
if (document.location.href.search(/gist\.github\.com/) > 0 ) { //TODO to be fixed
id = '#blob_contents';
name = 'gist';
}
else if (document.location.href.search(/github\.com.*new$/) > 0 ) {
id = '#issue_body';
name = 'github';
}
else if (document.location.href.search(/github\.com\/.*\/issues\/[0-9]+/) > 0 ) {
id = '#new_comment_field';
name = 'github';
}
else if (document.location.href.search(/slack\.com/) > 0 ) {
id = '#message-input';
name = 'slack';
}
else if (document.location.href.search(/(stackoverflow|stackexchange|superuser|askubuntu|serverfault)/) > 0 ) {
id = '[id^=wmd-input]';
name = 'stack';
}
(function() {
document.addEventListener('keydown', function(e){
//window.alert(e.which);
if (document.activeElement.tagName == "TEXTAREA") {
// ctrl+0 [tag:]
if (e.ctrlKey && e.which == 96) {
override(e);
wrapText(id, '[tag:', ']');
}
// ctrl+b: bold
if (e.ctrlKey && e.which == 66) {
override(e);
if (name == 'slack') { wrapText(id, '*', '*'); }
if (name == 'github' || name == 'gist') { wrapText(id, '**', '**'); }
}
// ctrl+i: emphasis
if (e.ctrlKey && e.which == 73) {
override(e);
wrapText(id, '_', '_');
}
// ctrl+k: code
if (e.ctrlKey && e.which == 75) {
override(e);
wrapText(id, '`', '`');
}
// ctrl+l: link
if (name !== 'stack' && e.ctrlKey && e.which == 76) {
override(e);
var p = window.prompt("Link paste bellow");
wrapText(id, '[', ']('+p+')');
}
}
}, true);
})();
function wrapText(elementID, openTag, closeTag) {
var textArea = document.querySelector(elementID);
var len = textArea.value.length;
var start = textArea.selectionStart;
var end = textArea.selectionEnd;
var selectedText = textArea.value.substring(start, end);
if (openTag == '`' && selectedText.search(/\n/) > 0 ) {
openTag = '```\n';
closeTag = '\n```';
}
var replacement = openTag + selectedText + closeTag;
textArea.value = textArea.value.substring(0, start) + replacement + textArea.value.substring(end, len);
}
function override(e) {
e.stopImmediatePropagation();
e.cancelBubble = true;
e.preventDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment