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.1
  5. // @description just focus!
  6. // @author litt1e-p
  7. // @match https://leetcode-cn.com/problemset/*
  8. // @match https://leetcode-cn.com/problems/*
  9. // @match https://leetcode-cn.com/contest/*/problems/*
  10. // @match https://leetcode.com/problemset/*
  11. // @match https://leetcode.com/problems/*
  12. // @match https://leetcode.com/contest/*/problems/*
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // ==/UserScript==
  16. (function(){
  17. var nel = document.querySelector('#navbar-root');
  18. if (!nel) return;
  19. function insertStyle() {
  20. const style = document.createElement('style');
  21. style.innerHTML = `
  22. .lc-nav-func-btn {
  23. width: 100px;
  24. height: 44px;
  25. background: #fff;
  26. border: 1px solid #ddd;
  27. color: #666;
  28. font-size: 16px;
  29. z-index: 999999;
  30. position: absolute;
  31. right: 0;
  32. top: 0px;
  33. }
  34. `
  35. document.body.appendChild(style);
  36. }
  37. function insertBtn() {
  38. var el = document.createElement('button');
  39. el.setAttribute('class', 'lc-nav-func-btn');
  40. el.addEventListener('click', btnEvent)
  41. document.body.appendChild(el);
  42. }
  43. function toggleBtn(active = true) {
  44. var el = document.querySelector('.lc-nav-func-btn');
  45. if (!el) return;
  46. el.innerText = active ? 'hide' : 'show';
  47. }
  48. function els () {
  49. var usNav = '#navbar-root';
  50. var cat = '.category-group-base';
  51. var sid = '.blog-sidebar';
  52. var cnNav = '#noj-navbar';
  53. var flo = '.floating-layer-container';
  54. var els = [usNav, cnNav, cat, sid, flo];
  55. var rs = [];
  56. for (var i = 0, l = els.length; i < l; i++) {
  57. const el = document.querySelector(els[i]);
  58. if (el) rs.push(el);
  59. }
  60. return rs;
  61. }
  62. function toggleEls(show = true) {
  63. var ls = els();
  64. for (let i = 0, l = ls.length; i < l; i++) {
  65. ls[i].style.display = (show ? 'block' : 'none');
  66. }
  67. }
  68. function btnEvent(e) {
  69. var el = e.target;
  70. var s = el.innerText === 'show';
  71. toggleState(s);
  72. syncState(s);
  73. }
  74. function toggleState(s) {
  75. setTimeout(() => {
  76. toggleBtn(s);
  77. toggleEls(s);
  78. }, 20);
  79. }
  80. function syncState(show = false) {
  81. window.localStorage.setItem('lc-focus-show#', show ? 1 : 0)
  82. }
  83. function savedState() {
  84. return window.localStorage.getItem('lc-focus-show#')
  85. }
  86. function initState() {
  87. let s = savedState();
  88. if (s === null) return;
  89. toggleState(+s);
  90. }
  91. function main() {
  92. insertStyle();
  93. insertBtn();
  94. toggleBtn();
  95. initState();
  96. }
  97. main();
  98. })();