Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kms70847
Last active April 21, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/questions/.*/
// @version 8
// @grant none
// ==/UserScript==
function make_input(text){
var input = document.createElement("input");
input.type = "text";
input.value = text;
input.style.display = "block";
input.style.width = "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
if(box.querySelector(".gm-share-box")){
return;
}
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
input.parentElement.appendChild(make_input(text));
}
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){
callback(result);
}
else{
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);
}
try{
for (var menu of document.querySelectorAll(".js-share-link")){
whenHasSibling(menu, ".s-popover", enhance_box);
console.log(menu);
}
}
catch(err){
console.log(err.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment