LeetCode Focus Mode

just focus!

As of 2020-11-02. See the latest version.

  1. // ==UserScript==
  2. // @name LeetCode Focus Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  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: 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. return rs;
  63. }
  64. function els () {
  65. var usNav = '#navbar-root';
  66. var cat = '.category-group-base';
  67. var sid = '.blog-sidebar';
  68. var cnNav = '#noj-navbar';
  69. var flo = '.floating-layer-container';
  70. var els = [usNav, cnNav, cat, sid, flo];
  71. var rs = [];
  72. for (var i = 0, l = els.length; i < l; i++) {
  73. const el = document.querySelector(els[i]);
  74. if (el) rs.push(el);
  75. }
  76. const nav2 = findNav();
  77. return nav2 ? rs.concat([nav2]) : rs;
  78. }
  79. function toggleEls(show = true) {
  80. var ls = els();
  81. for (let i = 0, l = ls.length; i < l; i++) {
  82. ls[i].style.display = (show ? 'block' : 'none');
  83. }
  84. }
  85. function toggleFullPage(show = true) {
  86. const el = document.querySelector('.blog-main');
  87. if (!el) return;
  88. if (show) {
  89. el.classList.remove('lc-focus-full')
  90. } else {
  91. el.classList.add('lc-focus-full')
  92. }
  93. }
  94. function btnEvent(e) {
  95. var el = e.target;
  96. var s = el.innerText === 'show';
  97. toggleState(s);
  98. syncState(s);
  99. }
  100. function toggleState(s) {
  101. setTimeout(() => {
  102. toggleBtn(s);
  103. toggleEls(s);
  104. toggleFullPage(s);
  105. }, 20);
  106. }
  107. function syncState(show = false) {
  108. window.localStorage.setItem('lc-focus-show#', show ? 1 : 0)
  109. }
  110. function savedState() {
  111. return window.localStorage.getItem('lc-focus-show#')
  112. }
  113. function initState() {
  114. let s = savedState();
  115. if (s === null) return;
  116. window.onload = function(){
  117. toggleState(+s)
  118. };
  119. }
  120. function main() {
  121. insertStyle();
  122. insertBtn();
  123. toggleBtn();
  124. initState();
  125. }
  126. main();
  127. })();