Greasy Fork is available in English.

ChatGPT Const to Var switch for Javascript snippets

ChatGPT always makes const variables in JavaScript snippets, and sometimes refuses to change it. This script solves the problem.

  1. // ==UserScript==
  2. // @name ChatGPT Const to Var switch for Javascript snippets
  3. // @version 1.0.4
  4. // @license GPL-3.0-or-later; https://www.gnu.org/licenses/gpl-3.0.txt
  5. // @author toolzmaker
  6. // @description ChatGPT always makes const variables in JavaScript snippets, and sometimes refuses to change it. This script solves the problem.
  7. // @homepageURL https://discord.gg/BJTk6get7H
  8. // @match *chat.openai.com/chat*
  9. // @namespace https://greatest.deepsurf.us/en/users/971770
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function updateJSCodeSnippets() {
  17. // Find all divs with JavaScript code
  18. var blackRoundedDivs = document.querySelectorAll('div.bg-black.rounded-md');
  19. var javascriptDivs = [];
  20.  
  21. blackRoundedDivs.forEach(div => {
  22. var javascriptSpan = div.querySelector('span');
  23. if (javascriptSpan && javascriptSpan.textContent.includes('javascript')) {
  24. javascriptDivs.push(div);
  25. }
  26. });
  27.  
  28. // Add 'Toggle const/var' button to each div with JavaScript code and attach click event
  29. javascriptDivs.forEach(div => {
  30. if (!div.classList.contains('extrabtns')) {
  31.  
  32. var constSpans = div.querySelectorAll('span.hljs-keyword');
  33. constSpans.forEach(span => {
  34. var variants = ['var', 'let', 'const'];
  35. //if (span.textContent == 'const') {
  36. if (variants.some(word => span.textContent.includes(word))) {
  37. span.addEventListener('click', () => {
  38. //var variants = ['var', 'let', 'const'];
  39. var currentText = span.textContent;
  40. var index = variants.indexOf(currentText);
  41. var nextText = variants[(index + 1) % variants.length];
  42. span.textContent = nextText;
  43. });
  44. }
  45. });
  46.  
  47.  
  48. var existingButton = div.querySelector('button');
  49. if (existingButton && existingButton.textContent === 'Copy code') {
  50. var clonedButton = existingButton.cloneNode(true);
  51. clonedButton.textContent = 'Const/Var';
  52. existingButton.insertAdjacentElement('beforebegin', clonedButton);
  53.  
  54.  
  55. var copyButton = existingButton.cloneNode(true);
  56. copyButton.textContent = 'COPY';
  57. clonedButton.insertAdjacentElement('afterend', copyButton);
  58.  
  59.  
  60. // Attach click event to clonedButton
  61. clonedButton.addEventListener('click', () => {
  62. var keywordSpans = div.querySelectorAll('span.hljs-keyword');
  63. keywordSpans.forEach(span => {
  64. if (span.textContent === 'const') {
  65. span.textContent = 'var';
  66. } else if (span.textContent === 'var') {
  67. span.textContent = 'const';
  68. }
  69. });
  70. });
  71.  
  72. // Attach click event to copyButton
  73. copyButton.addEventListener('click', () => {
  74. var codeElement = div.querySelector('code');
  75. if (codeElement) {
  76. var codeText = codeElement.textContent;
  77. var textArea = document.createElement('textarea');
  78. textArea.value = codeText;
  79. document.body.appendChild(textArea);
  80. textArea.select();
  81. document.execCommand('copy');
  82. document.body.removeChild(textArea);
  83.  
  84. // Change button text style to bold
  85. copyButton.style.fontWeight = 'bold';
  86.  
  87. // Revert button text style to regular after a short delay
  88. setTimeout(() => {
  89. copyButton.style.fontWeight = 'normal';
  90. }, 500);
  91. }
  92. });
  93.  
  94. // Add 'extrabtns' class to div
  95. div.classList.add('extrabtns');
  96.  
  97.  
  98. }
  99. }
  100.  
  101. });
  102. }
  103.  
  104. setInterval(updateJSCodeSnippets, 5000);
  105. })();