Greasy Fork is available in English.

Leetcode: format on save

Format code on save (Ctrl + S)

  1. // ==UserScript==
  2. // @name Leetcode: format on save
  3. // @namespace http://tampermonkey.net/
  4. // @license MIT
  5. // @version 2023-12-11
  6. // @description Format code on save (Ctrl + S)
  7. // @author You
  8. // @match leetcode.cn/problems/*
  9. // @match leetcode.com/problems/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
  11. // @grant none
  12. // @run-at document-idle
  13. // ==/UserScript==
  14.  
  15. 'use strict';
  16. (function() {
  17. const formatBtnSelector = ".gap-1.items-center > button:nth-of-type(1)"
  18. // const formatBtnSelector = "#format-button"
  19. document.addEventListener('keydown', e => {
  20. try{
  21. const formatBtn = document.querySelector(formatBtnSelector)
  22. if (e.ctrlKey && e.key === 's') formatBtn.click()
  23. } catch {}
  24. });
  25. })();