Last active
October 30, 2024 19:19
-
-
Save shu8/9203772dd22c4bc04aafebd4fc4f8ca1 to your computer and use it in GitHub Desktop.
A userscript that lets you quickly copy content on SE as markdown. Just select and pres Ctrl+Alt+M
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 Copy as markdown | |
// @namespace http://stackexchange.com/users/4337810/ | |
// @version 1.3 | |
// @description A userscript that lets you quickly copy content on SE as markdown. Just select and pres Ctrl+Alt+M | |
// @author ᔕᖺᘎᕊ (http://stackexchange.com/users/4337810/) | |
// @match *://*.stackexchange.com/* | |
// @match *://*.stackoverflow.com/* | |
// @match *://*.superuser.com/* | |
// @match *://*.serverfault.com/* | |
// @match *://*.askubuntu.com/* | |
// @match *://*.stackapps.com/* | |
// @match *://*.mathoverflow.net/* | |
// @require https://unpkg.com/turndown/dist/turndown.js | |
// @grant none | |
// ==/UserScript== | |
const turndownService = new TurndownService(); | |
turndownService.addRule('div', { | |
filter: ['div'], | |
replacement: function(html, node) { | |
return '\n ' + html; | |
} | |
}); | |
turndownService.addRule('script', { | |
filter: ['script'], | |
replacement: function(html, node) { | |
return $(node).text(); | |
} | |
}); | |
function getSelectionHtml() { //http://stackoverflow.com/a/6668159/3541881 | |
if (typeof window.getSelection != "undefined") { | |
const sel = window.getSelection(); | |
if (sel.rangeCount) { | |
const container = document.createElement("div"); | |
for (let i = 0, len = sel.rangeCount; i < len; ++i) { | |
container.appendChild(sel.getRangeAt(i).cloneContents()); | |
} | |
return container.innerHTML; | |
} | |
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") { | |
return document.selection.createRange().htmlText; | |
} | |
} | |
$(document).on('keyup', function(e) { | |
if (e.ctrlKey && e.altKey && e.which == 77) { | |
const parsed = $.parseHTML(getSelectionHtml()); | |
const c = $('<div/>'); | |
$.each(parsed, function(i, v) { | |
c.append(v); | |
}); | |
c.find('*').filter(function() { | |
return $(this).text().trim() === ''; | |
}).remove(); | |
const markdown = turndownService.turndown(c.html()); | |
console.log(markdown); | |
prompt('Press Ctrl+C to copy markdown', markdown); | |
} | |
}); |
@vbrozik Thanks for letting me know -- it should be fixed now! :)
Thanks, it works again!
Installation URL of this script for Tampermonkey:
https://gist.github.com/shu8/9203772dd22c4bc04aafebd4fc4f8ca1/raw/markdownCopier.user.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Chrome 101 the script does not seem to do anything after pressing Ctrl+Alt+m.
Note here is the script's page at StackExchange: https://stackapps.com/questions/6894/copy-post-content-as-markdown-quick-and-easy-way-to-quote-people
I have found a different script which works in my Chrome 101: