LeetCode Focus Mode

just focus!

As of 2020-10-31. See the latest version.

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