Reverso Context Ctrl+C+C Adapter

Convert Ctrl+C+C to Ctrl+Alt+R, YouTube Captions integration

  1. // ==UserScript==
  2. // @name Reverso Context Ctrl+C+C Adapter
  3. // @namespace https://greasyfork.org/en/users/901750-gooseob
  4. // @version 1.0
  5. // @description Convert Ctrl+C+C to Ctrl+Alt+R, YouTube Captions integration
  6. // @author GooseOb
  7. // @license MIT
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. let ytCallback = null;
  16.  
  17. if (window.location.hostname === "www.youtube.com") {
  18. const phantomElement = document.createElement("div");
  19. phantomElement.style.position = "absolute";
  20. phantomElement.style.fontSize = "0";
  21. document.body.appendChild(phantomElement);
  22.  
  23. ytCallback = () => {
  24. const captionsText = Array.from(
  25. document.querySelectorAll(".html5-video-player .captions-text > span"),
  26. (el) => el.textContent,
  27. )
  28. .join(" ")
  29. .trim();
  30.  
  31. if (captionsText) {
  32. phantomElement.textContent = captionsText;
  33.  
  34. const range = document.createRange();
  35. range.selectNodeContents(phantomElement);
  36. const selection = window.getSelection();
  37. selection.removeAllRanges();
  38. selection.addRange(range);
  39. }
  40. };
  41. }
  42.  
  43. let ctrlPressed = false;
  44. let cCount = 0;
  45. let lastCPressTime = 0;
  46. const TIME_THRESHOLD = 300;
  47.  
  48. document.addEventListener("keydown", (e) => {
  49. ctrlPressed = e.ctrlKey;
  50.  
  51. if (ctrlPressed && e.code === "KeyC") {
  52. const currentTime = Date.now();
  53.  
  54. cCount = currentTime - lastCPressTime > TIME_THRESHOLD ? 1 : cCount + 1;
  55.  
  56. lastCPressTime = currentTime;
  57.  
  58. if (cCount === 2) {
  59. ytCallback?.();
  60.  
  61. document.dispatchEvent(
  62. new KeyboardEvent("keydown", {
  63. key: "r",
  64. keyCode: 82,
  65. ctrlKey: true,
  66. altKey: true,
  67. bubbles: true,
  68. cancelable: true,
  69. }),
  70. );
  71.  
  72. cCount = 0;
  73. lastCPressTime = 0;
  74. }
  75. }
  76. });
  77.  
  78. document.addEventListener("keyup", (e) => {
  79. if (!e.ctrlKey) {
  80. ctrlPressed = false;
  81. cCount = 0;
  82. lastCPressTime = 0;
  83. }
  84. });
  85. })();