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-09-27 提交的版本,查看 最新版本

  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('style', 'position: fixed; top:0; left:0;z-index: 9999');
  27. document.body.appendChild(input);
  28. input.select();
  29. input.setSelectionRange(0, input.value.length);
  30.  
  31. let ok = false
  32. if (document.execCommand('copy')) {
  33. document.execCommand('copy');
  34. ok = true
  35. }
  36. document.body.removeChild(input);
  37. ok && alert('标题已复制\n' + content)
  38. ok || alert('copy title failed, please check browser version')
  39. return ok || false
  40. };
  41. var 取标题 = () => {
  42. const 最长标题 = [
  43. document.title,
  44. document.querySelector('h1')?.innerText || ''
  45. ]
  46. .map(str => str.replace(/\r?\n.*/g, ''))
  47. .sort((a, b) => a.length - b.length)
  48. .pop()
  49. return 最长标题
  50. }
  51. window.addEventListener('keydown', (e) => {
  52. if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  53. 复制标题文本(`[${取标题()}]( ${location.href} )`)
  54. e.preventDefault()
  55. e.stopPropagation()
  56. }
  57. if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyT') {
  58. 复制标题文本(`# ${取标题()}\n${location.href}`)
  59. e.preventDefault()
  60. e.stopPropagation()
  61. }
  62. }, false)
  63. })();