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.5
  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 els () {
  52. var usNav = '#navbar-root';
  53. var cat = '.category-group-base';
  54. var sid = '.blog-sidebar';
  55. var cnNav = '#noj-navbar';
  56. var flo = '.floating-layer-container';
  57. var els = [usNav, cnNav, cat, sid, flo];
  58. var rs = [];
  59. for (var i = 0, l = els.length; i < l; i++) {
  60. const el = document.querySelector(els[i]);
  61. if (el) rs.push(el);
  62. }
  63. return rs;
  64. }
  65. function toggleEls(show = true) {
  66. var ls = els();
  67. for (let i = 0, l = ls.length; i < l; i++) {
  68. ls[i].style.display = (show ? 'block' : 'none');
  69. }
  70. }
  71. function toggleFullPage(show = true) {
  72. const el = document.querySelector('.blog-main');
  73. if (!el) return;
  74. if (show) {
  75. el.classList.remove('lc-focus-full')
  76. } else {
  77. el.classList.add('lc-focus-full')
  78. }
  79. }
  80. function btnEvent(e) {
  81. var el = e.target;
  82. var s = el.innerText === 'show';
  83. toggleState(s);
  84. syncState(s);
  85. }
  86. function toggleState(s) {
  87. setTimeout(() => {
  88. toggleBtn(s);
  89. toggleEls(s);
  90. toggleFullPage(s);
  91. }, 20);
  92. }
  93. function syncState(show = false) {
  94. window.localStorage.setItem('lc-focus-show#', show ? 1 : 0)
  95. }
  96. function savedState() {
  97. return window.localStorage.getItem('lc-focus-show#')
  98. }
  99. function initState() {
  100. let s = savedState();
  101. if (s === null) return;
  102. toggleState(+s);
  103. }
  104. function main() {
  105. insertStyle();
  106. insertBtn();
  107. toggleBtn();
  108. initState();
  109. }
  110. main();
  111. })();