LeetCode solution article widener

Add a toggle to widen the solution articles to view long code easier.

  1. // ==UserScript==
  2. // @name LeetCode solution article widener
  3. // @namespace https://github.com/zica87/self-made-userscipts
  4. // @version 2.1
  5. // @description Add a toggle to widen the solution articles to view long code easier.
  6. // @author zica
  7. // @match https://leetcode.com/*
  8. // @grant none
  9. // @license GPL-3.0
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. // biome-ignore lint/complexity/useRegexLiterals: escaping '/' reduces readability
  16. const solutionPageRegEx = new RegExp(
  17. "https://leetcode.com/problems/.*/solutions/.*/"
  18. );
  19. // biome-ignore lint/complexity/useRegexLiterals: escaping '/' reduces readability
  20. const editorialPageRegEx = new RegExp(
  21. "https://leetcode.com/problems/.*/editorial"
  22. );
  23.  
  24. const toggle = document.createElement("button");
  25. toggle.innerText = "←→";
  26. toggle.onclick = () => {
  27. if (toggle.innerText === "←→") {
  28. const articles = document.getElementsByClassName("mx-auto");
  29. for (const article of articles) {
  30. article.style.maxWidth = "none";
  31. }
  32. toggle.innerText = "default width";
  33. } else {
  34. const articles = document.getElementsByClassName("mx-auto");
  35. for (const article of articles) {
  36. article.style = {};
  37. }
  38. toggle.innerText = "←→";
  39. }
  40. };
  41.  
  42. const observer = new MutationObserver((_, observerInstance) => {
  43. if (
  44. !solutionPageRegEx.test(window.location.href) &&
  45. !editorialPageRegEx.test(window.location.href)
  46. ) {
  47. return;
  48. }
  49. const layoutButton = document.getElementById(
  50. "qd-layout-manager-btn"
  51. )?.parentElement;
  52. if (layoutButton === undefined || layoutButton === null) {
  53. return;
  54. }
  55. observerInstance.disconnect();
  56. layoutButton.before(toggle);
  57. });
  58. observer.observe(document.body, {
  59. childList: true,
  60. subtree: true,
  61. });
  62. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址