OpenAI Chat Copy Code Hotkey

Binds the "Alt+C" hotkey to find and click the HTML element with the text "Copy code" on chat.openai.com.

  1. // ==UserScript==
  2. // @name OpenAI Chat Copy Code Hotkey
  3. // @namespace https://userscript.snomiao.com/
  4. // @version 0.0.1
  5. // @description Binds the "Alt+C" hotkey to find and click the HTML element with the text "Copy code" on chat.openai.com.
  6. // @author snomiao@gmail.com
  7. // @match https://chat.openai.com/*
  8. // @grant none
  9. // @license GPL-3.0+
  10. // @contributionURL https://snomiao.com/donate
  11. // @supportURL https://github.com/snomiao/userscript.js/issues
  12. // @copyright 2017 - 2023, @snomiao <snomiao.com>
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. document.addEventListener("keydown", function (event) {
  19. // Check if the pressed key is 'C' and the 'Alt' key is held down
  20. if (event.key === "c" && event.altKey) {
  21. // Prevent default action (if any)
  22. event.preventDefault();
  23.  
  24. // Find the HTML element with the text "Copy code"
  25. const elements = [...document.getElementsByTagName("*")].reverse();
  26. for (let i = 0; i < elements.length; i++) {
  27. const element = elements[i];
  28. if (element.textContent === "Copy code") {
  29. // Simulate a click event on the element
  30. element.click();
  31. break;
  32. }
  33. }
  34. }
  35. });
  36. })();