Copy Title Alt+T

Press Alt+T to copy title and url like this `# ${TITLE}\n${URL}` and Alt+Shift+T to copy the markdown style link `[${TITLE}]( ${URL} )`

  1. // ==UserScript==
  2. // @name Copy Title Alt+T
  3. // @name:zh Alt+T 复制Markdown格式标题和地址快速分享
  4. // @name:en Alt+T Copy Title and Link as Markdown Style
  5. // @description Press Alt+T to copy title and url like this `# ${TITLE}\n${URL}` and Alt+Shift+T to copy the markdown style link `[${TITLE}]( ${URL} )`
  6. // @description:zh 按 Alt+T 复制 Markdown 格式的链接 `[${TITLE}]( ${URL} )` and Alt+Shift+T 复制 标题和地址 `# ${TITLE}\n${URL}`
  7. // @namespace https://userscript.snomiao.com/
  8. // @version 0.7.3
  9. // @author snomiao@gmail.com
  10. // @match *://*/*
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16. var textCopy = (content) => {
  17. const input = document.createElement('textarea');
  18. input.setAttribute('readonly', 'readonly');
  19. input.setAttribute('value', content);
  20. input.innerHTML = content;
  21. input.setAttribute(
  22. 'style',
  23. 'position: fixed; top:0; left:0;z-index: 9999'
  24. );
  25. document.body.appendChild(input);
  26. input.select();
  27. input.setSelectionRange(0, input.value.length);
  28.  
  29. let ok = false;
  30. if (document.execCommand('copy')) {
  31. document.execCommand('copy');
  32. ok = true;
  33. }
  34. document.body.removeChild(input);
  35. ok && alert('Title Copied\n' + content);
  36. ok || alert('copy title failed, please check browser version');
  37. return ok || false;
  38. };
  39. var TitleGet = () => {
  40. const LongestTitle = [
  41. document.title,
  42. document.querySelector('h1')?.innerText || '',
  43. ]
  44. .map((str) => str.replace(/\r?\n.*/g, ''))
  45. .sort((a, b) => a.length - b.length)
  46. .pop();
  47. return LongestTitle;
  48. };
  49. window.addEventListener(
  50. 'keydown',
  51. (e) => {
  52. if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  53. textCopy(`[${TitleGet()}]( ${location.href} )`);
  54. e.preventDefault();
  55. e.stopPropagation();
  56. }
  57. if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  58. textCopy(`# ${TitleGet()}\n${location.href}`);
  59. e.preventDefault();
  60. e.stopPropagation();
  61. }
  62. },
  63. false
  64. );
  65. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址