Skip to content

Instantly share code, notes, and snippets.

@tmash06
Last active March 5, 2022 07:47
Show Gist options
  • Select an option

  • Save tmash06/b946cfac51cf857bc4e32379aedcf466 to your computer and use it in GitHub Desktop.

Select an option

Save tmash06/b946cfac51cf857bc4e32379aedcf466 to your computer and use it in GitHub Desktop.
Copy a title and an URL to a clipboard. (Alt + c)
// ==UserScript==
// @name Copy a title and an URL to a clipboard. (Alt + c)
// @namespace https://github.com/tmash06
// @version 0.1
// @description Copy a title and an URL to a clipboard. (Alt + c)
// @author tmash06
// @match *://*/*
// @grant none
// ==/UserScript==
(() => {
'use strict';
document.addEventListener('keydown', (event) => {
if (event.altKey && event.key === 'c') {
const title = document.title;
const url = location.href;
const textMessage = `${title}'\n'${url}`;
const htmlMessage = `<a href="${url}">${title}</a>`;
const textBlob = new Blob([textMessage], { type: "text/plain" });
const htmlBlob = new Blob(
[htmlMessage],
{ type: "text/html" }
);
const data = new ClipboardItem({
"text/plain": textBlob,
"text/html": htmlBlob,
});
navigator.clipboard.write([data]);
alert(`Copied to a clipboard:\n${textMessage}`);
}
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment