Last active
March 16, 2017 20:58
-
-
Save philippeowagner/e77990f73a667f932994beb4770a221d to your computer and use it in GitHub Desktop.
A bookmarklet that takes an HTML selection and converts it to Markdown
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
// GiveMeMarkdown is a bookmarklet that takes an HTML selection and converts it to Markdown | |
// javascript:(function(){var p=document.createElement("p");p.innerHTML="<strong>Loading…</strong>";p.id="loadingp";p.style.padding="20px";p.style.background="#fff";p.style.left="20px";p.style.top=0;p.style.position="fixed";p.style.zIndex="9999999";p.style.opacity=".85";document.body.appendChild(p);document.body.appendChild(document.createElement("script")).src="http://inflexion.ch/getselection.js?x="+(Math.random());})(); | |
(function () { | |
function callback() { | |
(function ($) { | |
var raw, userSelection; | |
if (window.getSelection) { | |
// W3C Ranges | |
userSelection = window.getSelection (); | |
// Get the range: | |
if (userSelection.getRangeAt) | |
var range = userSelection.getRangeAt (0); | |
else { | |
var range = document.createRange (); | |
range.setStart (userSelection.anchorNode, userSelection.anchorOffset); | |
range.setEnd (userSelection.focusNode, userSelection.focusOffset); | |
} | |
// And the HTML: | |
var clonedSelection = range.cloneContents (); | |
var div = document.createElement ('div'); | |
div.appendChild (clonedSelection); | |
raw = div.innerHTML; | |
} else if (document.selection) { | |
// Explorer selection, return the HTML | |
userSelection = document.selection.createRange (); | |
raw = userSelection.htmlText; | |
} else { | |
raw = ""; | |
} | |
raw = raw.replace(/(src|href)=\"(.*?)\"/g, function(match, p1, p2, offset, string) { | |
if (!/^http/.test(p2)) { | |
if (/^\//.test(p2)) | |
var baselink = window.location.protocol + "//" + window.location.hostname; | |
else | |
var baselink = window.location.href.split("/").slice(0,-1).join("/") + "/"; | |
return [p1,"=\"",baselink+p2,"\""].join(''); | |
} | |
return match; | |
}) | |
var showFrame = "0"; | |
if (window.markerShowFrame !== undefined) { | |
switch (window.markerShowFrame) { | |
case 0: | |
case false: showFrame = "0"; break; | |
default: showFrame = "1"; | |
} | |
} | |
var $form = $("<form>").attr("method", "post").attr("action", "http://heckyesmarkdown.com/go/") | |
.append($("<input name=html>").val(encodeURIComponent(raw))) | |
.append($("<input name=read>").val("0")) | |
.append($("<input name=output>").val("md")) | |
.append($("<input name=showframe>").val(showFrame)) | |
.appendTo('body'); | |
$form.submit(); | |
})(jQuery.noConflict(true)) | |
} | |
var s = document.createElement("script"); | |
s.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"; | |
if (s.addEventListener) { | |
s.addEventListener("load", callback, false) | |
} else if (s.readyState) { | |
s.onreadystatechange = callback | |
} | |
document.body.appendChild(s); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment