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

  1. // ==UserScript==
  2. // @name Copy Title Alt+T
  3. // @name:zh Alt+T 复制标题和地址快速分享
  4. // @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})`
  5. // @description:zh 按 Alt+T 复制标题和地址 `# ${TITLE}\n${URL}` and Alt+Shift+T 复制 Markdown 格式的链接 `[${TITLE}](${URL})`
  6. // @namespace https://userscript.snomiao.com/
  7. // @version 0.4
  8. // @author snomiao@gmail.com
  9. // @match *://*/*
  10. // @grant none
  11. // ==/UserScript==
  12. // (20200423)更新:增加格式
  13. (function () {
  14. 'use strict';
  15. var 复制文本 = (content) => {
  16. const input = document.createElement('textarea');
  17. input.setAttribute('readonly', 'readonly');
  18. input.setAttribute('value', content);
  19. input.innerHTML = (content);
  20. input.setAttribute('style', 'position: fixed; top:0; left:0;z-index: 9999');
  21. document.body.appendChild(input);
  22. input.select();
  23. input.setSelectionRange(0, 9999);
  24. if (document.execCommand('copy')) {
  25. document.execCommand('copy');
  26. var ok = true
  27. }
  28. document.body.removeChild(input);
  29. return ok || false
  30. };
  31. var 取标题 = () => {
  32. var 标题列 = [...document.querySelectorAll('h1')]
  33. return 标题列.length == 1 && 标题列[0].innerText.trim() || document.title || ''
  34. }
  35. window.addEventListener('keydown', (e) => {
  36. if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyT')
  37. 复制文本(`# ${取标题()}\n${location.href}`)
  38. ? alert(标题地址 + '\n copyied!')
  39. : alert('copy title failed, please check browser version')
  40. if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyT')
  41. 复制文本(`[${取标题()}](${location.href})`)
  42. ? alert(标题地址 + '\n copyied!')
  43. : alert('copy title failed, please check browser version')
  44.  
  45. })
  46. })();