LeetCode Focus Mode

just focus!

Verze ze dne 31. 10. 2020. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name LeetCode Focus Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  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. console.info(divs[3])
  55. for(let i = 0, l = divs.length; i < l; i++) {
  56. const e = divs[i];
  57. const m = Object.assign({}, e.dataset);
  58. if (m.hasOwnProperty('isLoading') && m.hasOwnProperty('status')) {
  59. rs = e;
  60. break;
  61. }
  62. }
  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. for (let i = 0, l = ls.length; i < l; i++) {
  83. ls[i].style.display = (show ? 'block' : 'none');
  84. }
  85. }
  86. function toggleFullPage(show = true) {
  87. const el = document.querySelector('.blog-main');
  88. if (!el) return;
  89. if (show) {
  90. el.classList.remove('lc-focus-full')
  91. } else {
  92. el.classList.add('lc-focus-full')
  93. }
  94. }
  95. function btnEvent(e) {
  96. var el = e.target;
  97. var s = el.innerText === 'show';
  98. toggleState(s);
  99. syncState(s);
  100. }
  101. function toggleState(s) {
  102. setTimeout(() => {
  103. toggleBtn(s);
  104. toggleEls(s);
  105. toggleFullPage(s);
  106. }, 20);
  107. }
  108. function syncState(show = false) {
  109. window.localStorage.setItem('lc-focus-show#', show ? 1 : 0)
  110. }
  111. function savedState() {
  112. return window.localStorage.getItem('lc-focus-show#')
  113. }
  114. function initState() {
  115. let s = savedState();
  116. if (s === null) return;
  117. toggleState(+s);
  118. }
  119. function main() {
  120. insertStyle();
  121. insertBtn();
  122. toggleBtn();
  123. initState();
  124. }
  125. main();
  126. })();