[snolab] Google 日历键盘操作增强

【功能测试中, bug反馈:snomiao@gmail.com】Google日历键盘增强,雪星自用,功能:双击复制日程视图里的文本内容

Verze ze dne 20. 11. 2021. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name [snolab] Google 日历键盘操作增强
  3. // @name:zh [雪星实验室] Google Calendar with Keyboard Enhanced
  4. // @namespace https://userscript.snomiao.com/
  5. // @version 0.0.2
  6. // @description 【功能测试中, bug反馈:snomiao@gmail.com】Google日历键盘增强,雪星自用,功能:双击复制日程视图里的文本内容
  7. // @author snomiao@gmail.com
  8. // @match *://calendar.google.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. var clk = (sel) => {
  13. var e = document.querySelector(sel);
  14. console.log('clk', e);
  15. e?.focus();
  16. e?.click();
  17. return !!e;
  18. };
  19. var stmo = () => clk('input[aria-label="Start time"]');
  20. var etmo = () => clk('input[aria-label="End time"]');
  21.  
  22. document.onkeydown = (e) => {
  23. var isInput = ['INPUT', 'BUTTON'].includes(e.target.tagName);
  24. var { altKey, ctrlKey, shiftKey, key } = e;
  25. var keyName =
  26. (altKey ? '!' : '') +
  27. (ctrlKey ? '^' : '') +
  28. (shiftKey ? '+' : '') +
  29. key.toLowerCase();
  30. var okay = () => {
  31. e.defaultPrevented();
  32. e.stopPropagation();
  33. };
  34. console.log(keyName + ' pressed on ', e.target.tagName);
  35. if (keyName === '!s') {
  36. stmo();
  37. okay();
  38. return;
  39. }
  40. if (keyName === '!e') {
  41. etmo();
  42. okay();
  43. return;
  44. }
  45. };
  46.  
  47. var 复制文本 = (content) => {
  48. const input = document.createElement('textarea');
  49. input.setAttribute('readonly', 'readonly');
  50. input.setAttribute('value', content);
  51. input.innerHTML = content;
  52. input.setAttribute('style', 'position: fixed; top:0; left:0;z-index: 9999');
  53. document.body.appendChild(input);
  54. input.select();
  55. input.setSelectionRange(0, input.value.length);
  56. if (document.execCommand('copy')) {
  57. document.execCommand('copy');
  58. // console.log(`长度为${content.length}的内容已复制`);
  59. // alert(`长度为${content.length}的内容已复制`);
  60. }
  61. document.body.removeChild(input);
  62. };
  63.  
  64. // 复制日程内容
  65. var cpy = (ele) => {
  66. ele.style.background = 'lightblue';
  67. setTimeout(() => (ele.style.background = 'none'), 200);
  68. return 复制文本(
  69. ele.innerText
  70. // 把时间和summary拼到一起
  71. .replace(
  72. /.*\n(.*) – (.*)\n(.*)\n.*/gim,
  73. (_, a, b, c) => a + '-' + b + ' ' + c
  74. )
  75. // 删掉前2行
  76. .replace(/^.*\n.*\n/, '')
  77. );
  78. };
  79. document.body.addEventListener(
  80. 'mousedown',
  81. () => {
  82. [...document.querySelectorAll('div.L1Ysrb')].map((e) => {
  83. if (!e.flag_cpy_eventlistener) {
  84. e.addEventListener('dblclick', () => cpy(e), false);
  85. }
  86. e.flag_cpy_eventlistener = 1;
  87. });
  88. },
  89. true
  90. );