Skip to content

Instantly share code, notes, and snippets.

@djsnipa1
Forked from bradleybossard/titleUrlMarkdownClip.js
Last active April 26, 2023 17:44
Show Gist options
  • Save djsnipa1/28d4c9fb275d4fdf92ba91ac45da70fd to your computer and use it in GitHub Desktop.
Save djsnipa1/28d4c9fb275d4fdf92ba91ac45da70fd to your computer and use it in GitHub Desktop.
Bookmarklet to copy current page title and url in Markdown format to clipboard, like [title](url) - Usual for posting links to resources in README.md files
// Converted using Bookmarklet Creator with Script Includer - https://mrcoles.com/bookmarklet/
javascript:(function()%7Bjavascript%3A(function()%20%7Bfunction%20copyToClipboard(text)%20%7Bif%20(window.clipboardData%20%26%26%20window.clipboardData.setData)%20%7B%2F*IE%20specific%20code%20path%20to%20prevent%20textarea%20being%20shown%20while%20dialog%20is%20visible.*%2Freturn%20clipboardData.setData(%22Text%22%2C%20text)%3B%7D%20else%20if%20(document.queryCommandSupported%20%26%26%20document.queryCommandSupported(%22copy%22))%20%7Bvar%20textarea%20%3D%20document.createElement(%22textarea%22)%3Btextarea.textContent%20%3D%20text%3Btextarea.style.position%20%3D%20%22fixed%22%3B%20%20%2F*%20Prevent%20scrolling%20to%20bottom%20of%20page%20in%20MS%20Edge.*%2Fdocument.body.appendChild(textarea)%3Btextarea.select()%3Btry%20%7Breturn%20document.execCommand(%22copy%22)%3B%20%20%2F*%20Security%20exception%20may%20be%20thrown%20by%20some%20browsers.*%2F%7D%20catch%20(ex)%20%7Bconsole.warn(%22Copy%20to%20clipboard%20failed.%22%2C%20ex)%3Breturn%20false%3B%7D%20finally%20%7Bdocument.body.removeChild(textarea)%3B%7D%7D%7Dvar%20markdown%20%3D%20'%5B'%20%2B%20document.title%20%2B%20'%5D('%20%2B%20window.location.href%20%2B%20')'%3BcopyToClipboard(markdown)%3Balert(%60Copied%20to%20clipboard%3A%20%5Cn%24%7Bmarkdown%7D%60)%3B%7D)()%7D)()
javascript:(function() {
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
/*IE specific code path to prevent textarea being shown while dialog is visible.*/
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
var markdown = '[' + document.title + '](' + window.location.href + ')';
copyToClipboard(markdown);
alert(`Copied to clipboard: \n
${markdown}`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment