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

当前为 2020-12-24 提交的版本,查看 最新版本

  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
  9. // @author snomiao@gmail.com
  10. // @match *://*/*
  11. // @grant none
  12. // ==/UserScript==
  13. // (20200425)更新:优化取标题方案
  14. // (20200425)更新:修改格式
  15. // (20200423)更新:增加格式
  16. (function () {
  17. 'use strict';
  18. var 复制标题文本 = (content) => {
  19. const input = document.createElement('textarea');
  20.  
  21. input.setAttribute('readonly', 'readonly');
  22. input.setAttribute('value', content);
  23. input.innerHTML = (content);
  24. input.setAttribute('style', 'position: fixed; top:0; left:0;z-index: 9999');
  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
  36. ? alert(标题地址 + '\n copyied!')
  37. : alert('copy title failed, please check browser version')
  38. return ok || false
  39. };
  40. var 取标题 = () => {
  41. const 最长标题 = [
  42. document.title,
  43. ...[...document.querySelectorAll('h1')]
  44. .map(e => e.innerText)]
  45. .map(str => str.replace(/\r?\n.*/g, ''))
  46. .sort((a, b) => a.length - b.length)
  47. .pop()
  48. return 最长标题
  49. // var 标题列 = [...document.querySelectorAll('h1')]
  50. // var 页面标题 = 标题列.length == 1 && 标题列[0].innerText.trim() || ''
  51. // var 头标题 = document.title || ''
  52. // return 页面标题.length > 头标题.length ? 页面标题 : 头标题
  53. }
  54. window.addEventListener('keydown', (e) => {
  55. if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyT')
  56. 复制标题文本(`[${取标题()}]( ${location.href} )`)
  57. if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyT')
  58. 复制标题文本(`# ${取标题()}\n${location.href}`)
  59. })
  60. })();