copy title

shift+c to copy title link

目前为 2024-01-12 提交的版本。查看 最新版本

// ==UserScript==
// @name           copy title
// @description    shift+c to copy title link
// @name:en        copy title
// @description:en shift+c to copy title link
// @name:ko        페이지 제목 복사
// @description:ko shift+c로 페이지 제목 링크 복사
// @namespace      https://greasyfork.org/ko/users/713014-nanikit
// @version        240113
// @author         nanikit
// @match          *://*/*
// @grant          none
// ==/UserScript==
'use strict';

const copyTitleHandler = (event) => {
  const titleText = document.title;
  const anchor = document.createElement('a');
  anchor.href = document.location.href;
  anchor.innerText = titleText;
  const titleHtml = anchor.outerHTML;
  event.clipboardData.setData("text/html", titleHtml);
  event.clipboardData.setData("text/plain", titleText);
  event.preventDefault();
}

const copyTitle = () => {
  document.addEventListener("copy", copyTitleHandler);
  document.execCommand("copy");
  document.removeEventListener("copy", copyTitleHandler);
};

window.addEventListener('keydown', (event) => {
  if (event.shiftKey && event.code === 'KeyC') {
    copyTitle();
  }
});