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} )`

当前为 2021-11-20 提交的版本,查看 最新版本

  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.2
  9. // @author snomiao@gmail.com
  10. // @match *://*/*
  11. // @grant none
  12. // ==/UserScript==
  13. // (20210414)更新:修复知乎复制标题 bug
  14. // (20210303)更新:优化提示
  15. // (20200425)更新:优化取标题方案
  16. // (20200425)更新:修改格式
  17. // (20200423)更新:增加格式
  18. (function () {
  19. 'use strict';
  20. var 复制标题文本 = (content) => {
  21. const input = document.createElement('textarea');
  22.  
  23. input.setAttribute('readonly', 'readonly');
  24. input.setAttribute('value', content);
  25. input.innerHTML = content;
  26. input.setAttribute(
  27. 'style',
  28. 'position: fixed; top:0; left:0;z-index: 9999'
  29. );
  30. document.body.appendChild(input);
  31. input.select();
  32. input.setSelectionRange(0, input.value.length);
  33.  
  34. let ok = false;
  35. if (document.execCommand('copy')) {
  36. document.execCommand('copy');
  37. ok = true;
  38. }
  39. document.body.removeChild(input);
  40. ok && alert('标题已复制\n' + content);
  41. ok || alert('copy title failed, please check browser version');
  42. return ok || false;
  43. };
  44. var 取标题 = () => {
  45. const 最长标题 = [
  46. document.title,
  47. document.querySelector('h1')?.innerText || '',
  48. ]
  49. .map((str) => str.replace(/\r?\n.*/g, ''))
  50. .sort((a, b) => a.length - b.length)
  51. .pop();
  52. return 最长标题;
  53. };
  54. window.addEventListener(
  55. 'keydown',
  56. (e) => {
  57. if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  58. 复制标题文本(`[${取标题()}]( ${location.href} )`);
  59. e.preventDefault();
  60. e.stopPropagation();
  61. }
  62. if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  63. 复制标题文本(`# ${取标题()}\n${location.href}`);
  64. e.preventDefault();
  65. e.stopPropagation();
  66. }
  67. },
  68. false
  69. );
  70. })();