Last active
March 5, 2022 07:47
-
-
Save tmash06/b946cfac51cf857bc4e32379aedcf466 to your computer and use it in GitHub Desktop.
Copy a title and an URL to a clipboard. (Alt + c)
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
| // ==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