copy title

alt+c to copy title link

  1. // ==UserScript==
  2. // @name copy title
  3. // @description alt+c to copy title link
  4. // @name:en copy title
  5. // @description:en alt+c to copy title link
  6. // @name:ko 페이지 제목 복사
  7. // @description:ko alt+c로 페이지 제목 링크 복사
  8. // @namespace https://greasyfork.org/ko/users/713014-nanikit
  9. // @version 240207
  10. // @author nanikit
  11. // @match *://*/*
  12. // @grant none
  13. // ==/UserScript==
  14. 'use strict';
  15.  
  16. addEventListener('keydown', (event) => {
  17. if (event.altKey && event.code === 'KeyC') {
  18. copyTitle();
  19. }
  20. });
  21.  
  22. async function copyTitle() {
  23. const anchor = createTitleAnchor();
  24. await setClipboard([
  25. new Blob([anchor.outerHTML], { type: 'text/html' }),
  26. new Blob([anchor.innerText], { type: 'text/plain' })
  27. ]);
  28. }
  29.  
  30. async function setClipboard(blobs) {
  31. const item = blobs.reduce((data, blob) => (data[blob.type] = blob, data), {});
  32. await navigator.clipboard.write([new ClipboardItem(item)]);
  33. }
  34.  
  35. function createTitleAnchor(){
  36. const titleText = document.title;
  37. const anchor = document.createElement('a');
  38. anchor.href = document.location.href;
  39. anchor.innerText = titleText;
  40. return anchor
  41. }