Skip to content

Instantly share code, notes, and snippets.

@wimglenn
Forked from kms70847/share_markdown.user.js
Created February 19, 2020 16:08
Show Gist options
  • Save wimglenn/61124f9610a5c66318a3265b99139765 to your computer and use it in GitHub Desktop.
Save wimglenn/61124f9610a5c66318a3265b99139765 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 http://stackoverflow.com/questions/*
// @include https://stackoverflow.com/questions/*
// @version 6
// @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 url = box.getElementsByTagName("input")[0].value;
var title = document.getElementsByClassName("question-hyperlink")[0].childNodes[0].nodeValue;
title = title.replace("[", "\\[").replace("]", "\\]");
var text = "[" + title + "](" + url + ")";
//stick it on the end
box.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 whenHasChild(element, selector, callback, delay){
//like whenExists, but for checking whether an element has a child matching a selector
function getChild(){
return element.querySelector(selector);
}
whenExists(getChild, callback, delay);
}
try{
for(var menu of document.querySelectorAll(".post-menu")){
whenHasChild(menu, ".s-popover", enhance_box);
}
}
catch(err){
console.log(err.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment