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