Skip to content

Instantly share code, notes, and snippets.

@shu8
Last active March 25, 2020 19:09
Show Gist options
  • Save shu8/6c7979a4e5861c2e234301cbfa504628 to your computer and use it in GitHub Desktop.
Save shu8/6c7979a4e5861c2e234301cbfa504628 to your computer and use it in GitHub Desktop.
Enhances magic links in SE comments to make them more useful
// ==UserScript==
// @name Magic Links Enhancer
// @namespace stackexchange.com/users/4337810/
// @version 1.0.1
// @description Enhances magic links in SE comments to make them more useful
// @author ᔕᖺᘎᕊ (stackexchange.com/users/4337810/)
// @match *://*.stackoverflow.com/questions/*
// @match *://*.stackexchange.com/questions/*
// @match *://*.superuser.com/questions/*
// @match *://*.serverfault.com/questions/*
// @match *://*.askubuntu.com/questions/*
// @match *://*.stackapps.com/questions/*
// @match *://*.mathoverflow.net/questions/*
// @grant none
// ==/UserScript==
var urls = {
"so": ["stackoverflow.com", "Stack Overflow"],
"su": ["superuser.com", "Super User"],
"sf": ["serverfault.com", "Server Fault"],
"stackoverflow": ["stackoverflow.com", "Stack Overflow"],
"superuser": ["superuser.com", "Super User"],
"serverfault": ["serverfault.com", "Server Fault"],
"stackapps": ["stackapps.com", "Stack Apps"],
"meta": ["meta.stackexchange.com", "Meta Stack Exchange"],
"webapps": ["webapps.stackexchange.com", "Web Applications Stack Exchange"],
"gaming": ["gaming.stackexchange.com", "Arqade Stack Exchange"],
"webmasters": ["webmasters.stackexchange.com", "Webmasters Stack Exchange"],
"cooking": ["cooking.stackexchange.com", "Seasoned Advice Stack Exchange"],
"gamedev": ["gamedev.stackexchange.com", "Game Development Stack Exchange"],
"photo": ["photo.stackexchange.com", "Photography Stack Exchange"],
"stats": ["stats.stackexchange.com", "Cross Validated Stack Exchange"],
"math": ["math.stackexchange.com", "Mathematics Stack Exchange"],
"diy": ["diy.stackexchange.com", "Home Improvement Stack Exchange"],
"gis": ["gis.stackexchange.com", "Geographic Information Systems Stack Exchange"],
"tex": ["tex.stackexchange.com", "TeX - LaTeX Stack Exchange"],
"askubuntu": ["askubuntu.com", "Ask Ubuntu Stack Exchange"],
"money": ["money.stackexchange.com", "Personal Finance & Money Stack Exchange"],
"english": ["english.stackexchange.com", "English Language & Usage Stack Exchange"],
"ux": ["ux.stackexchange.com", "User Experienc Stack Exchangee"],
"unix": ["unix.stackexchange.com", "Unix & Linux Stack Exchange"],
"wordpress": ["wordpress.stackexchange.com", "WordPress Development Stack Exchange"],
"cstheory": ["cstheory.stackexchange.com", "Theoretical Computer Science Stack Exchange"],
"apple": ["apple.stackexchange.com", "Ask Different Stack Exchange"],
"rpg": ["rpg.stackexchange.com", "Role-playing Games Stack Exchange"],
"bicycles": ["bicycles.stackexchange.com", "Bicycles Stack Exchange"],
"programmers": ["programmers.stackexchange.com", "Programmers Stack Exchange"],
"electronics": ["electronics.stackexchange.com", "Electrical Engineering Stack Exchange"],
"android": ["android.stackexchange.com", "Android Enthusiasts Stack Exchange"],
"boardgames": ["boardgames.stackexchange.com", "Board & Card Games Stack Exchange"],
"physics": ["physics.stackexchange.com", "Physics Stack Exchange"],
"homebrew": ["homebrew.stackexchange.com", "Homebrewing Stack Exchange"],
"security": ["security.stackexchange.com", "Information Security Stack Exchange"],
"writers": ["writers.stackexchange.com", "Writers Stack Exchange"],
"video": ["video.stackexchange.com", "Video Production Stack Exchange"],
"graphicdesign": ["graphicdesign.stackexchange.com", "Graphic Design Stack Exchange"],
"dba": ["dba.stackexchange.com", "Database Administrators Stack Exchange"],
"scifi": ["scifi.stackexchange.com", "Science Fiction & Fantasy Stack Exchange"],
"codereview": ["codereview.stackexchange.com", "Code Review Stack Exchange"],
"codegolf": ["codegolf.stackexchange.com", "Programming Puzzles & Code Golf Stack Exchange"],
"quant": ["quant.stackexchange.com", "Quantitative Finance Stack Exchange"],
"pm": ["pm.stackexchange.com", "Project Management Stack Exchange"],
"skeptics": ["skeptics.stackexchange.com", "Skeptics Stack Exchange"],
"fitness": ["fitness.stackexchange.com", "Physical Fitness Stack Exchange"],
"drupal": ["drupal.stackexchange.com", "Drupal Answers Stack Exchange"],
"mechanics": ["mechanics.stackexchange.com", "Motor Vehicle Maintenance & Repair Stack Exchange"],
"parenting": ["parenting.stackexchange.com", "Parenting Stack Exchange"],
"sharepoint": ["sharepoint.stackexchange.com", "SharePoint Stack Exchange"],
"music": ["music.stackexchange.com", "Music: Practice & Theory Stack Exchange"],
"sqa": ["sqa.stackexchange.com", "Software Quality Assurance & Testing Stack Exchange"],
"judaism": ["judaism.stackexchange.com", "Mi Yodeya Stack Exchange"],
"german": ["german.stackexchange.com", "German Language Stack Exchange"],
"japanese": ["japanese.stackexchange.com", "Japanese Language Stack Exchange"],
"philosophy": ["philosophy.stackexchange.com", "Philosophy Stack Exchange"],
"gardening": ["gardening.stackexchange.com", "Gardening & Landscaping Stack Exchange"],
"travel": ["travel.stackexchange.com", "Travel Stack Exchange"],
"productivity": ["productivity.stackexchange.com", "Personal Productivity Stack Exchange"],
"crypto": ["crypto.stackexchange.com", "Cryptography Stack Exchange"],
"dsp": ["dsp.stackexchange.com", "Signal Processing Stack Exchange"],
"french": ["french.stackexchange.com", "French Language Stack Exchange"],
"christianity": ["christianity.stackexchange.com", "Christianity Stack Exchange"],
"bitcoin": ["bitcoin.stackexchange.com", "Bitcoin Stack Exchange"],
"linguistics": ["linguistics.stackexchange.com", "Linguistics Stack Exchange"],
"hermeneutics": ["hermeneutics.stackexchange.com", "Biblical Hermeneutics Stack Exchange"],
"history": ["history.stackexchange.com", "History Stack Exchange"],
"bricks": ["bricks.stackexchange.com", "LEGO® Answers Stack Exchange"],
"spanish": ["spanish.stackexchange.com", "Spanish Language Stack Exchange"],
"scicomp": ["scicomp.stackexchange.com", "Computational Scienc Stack Exchangee"],
"movies": ["movies.stackexchange.com", "Movies & TV Stack Exchange"],
"chinese": ["chinese.stackexchange.com", "Chinese Language Stack Exchange"],
"biology": ["biology.stackexchange.com", "Biology Stack Exchange"],
"poker": ["poker.stackexchange.com", "Poker Stack Exchange"],
"mathematica": ["mathematica.stackexchange.com", "Mathematica Stack Exchange"],
"cogsci": ["cogsci.stackexchange.com", "Cognitive Sciences Stack Exchange"],
"outdoors": ["outdoors.stackexchange.com", "The Great Outdoors Stack Exchange"],
"martialarts": ["martialarts.stackexchange.com", "Martial Arts Stack Exchange"],
"sports": ["sports.stackexchange.com", "Sports Stack Exchange"],
"academia": ["academia.stackexchange.com", "Academia Stack Exchange"],
"cs": ["cs.stackexchange.com", "Computer Science Stack Exchange"],
"workplace": ["workplace.stackexchange.com", "The Workplace Stack Exchange"],
"windowsphone": ["windowsphone.stackexchange.com", "Windows Phone Stack Exchange"],
"chemistry": ["chemistry.stackexchange.com", "Chemistry Stack Exchange"],
"chess": ["chess.stackexchange.com", "Chess Stack Exchange"],
"raspberrypi": ["raspberrypi.stackexchange.com", "Raspberry Pi Stack Exchange"],
"russian": ["russian.stackexchange.com", "Russian Language Stack Exchange"],
"islam": ["islam.stackexchange.com", "Islam Stack Exchange"],
"salesforce": ["salesforce.stackexchange.com", "Salesforce Stack Exchange"],
"patents": ["patents.stackexchange.com", "Ask Patents Stack Exchange"],
"genealogy": ["genealogy.stackexchange.com", "Genealogy & Family History Stack Exchange"],
"robotics": ["robotics.stackexchange.com", "Robotics Stack Exchange"],
"expressionengine": ["expressionengine.stackexchange.com", "ExpressionEngine® Answers Stack Exchange"],
"politics": ["politics.stackexchange.com", "Politics Stack Exchange"],
"anime": ["anime.stackexchange.com", "Anime & Manga Stack Exchange"],
"magento": ["magento.stackexchange.com", "Magento Stack Exchange"],
"ell": ["ell.stackexchange.com", "English Language Learners Stack Exchange"],
"sustainability": ["sustainability.stackexchange.com", "Sustainable Living Stack Exchange"],
"tridion": ["tridion.stackexchange.com", "Tridion Stack Exchange"],
"reverseengineering": ["reverseengineering.stackexchange.com", "Reverse Engineering Stack Exchange"],
"networkengineering": ["networkengineering.stackexchange.com", "Network Engineering Stack Exchange"],
"opendata": ["opendata.stackexchange.com", "Open Data Stack Exchange"],
"freelancing": ["freelancing.stackexchange.com", "Freelancing Stack Exchange"],
"blender": ["blender.stackexchange.com", "Blender Stack Exchange"],
"mathoverflow.net": ["mathoverflow.net", "MathOverflow"],
"space": ["space.stackexchange.com", "Space Exploration Stack Exchange"],
"sound": ["sound.stackexchange.com", "Sound Design Stack Exchange"],
"astronomy": ["astronomy.stackexchange.com", "Astronomy Stack Exchange"],
"tor": ["tor.stackexchange.com", "Tor Stack Exchange"],
"pets": ["pets.stackexchange.com", "Pets Stack Exchange"],
"ham": ["ham.stackexchange.com", "Amateur Radio Stack Exchange"],
"italian": ["italian.stackexchange.com", "Italian Language Stack Exchange"],
"pt.stackoverflow": ["pt.stackoverflow.com", "Stack Overflow em Português"],
"aviation": ["aviation.stackexchange.com", "Aviation Stack Exchange"],
"ebooks": ["ebooks.stackexchange.com", "Ebooks Stack Exchange"],
"beer": ["beer.stackexchange.com", "Beer Stack Exchange"],
"softwarerecs": ["softwarerecs.stackexchange.com", "Software Recommendations Stack Exchange"],
"arduino": ["arduino.stackexchange.com", "Arduino Stack Exchange"],
"cs50": ["cs50.stackexchange.com", "CS50 Stack Exchange"],
"expatriates": ["expatriates.stackexchange.com", "Expatriates Stack Exchange"],
"matheducators": ["matheducators.stackexchange.com", "Mathematics Educators Stack Exchange"],
"earthscience": ["earthscience.stackexchange.com", "Earth Science Stack Exchange"],
"joomla": ["joomla.stackexchange.com", "Joomla Stack Exchange"],
"datascience": ["datascience.stackexchange.com", "Data Science Stack Exchange"],
"puzzling": ["puzzling.stackexchange.com", "Puzzling Stack Exchange"],
"craftcms": ["craftcms.stackexchange.com", "Craft CMS Stack Exchange"],
"buddhism": ["buddhism.stackexchange.com", "Buddhism Stack Exchange"],
"hinduism": ["hinduism.stackexchange.com", "Hinduism Stack Exchange"],
"communitybuilding": ["communitybuilding.stackexchange.com", "Community Building Stack Exchange"],
"startups": ["startups.stackexchange.com", "Startups Stack Exchange"],
"worldbuilding": ["worldbuilding.stackexchange.com", "Worldbuilding Stack Exchange"],
"ja.stackoverflow": ["ja.stackoverflow.com", "スタック・オーバーフロー"],
"emacs": ["emacs.stackexchange.com", "Emacs Stack Exchange"],
"hsm": ["hsm.stackexchange.com", "History of Science and Mathematics Stack Exchange"],
"economics": ["economics.stackexchange.com", "Economics Stack Exchange"],
"lifehacks": ["lifehacks.stackexchange.com", "Lifehacks Stack Exchange"],
"engineering": ["engineering.stackexchange.com", "Engineering Stack Exchange"],
"coffee": ["coffee.stackexchange.com", "Coffee Stack Exchange"],
"vi": ["vi.stackexchange.com", "Vi and Vim Stack Exchange"],
"musicfans": ["musicfans.stackexchange.com", "Music Fans Stack Exchange"],
"woodworking": ["woodworking.stackexchange.com", "Woodworking Stack Exchange"],
"civicrm": ["civicrm.stackexchange.com", "CiviCRM Stack Exchange"],
"health": ["health.stackexchange.com", "Health Stack Exchange"],
"ru.stackoverflow": ["ru.stackoverflow.com", "Stack Overflow на русском"],
"rus": ["rus.stackexchange.com", "Русский язык Stack Exchange"],
"mythology": ["mythology.stackexchange.com", "Mythology Stack Exchange"],
"law": ["law.stackexchange.com", "Law Stack Exchange"],
"opensource": ["opensource.stackexchange.com", "Open Source Stack Exchange"],
"elementaryos": ["elementaryos.stackexchange.com", "elementary OS Stack Exchange"],
"portuguese": ["portuguese.stackexchange.com", "Portuguese Language Stack Exchange"],
"computergraphics": ["computergraphics.stackexchange.com", "Computer Graphics Stack Exchange"],
"hardwarerecs": ["hardwarerecs.stackexchange.com", "Hardware Recommendations Stack Exchange"],
"es.stackoverflow": ["es.stackoverflow.com", "Stack Overflow en español Stack Exchange"],
"docs-beta": ["docs-beta.stackexchange.com", "Documentation Beta Stack Exchange"],
"3dprinting": ["3dprinting.stackexchange.com", "3D Printing Stack Exchange"],
"ethereum": ["ethereum.stackexchange.com", "Ethereum Stack Exchange"],
"latin": ["latin.stackexchange.com", "Latin Language Stack Exchange"],
"languagelearning": ["languagelearning.stackexchange.com", "Language Learning Stack Exchange"]
};
var privs = {
"site-analytics": "access to site analytics",
"trusted-user": "trusted user",
"protect-questions": "protect questions",
"moderator-tools": "access to moderator tools",
"approve-tag-wiki-edits": "approve tag wiki edits",
"close-questions": "cast close and reopen votes",
"suggest-tag-synonyms": "create tag synonyms",
"edit": "edit questions and answers",
"access-review-queues": "access review queues",
"established-user": "established user",
"gallery-chat-rooms": "create gallery chat rooms",
"create-tags": "create tags",
"view-close-votes": "view close votes",
"reduced-ads": "reduce ads",
"vote-down": "vote down",
"edit-community-wiki": "edit community wiki",
"chat-rooms": "create chat rooms",
"set-bounties": "set bounties",
"chat": "talk in chat",
"flag-posts": "flag posts",
"vote-up": "vote up",
"new-user": "remove new user restrictions",
"community-wiki": "create wiki posts",
"comment": "comment everywhere",
"participate-in-meta": "participate in meta",
"create-posts": "create posts"
};
function siteUrl(site) {
if (site.indexOf('meta.') > -1 && site.indexOf('meta.stackexchange.com') == -1) {
return [
'//meta.' + urls[site.split('.')[1]][0],
'Meta ' + urls[site.split('.')[1]][1]
];
} else {
return [
'//' + urls[site][0],
urls[site][1]
];
}
}
function markdownify(text, url) {
return '[' + text + '](' + url + ')';
}
function api(url) {
var x;
$.ajax({
url: url,
type: 'get',
async: false,
success: function(d) {
x = d;
}
});
return x;
}
function convertMagicLinks(val) {
var split = val.replace(/\[/g, '').replace(/\]/g, '').split(':');
if (split.length == 3 || split.length == 2) {
var site = (split.length == 3 ? siteUrl(split[0])[0] : location.protocol + '//' + location.hostname),
type = (split.length == 3 ? split[1] : split[0]),
anchorId = (split.length == 3 ? split[2] : split[1]),
apiSite = site.replace(/(h?t?t?p?s?:?\/?\/?)/, '');
if (type == 'user') {
x = api("http://api.stackexchange.com/2.2/users/" + anchorId + "?site=" + apiSite);
return markdownify(x.items[0].display_name, x.items[0].link);
} else if (type == 'post') {
x = api("http://api.stackexchange.com/2.2/posts/" + anchorId + "?site=" + apiSite + "&filter=!9YdnSEBb8");
return markdownify(x.items[0].title, x.items[0].link);
} else if (type == 'help') {
return markdownify('Help Center', site + '/help/' + anchorId);
} else if (type == 'priv') {
return markdownify(privs[anchorId], site + '/help/privileges/' + anchorId);
} else {
return val;
}
} else if (split.length == 1) {
var anchorId = split[0];
if ($.isNumeric(anchorId)) {
x = api("http://api.stackexchange.com/2.2/posts/" + anchorId + "?site=" + location.hostname + "&filter=!9YdnSEBb8");
return markdownify(x.items[0].title, x.items[0].link);
} else {
return markdownify(siteUrl(anchorId)[1], siteUrl(anchorId)[0]);
}
}
}
function replaceMagicLinks($el) {
var val = $el.val();
val = val.replace(/(\[.*?\])/g, function(a, b, c) {
console.log(convertMagicLinks(b));
return convertMagicLinks(b);
});
$el.val(val);
}
//Handlers:
$(document).on('keydown', '.comment-form textarea', function (e) {
if (e.keyCode == 13) {
replaceMagicLinks($(this));
}
});
$(document).on('click', '.comment-form input[type="submit"]', function () {
replaceMagicLinks($(this).parent().prev().find('textarea'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment