brie

joined 1 year ago
1
A Kbin userscript (beehaw.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 

Not a Lemmy script, but I thought it might still be useful for those of you who use Kbin.

The userscript has the following features:

  • Downvotes are hidden;
  • Heavily downvoted posts fade out;
  • The title text of the date also shows in local time;
  • Links to the parent, previous, and next comment in comments;
  • Some of the sidebar items are removed; and
  • Hovering over usernames is disabled.

I tried to make the sections cleanly delineated, so that unwanted features can be easily edited out.

// ==UserScript==
// @name        Kbin script
// @namespace   https://beehaw.org/u/brie
// @match       https://kbin.social/*
// @grant       none
// @version     1.1
// @author      @[email protected]
// @description Changes some aspects of comment display
// @run-at      document-start
// @license     AGPL-3.0-only
// ==/UserScript==

// Custom style
document.head.appendChild(document.createElement('style')).textContent = `\
/* Some color tweaks */
.theme--dark {
  --kbin-success-color: #38fa9f;
}


/* Hide sidebar sections */
.related-magazines,
.active-users,
.posts.section,
.entries.section {
  display: none;
}

/* Hide downvotes */
.comment span[data-subject-target="downvoteCounter"] {
  display: none;
}
`;

window.addEventListener("DOMContentLoaded", () => {
  // Rewrite community links to magazine links
  for (const link of document.querySelectorAll(`a[href^="/c/"]`)) link.setAttribute("href", `/m/${link.getAttribute("href").slice(3)}`);

  const siblings = new Map();
  for (const comment of document.querySelectorAll(`.comment`)) {
    // Fade out heavily downvoted posts (inspired by Hacker-News).
    const upvotes = comment.querySelector(`span[data-subject-target="favCounter"]`)?.textContent | 0;
    const downvotes = comment.querySelector(`span[data-subject-target="downvoteCounter"]`)?.textContent | 0;
    const boosts = comment.querySelector(`span[data-subject-target="upvoteCounter"]`)?.textContent | 0;

    const score =  (upvotes + 3) / (downvotes + 1);
    if (score < 1) {
      comment.style.setProperty("color", `color-mix(in srgb, var(--kbin-section-text-color) ${score*100}%, transparent)`);
      comment.style.setProperty("background", `color-mix(in srgb, var(--kbin-section-bg) ${score*100}%, var(--kbin-bg))`);
    }

    // Show downvotes in the title text
    comment.querySelector(`.vote__down > button`)?.setAttribute("title", `Reduce (${downvotes})`);

    // Show local date
    const timeago = comment.querySelector(`.timeago`);
    if (timeago) {
      const datetime = timeago.getAttribute("datetime");
      if (datetime) timeago.setAttribute("title", `\
${new Date(datetime)}
${datetime}`);
    }

    // HN-style navigation
    const header = comment.querySelector(`:scope > header`);
    const parent = comment.getAttribute("data-subject-parent-value");
    if (parent) {
      const toParent = document.createElement("a");
      toParent.href = `#entry-comment-${parent}`;
      toParent.textContent = "parent"
      header?.appendChild(toParent);
    }
    const sibling = siblings.get(parent);
    siblings.set(parent, comment);

    if (sibling) {
      const prev = document.createElement("a");
      prev.href = `#${sibling.getAttribute("id")}`;
      prev.textContent = "prev";

      if (parent) header?.appendChild(document.createTextNode(" "));
      header?.appendChild(prev);

      const next = document.createElement("a");
      next.href = `#${comment.getAttribute("id")}`;
      next.textContent = "next";
      const siblingHeader = sibling.querySelector(`:scope > header`)
      if (siblingHeader) {
        siblingHeader.appendChild(document.createTextNode(" "));
        siblingHeader.appendChild(next);
      }
    }
  }

  // Neuter the hover actions
  for (const el of document.querySelectorAll(`[data-action="mouseover->kbin#mention"]`)) {
    el.removeAttribute("data-action");
  }
});
[–] [email protected] 6 points 1 year ago (4 children)

GuessKerbal Space Program?

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

Shoot and build, capture intel.

HintThe intel has returned to the heavens.

[–] [email protected] 5 points 1 year ago

As a fellow mostly-lurker, this is also how I felt about downvotes. For me I think it was also because of the concept of having a karma score for the account as a whole, rather only on individual comments.

[–] [email protected] 5 points 1 year ago (2 children)

It has the same plugin system, but they pull from Open VSX rather than Microsoft's extension marketplace. If there's an extension not available there, you can still download it from Microsoft's marketplace and then add it manually.

[–] [email protected] 3 points 1 year ago

Oops. Hopefully it helps others though!

[–] [email protected] 2 points 1 year ago (2 children)

That's actually pretty good for a beginner.

Some tips (take with a grain of salt since some are just personal preference):

Make collatz take a number, and parse the number outside the loop, which also avoids needing to repeat the conversion to int. It also means that you can remove the try-catch from inside collatz, since the only place a ValueError is expected is the conversion to int.

def collatz(number):
    ...

number = None # Declare number to give it the correct scope
while True:
    try:
        number = int(input('Type in a positive integer: '))
        break
    except ValueError:
        print("Input could not be parsed")

print(collatz(number))

Avoid commenting what your code does, and instead focus on getting across the intent, like how you commented that int(number) % 2 == 0 is a check for even numbers. It is fine to leave out comments for pieces of code that are truly self explanatory.

The elif condition can be replaced with just else, since it will only be run if the if condition is false.

Format with Black, it makes things look nicer without having to deal with manual formatting.

[–] [email protected] 2 points 1 year ago

Bad segments are definitely a problem. At least for the channels I watch, the sponsor segments tend to be well done, but for other categories there's often weird segments. Manual skip is still quite useful though.

[–] [email protected] 4 points 1 year ago (1 children)

For searching web archives, there's the Web Archives addon (GPLv3). It just opens a search page though, and doesn't have the ability to automatically archive.

For userscripts I use Violentmonkey (MIT).