Created
January 6, 2023 17:25
-
-
Save oaustegard/7a912340bd2c2d54ce196c558a5c7aa1 to your computer and use it in GitHub Desktop.
Bookmarklet to copy a link to the current page formatted as html or markdown depending on destination, originally by @dlenski
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
javascript:void(function() { | |
loc = window.location; | |
page_title = window.document.title; /* create page */ | |
w = window.open(null, null, "height=150,width=900"); | |
d = w.document; | |
d.open(); | |
d.write(`<html><head><title>Create formatted link</title></head> | |
<body> | |
<form onsubmit="return false"> | |
<table> | |
<tr><th>URL: </th> <td><input id="url" type="text" size="100"></td></tr> | |
<tr><th>Link title:</th><td><input id="title" type="text" size="100"></td></tr> | |
<tr><td/> <td><input id="copy" type="submit" value="Copy & Close"/></td></tr> | |
</table> | |
</form> | |
</body></html>`); | |
/* initialize */ | |
l = d.getElementById("url"); | |
t = d.getElementById("title"); | |
c = d.getElementById("copy"); | |
l.value = window.location.toString(); | |
t.value = page_title; | |
/* https://jsfiddle.net/jdhenckel/km7prgv4/3/ */ | |
function copyToClip(doc, html, text = null) { | |
function listener(e) { | |
e.clipboardData.setData("text/html", html); | |
e.clipboardData.setData("text/plain", text || html); | |
e.preventDefault(); | |
} | |
doc.addEventListener("copy", listener); | |
doc.execCommand("copy"); | |
doc.removeEventListener("copy", listener); | |
}; | |
c.onclick = function() { | |
e = d.createElement("a"); | |
e.href = l.value; | |
e.innerHTML = t.value; | |
mdlink = `[${t.value}](${l.value})`; | |
copyToClip(d, e.outerHTML, mdlink); | |
w.close(); | |
}; | |
/* activate torpedos */ | |
d.close(); | |
c.focus(); | |
}) () |
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
## Source | |
https://gist.github.com/dlenski/e598197774666d440503d5168eefed31 | |
## Change | |
rather than plain text url changed the text option to be the markdown for the link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment