Skip to content

Instantly share code, notes, and snippets.

Last active April 21, 2021 16:00
Show Gist options
  • Save kms70847/3b751f24494cb6ed219b to your computer and use it in GitHub Desktop.
Save kms70847/3b751f24494cb6ed219b to your computer and use it in GitHub Desktop.
For each Stack Overflow question, adds an additional box to the "share" button, containing a link with markdown
// ==UserScript==
// @name Share Markdown (Stack Overflow)
// @namespace about:blank
// @include /^https?:\/\/\w*.?(||||||\/questions/.*/
// @version 8
// @grant none
// ==/UserScript==
function make_input(text){
var input = document.createElement("input");
input.type = "text";
input.value = text; = "block"; = "292px";
input.className = "gm-share-box";
return input;
//add functionality to the "share permalink" box.
function enhance_box(box){
//don't add the new element if it already exists
var input = box.getElementsByTagName("input")[0];
var url = input.value;
var title = document.getElementsByClassName("question-hyperlink")[0].childNodes[0].nodeValue;
title = title.replace("[", "\\[").replace("]", "\\]");
var text = "[" + title + "](" + url + ")";
//stick it after the first box
function whenExists(func, callback, delay){
//calls `func` every `delay` milliseconds, until it returns a non-null value.
//when it does, calls `callback(result)`.
var result = func()
if (result != null){
window.setTimeout(whenExists, delay, func, callback, delay);
function whenHasSibling(element, selector, callback, delay){
//like whenExists, but for checking whether an element has a sibling matching a selector
function getChild(){
return element.parentElement.querySelector(selector);
whenExists(getChild, callback, delay);
for (var menu of document.querySelectorAll(".js-share-link")){
whenHasSibling(menu, ".s-popover", enhance_box);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment