|
// ==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); |
|
} |