Last active
December 13, 2019 12:53
-
-
Save StefRe/4d9bc5cc33fdb2c7ee155a35c0a2716e to your computer and use it in GitHub Desktop.
User script to add links to stackprinter.com for pretty printing SO questions and answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Pretty print stackoverflow questions and answers | |
// @namespace gist.github.com/StefRe | |
// @description Add links to stackprinter for SO questions and answers | |
// @include https://stackoverflow.com/questions/* | |
// @include https://serverfault.com/questions/* | |
// @include https://superuser.com/questions/* | |
// @include https://stackapps.com/questions/* | |
// @include https://askubuntu.com/questions/* | |
// @include https://mathoverflow.net/questions/* | |
// @include https://*.stackexchange.com/questions/* | |
// @include https://*.stackoverflow.com/questions/* | |
// @include https://*.serverfault.com/questions/* | |
// @include https://*.superuser.com/questions/* | |
// @include https://*.stackapps.com/questions/* | |
// @include https://*.askubuntu.com/questions/* | |
// @include https://*.mathoverflow.net/questions/* | |
// @version 1.0 | |
// @grant none | |
// ==/UserScript== | |
function openUrl() { | |
const url = this.dataset.url; | |
if (!window.open(url)) { | |
location.href = url; | |
} | |
} | |
function printLink(url) { | |
const printA = document.createElement('a'); | |
printA.innerHTML = '🖨️'; | |
printA.style.cursor = 'pointer'; | |
printA.onclick = openUrl; | |
printA.setAttribute('data-url', url); | |
printA.style.fontSize = 'x-large'; | |
const printDiv = document.createElement('div'); | |
printDiv.style.textAlign = 'center' | |
printDiv.appendChild(printA); | |
return printDiv; | |
} | |
const re = new RegExp('^https://(.*)\.(?:com|net)/questions/([0-9]*)/'); | |
const group = re.exec(window.location.href); | |
const spurl = 'http://www.stackprinter.com/export?format=HTML&service=' + group[1] + '&question=' + group[2]; | |
const question = document.getElementsByClassName('question')[0].getElementsByClassName('js-vote-down-btn')[0]; | |
question.parentNode.insertBefore(printLink(spurl), question.nextSibling); | |
for (let answer of document.getElementsByClassName('answer')) { | |
let vdbtn = answer.getElementsByClassName('js-vote-down-btn')[0]; | |
vdbtn.parentNode.insertBefore(printLink(spurl + '&answer=' + answer.getAttribute('data-answerid')), vdbtn.nextSibling); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment