Created
January 25, 2024 23:13
-
-
Save RyanAtSnapLogic/969457e51092cc040dfb4f5f79a4aa3a to your computer and use it in GitHub Desktop.
Rework of @dlenski's Bookmarklet to copy current page title as a rich-text formatted link, without a popup window to customize the link
This file contains hidden or 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; | |
copyToClip(d, e.outerHTML, l.value); | |
w.close(); | |
}; | |
/* activate torpedos */ | |
d.close(); | |
c.focus(); | |
}) () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment