Copy Title Alt+T

Press Alt+T to copy title and url like this `[${TITLE}](${URL})`

当前为 2020-04-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Copy Title Alt+T
  3. // @namespace https://userscript.snomiao.com/
  4. // @version 0.3
  5. // @description Press Alt+T to copy title and url like this `[${TITLE}](${URL})`
  6. // @author snomiao@gmail.com
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13. var 复制文本 = (content) => {
  14. const input = document.createElement('textarea');
  15. input.setAttribute('readonly', 'readonly');
  16. input.setAttribute('value', content);
  17. input.innerHTML = (content);
  18. input.setAttribute('style', 'position: fixed; top:0; left:0;z-index: 9999');
  19. document.body.appendChild(input);
  20. input.select();
  21. input.setSelectionRange(0, 9999);
  22. console.log('test')
  23. if (document.execCommand('copy')) {
  24. document.execCommand('copy');
  25. var ok = true
  26. }
  27. document.body.removeChild(input);
  28. return ok || false
  29. };
  30. var 取标题 = () => {
  31. var 标题列 = [...document.querySelectorAll('h1')]
  32. return 标题列.length == 1 && 标题列[0].innerText.trim() || document.title || ''
  33. }
  34. window.addEventListener('keydown', (e) => {
  35. if (!(e.altKey && e.code == 'KeyT')) return;
  36. var 标题地址 = `[${取标题()}](${location.href})`
  37. 复制文本(标题地址)
  38. ? alert(标题地址 + '\n copyied!')
  39. : alert('copy title failed, please check browser version')
  40. })
  41. })();