HN Blocker

Block users on the orange website

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         HN Blocker
// @namespace    http://news.ycombinator.com/
// @version      0.1
// @description  Block users on the orange website
// @author       A. Person
// @match        https://news.ycombinator.com/item*
// @icon         https://www.google.com/s2/favicons?domain=ycombinator.com
// @grant        GM.setValue
// @grant        GM.getValue
// @grant        GM.addStyle
// @license      MIT
// ==/UserScript==

GM.addStyle(`
.default .comment.blocked span.commtext, .reply {
    display:none !important;
}

.default .comment.blocked:not(:hover)::after {
    content: "[blocked]";
}

.default .comment.blocked:hover span.commtext {
    display:block !important;
}
`);

var users = [];
async function getBlockedUsers() {
  users = await GM.getValue("hn_banned", []);
  console.log(`[HN] Loaded ${users.length} users`);
}

let banUser = function (name) {
  users.push(name);
  GM.setValue("hn_banned", users);
};

let unbanUser = function (name) {
  var i = users.indexOf(name);
  if (i !== -1) users.splice(i, 1);
  GM.setValue("hn_banned", users);
};

function handleItem(item) {
  var username = item.querySelector("a").text;
  let commentEl = item.querySelector(".comment");

  var seperator = item.querySelector(".hn_bl_seperator");

  if (seperator !== null) {
    seperator.remove();
    seperator = null;
  }

  seperator = document.createElement("span");
  seperator.innerHTML = " | ";
  seperator.className = "hn_bl_seperator";

  var actor = document.createElement("a");
  actor.href = "#";

  item.querySelector(".comhead").appendChild(seperator);
  seperator.appendChild(actor);

  var blockedMessage = item.querySelector(".blocked");

  if (blockedMessage !== null) {
    blockedMessage.remove();
  }

  if (users.includes(username)) {
    commentEl.classList.add("blocked");

    actor.innerHTML = "unblock";
    actor.onclick = function () {
      unbanUser(username);
      commentEl.classList.remove("blocked");
      return false;
    };
  } else {
    commentEl.classList.remove("blocked");

    actor.innerHTML = "block";
    actor.onclick = function () {
      banUser(username);
      commentEl.classList.add("blocked");
      return false;
    };
  }
}

(async function () {
  "use strict";

  await getBlockedUsers();

  var comments = document.querySelectorAll(".default");
  console.log("got comments", comments.length);

  comments.forEach((comment) => {
    handleItem(comment);
  });
})();