From e1584b3a9f8903a68864a8f1c314f59579ad7d9e Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Fri, 10 Feb 2023 11:58:36 -0800 Subject: [PATCH] WIP localstorage + add search > marketplace --- github-distractionless/distractionless.js | 46 +++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/github-distractionless/distractionless.js b/github-distractionless/distractionless.js index d1079d3..1f3d1fc 100644 --- a/github-distractionless/distractionless.js +++ b/github-distractionless/distractionless.js @@ -8,13 +8,35 @@ // @website https://github.com/turtlebasket/userscripts/tree/master/github-distractionless // @license MIT // @description Userscript that makes sure that GitHub stays a work tool and doesn't turn into a social media website -// @run-at document-end +// @run-at document-idle // ==/UserScript== let hideEls = []; -let focusing = false; let urlPath = new URL(window.location.href).pathname; +function runHideEls() { + for (let el of hideEls) { + el.setAttribute( + "style", + getFocusing() ? "display: none;" : "display: auto;"); + } +} + +function getFocusing() { + let focusStatus = Boolean(localStorage.getItem("focusOn")); + if (focusStatus == null) { + localStorage.setItem(String(true)) + return true; + } +} + +function toggleFocusing() { + localStorage.setItem(String(!getFocusing())); + if (getFocusing()) { + runHideEls(); + } +} + // title bar links - custom behavior for now const titleBarExclude = ["Explore", "Marketplace", "Codespaces"]; let titleBarEls = document.getElementsByClassName("js-selected-navigation-item") @@ -26,13 +48,15 @@ for (let i = 0; i < titleBarEls.length; i++) { } // general exclusion list +// [, , ] [ ["mail-status unread", [0], /^.*$/], ["UnderlineNav-item", [1], /^\/$/], + ["menu-item", [6], /^\/search[.*]$/], ] -.forEach(([className, hideIndices, pathRegex]) => { - hideIndices.forEach(i => { +.forEach(([className, hideInstances, pathRegex]) => { + hideInstances.forEach(i => { if (urlPath.search(pathRegex) > -1) { let el = document.getElementsByClassName(className)[i]; if (typeof el === 'undefined') { @@ -45,18 +69,8 @@ for (let i = 0; i < titleBarEls.length; i++) { }); }) -// hide all els in els -function toggleFocus() { - focusing = !focusing; - for (let el of hideEls) { - el.setAttribute( - "style", - focusing ? "display: none;" : "display: auto;"); - } -} - // initial state -toggleFocus(); +runHideEls(); // toggle switch // NOTE: WIP, currently bugged due to github content policy. add later @@ -70,7 +84,7 @@ toggleFocus(); // padding: 2px 4px; // font-size: 12pt;`; // const focusModeSwitch = document.createElement("button"); -// focusModeSwitch.setAttribute("innerText", `Focus: ${focusing ? "on" : "off"}`); +// focusModeSwitch.setAttribute("innerText", `Focus: ${getFocusing() ? "on" : "off"}`); // focusModeSwitch.setAttribute("style", btnStyle); // focusModeSwitch.setAttribute("onclick", toggleFocus) // document.getElementsByClassName("Header js-details-container Details")[0]