Created
October 28, 2015 15:45
-
-
Save shu8/991e94da2cec412be603 to your computer and use it in GitHub Desktop.
A userscript that lets you make custom magic links to use in the comments on SE sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Custom Magic Links | |
// @namespace http://stackexchange.com/users/4337810/ | |
// @version 1.0 | |
// @description A userscript that lets you make custom magic links to use in the comments on SE sites | |
// @author ᔕᖺᘎᕊ (http://stackexchange.com/users/4337810/) | |
// @match *://*.stackexchange.com/* | |
// @match *://*.stackoverflow.com/* | |
// @match *://*.superuser.com/* | |
// @match *://*.serverfault.com/* | |
// @match *://*.askubuntu.com/* | |
// @match *://*.stackapps.com/* | |
// @match *://*.mathoverflow.net/* | |
// @grant none | |
// ==/UserScript== | |
var magicLinks = {}; | |
var magicLinksCustomText = {}; | |
magicLinks['edit/q Edit Question'] = '$BASEURL$/posts/$QUESTIONID$/edit'; | |
magicLinks['edit/a Edit Answer'] = '$BASEURL$/posts/$ANSWERID$/edit'; | |
magicLinksCustomText['test'] = "This is a test"; | |
magicLinksCustomText['test2'] = "This is a test2"; | |
//-----------------------------------------------------------------------------------------------------------------// | |
function magicLink($el) { | |
var $textarea = ($el.is('textarea') ? $el : $el.parent().parent().find('textarea')); | |
var newVal = $textarea.val(); | |
$.each(magicLinks, function (k, v) { | |
var linkText = k.split(' ').slice(1).join(' '); | |
var regex = new RegExp("\\[" + k.split(' ')[0] + "\\]", 'g'); | |
var url = replacePlaceholders(v, $textarea); | |
newVal = newVal.replace(regex, "[" + linkText + "](" + url + ")"); | |
}); | |
$.each(magicLinksCustomText, function (k, v) { | |
var regex = new RegExp("(\\[.*?\\])\\[" + k + "\\]", "g") | |
var url = replacePlaceholders(v, $textarea); | |
newVal = newVal.replace(regex, "$1(" + url + ")"); //replacing newVal this time because the latest version is newVal. | |
}); | |
$textarea.val(newVal); | |
} | |
function replacePlaceholders(s, $textarea) { | |
var baseUrl = $(location).attr('protocol') + '//' + $(location).attr('hostname'); | |
var localMetaBase = $(location).attr('protocol') + '//meta.' + $(location).attr('hostname'); | |
var questionId = $('.question').data('questionid'); | |
var answerId = $textarea.closest('.answer').data('answerid'); | |
return s.replace(/\$BASEURL\$/ig, baseUrl).replace(/\$METABASEURL\$/ig, localMetaBase).replace(/\$QUESTIONID\$/ig, questionId).replace(/\$ANSWERID\$/ig, answerId); | |
} | |
function checkAndCall(e, $el) { //mini function to call magicLink after pressing enter (avoid repetition) | |
if (e.keyCode == 13) { | |
magicLink($el); | |
} | |
} | |
//Handlers: | |
$('.comment-form').on('keydown', 'textarea', function (e) { | |
checkAndCall(e, $(this)); | |
}); | |
$('.comment-form').on('click', 'input[type="submit"]', function () { | |
magicLink($(this)); | |
}); | |
//sets handler again after clicking edit - there's probably a better way to do it though! | |
$('.comments').on('click', '.comment-edit', function () { | |
setTimeout(function () { | |
$('.comment-form').on('keydown', 'textarea', function (e) { | |
checkAndCall(e, $(this)); | |
}); | |
}, 500); | |
}); | |
$('.comments').on('click', '.comment-edit', function () { | |
setTimeout(function () { | |
$('.comment-form').on('click', 'input[type="submit"]', function () { | |
magicLink($(this)); | |
}); | |
}, 500); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment