Copy Last Code Block on ChatGPT Without Header

Adds a button to copy the last code block on chat.openai.com without copying the header

  1. // ==UserScript==
  2. // @name Copy Last Code Block on ChatGPT Without Header
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Adds a button to copy the last code block on chat.openai.com without copying the header
  6. // @author max5555
  7. // @license MIT
  8. // @match https://chat.openai.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create the copy button
  16. const copyButton = document.createElement('button');
  17. copyButton.textContent = 'Copy Last Code';
  18. copyButton.style.position = 'fixed';
  19. copyButton.style.bottom = '20px';
  20. copyButton.style.right = '20px';
  21. copyButton.style.zIndex = '1000';
  22.  
  23. // Add the button to the body
  24. document.body.appendChild(copyButton);
  25.  
  26. // Function to copy the last code block
  27. copyButton.addEventListener('click', () => {
  28. const codeContainers = document.querySelectorAll('pre code');
  29. if (codeContainers.length > 0) {
  30. const lastCodeContainer = codeContainers[codeContainers.length - 1];
  31. navigator.clipboard.writeText(lastCodeContainer.textContent).then(() => {
  32. alert('Code copied to clipboard!');
  33. }).catch(err => {
  34. console.error('Failed to copy text: ', err);
  35. });
  36. } else {
  37. alert('No code blocks found!');
  38. }
  39. });
  40. })();