Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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);
})();
@bradleybossard

This comment has been minimized.

Copy link
Owner Author

bradleybossard commented Dec 5, 2015

Create a bookmark in your browser, paste the code above the URL field of the bookmark, and click the bookmark anytime you want to copy a page to the clipboard for easy pasting in a README.md

@nikosgpet

This comment has been minimized.

Copy link

nikosgpet commented Dec 21, 2015

Hi bradley, I didn't manage to get your boorkmarklet to work. I've been driving myself crazy to get something like this to work.

Apparently the functionality for execCommand('copy'), has only been activated recently for browsers. I don't really understand how it is supposed to work. Everyone says it can only work after a user action, like the click of a button, I don't know if clicking at a bookmarklet is considered user action. Anyway, when I execute the above code at the console I get false. When I copy paste the code to a bookmarklet it doesn't do anything...

document.execCommand("copy") 
>>> false

I am using Firefox 43, and Chrome 47.

Is there something I am missing? I would really appreciate your help.

@the-confessor

This comment has been minimized.

Copy link

the-confessor commented Mar 12, 2016

I am too having some problems, though not entirely consistent results. If added a statement to log the response of document.execCommand("copy") to the console to help gain insight.

When using Chrome 48 if I put the script directly into a bookmark it works fine - execCommand returns true and the content ends up in my clipboard - but if I load an external script in a bookmarklet or embed the script into an HTML page then the execCommand returns false and the content does not end up in my clipboard.

When using Firefox 45.0 the execCommand returns false and the content does not end up in my clipboard in all three variations. It also spits the following error out into the console:

“document.execCommand(‘cut/copy’) was denied because it was not called from inside a short running user-generated event handler”

I haven't had a chance to investigate this error yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.