Greasy Fork is available in English.

LeetCode Focus Mode

just focus!

  1. // ==UserScript==
  2. // @name LeetCode Focus Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description just focus!
  6. // @author litt1e-p
  7. // @homepage https://github.com/litt1e-p/TampermonkeyScripts/tree/master/leetcode-focus-mode
  8. // @match https://leetcode-cn.com/problemset/*
  9. // @match https://leetcode-cn.com/problems/*
  10. // @match https://leetcode-cn.com/contest/*/problems/*
  11. // @match https://leetcode.com/problemset/*
  12. // @match https://leetcode.com/problems/*
  13. // @match https://leetcode.com/contest/*/problems/*
  14. // @grant none
  15. // @run-at document-idle
  16. // ==/UserScript==
  17. (function(){
  18. function insertStyle() {
  19. const style = document.createElement('style');
  20. style.innerHTML = `
  21. .lc-nav-func-btn {
  22. width: 54px;
  23. height: 32px;
  24. line-height: 20px;
  25. background: #fff;
  26. border: 1px solid #ddd;
  27. color: #666;
  28. font-size: 16px;
  29. z-index: 999999;
  30. position: absolute;
  31. right: 132px;
  32. top: 5px;
  33. user-select: none;
  34. border-radius: 4px;
  35. padding: 0;
  36. }
  37. .lc-focus-full {
  38. width: 96% !important;
  39. }
  40. `
  41. document.body.appendChild(style);
  42. }
  43. function insertBtn() {
  44. var el = document.createElement('button');
  45. el.setAttribute('class', 'lc-nav-func-btn');
  46. el.addEventListener('click', btnEvent)
  47. document.body.appendChild(el);
  48. }
  49. function toggleBtn(active = true) {
  50. var el = document.querySelector('.lc-nav-func-btn');
  51. if (!el) return;
  52. el.innerText = active ? 'hide' : 'show';
  53. }
  54. function findNav() {
  55. let rs;
  56. const divs = [...document.querySelectorAll('div')];
  57. for(let i = 0, l = divs.length; i < l; i++) {
  58. const e = divs[i];
  59. const m = Object.assign({}, e.dataset);
  60. if (m.hasOwnProperty('isLoading') && m.hasOwnProperty('status')) {
  61. rs = e;
  62. break;
  63. }
  64. }
  65. return rs;
  66. }
  67. function els () {
  68. var usNav = '#navbar-root';
  69. var cat = '.category-group-base';
  70. var sid = '.blog-sidebar';
  71. var cnNav = '#noj-navbar';
  72. var flo = '.floating-layer-container';
  73. var els = [usNav, cnNav, cat, sid, flo];
  74. var rs = [];
  75. for (var i = 0, l = els.length; i < l; i++) {
  76. const el = document.querySelector(els[i]);
  77. if (el) rs.push(el);
  78. }
  79. const nav2 = findNav();
  80. return nav2 ? rs.concat([nav2]) : rs;
  81. }
  82. function toggleEls(show = true) {
  83. var ls = els();
  84. for (let i = 0, l = ls.length; i < l; i++) {
  85. ls[i].style.display = (show ? 'block' : 'none');
  86. }
  87. }
  88. function toggleFullPage(show = true) {
  89. const el = document.querySelector('.blog-main');
  90. if (!el) return;
  91. if (show) {
  92. el.classList.remove('lc-focus-full')
  93. } else {
  94. el.classList.add('lc-focus-full')
  95. }
  96. }
  97. function btnEvent(e) {
  98. var el = e.target;
  99. var s = el.innerText === 'show';
  100. toggleState(s);
  101. syncState(s);
  102. }
  103. function toggleState(s) {
  104. setTimeout(() => {
  105. toggleBtn(s);
  106. toggleEls(s);
  107. toggleFullPage(s);
  108. }, 20);
  109. }
  110. function syncState(show = false) {
  111. window.localStorage.setItem('lc-focus-show#', show ? 1 : 0)
  112. }
  113. function savedState() {
  114. return window.localStorage.getItem('lc-focus-show#')
  115. }
  116. function initState() {
  117. let s = savedState();
  118. if (s === null) return;
  119. window.onload = function(){
  120. toggleState(+s)
  121. };
  122. }
  123. function main() {
  124. insertStyle();
  125. insertBtn();
  126. toggleBtn();
  127. initState();
  128. }
  129. main();
  130. })();