LeetCode Reset Code to Default Keybind

Press Ctrl+Shift+X to reset code to default on LeetCode

Från och med 2026-03-01. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         LeetCode Reset Code to Default Keybind
// @namespace    Violentmonkey Scripts
// @match        https://leetcode.com/problems/*
// @grant        none
// @version      1.0
// @author      github.com/geoffkrishnan
// @description Press Ctrl+Shift+X to reset code to default on LeetCode
// @license MIT
// ==/UserScript==

(function() {
  const DEBUG = false;

  document.addEventListener('keydown', function(e) {
    // keybind - change if you want
    if (e.ctrlKey && e.shiftKey && e.key === 'KeyX') {
      e.preventDefault();
      if (DEBUG) console.log('key presses detected');

      const resetButton = document.querySelector('.fa-arrow-rotate-left');
      console.log('reset button:', resetButton);
      if (resetButton) {
        resetButton.closest('button').click();
        if (DEBUG) console.log('reset button clicked');


        const observer = new MutationObserver(() => {
          const confirmBtns = document.querySelectorAll('button');
          for (const cb of confirmBtns) {
            if (DEBUG) console.log('cb: ', cb);
            if (cb.textContent.trim() === 'Confirm') {
              if (DEBUG) console.log('confirm button: ', cb);
              cb.click();
              if (DEBUG) console.log('confirm button clicked');
              observer.disconnect();
            }
          }
        });

        observer.observe(document.body, { childList: true, subtree: true });
        setTimeout(() => observer.disconnect(), 3000);
      } else {
        console.warn('Reset button not found')
      }
    }
  });
})();