Kemono.Party - User Filter

Block specified user in artists page and posts page.

Stan na 26-07-2023. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Kemono.Party - User Filter
// @description  Block specified user in artists page and posts page.
// @version      1.01
// @match        https://*.kemono.su/posts*
// @match        https://*.kemono.su/artists*
// @match        https://*.kemono.party/posts*
// @match        https://*.kemono.party/artists*
// @namespace    none
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==
/* jshint esversion: 6 */

let blacklists = GM_getValue('blacklists', []);
let filter_enabled = GM_getValue('filter_enabled', true);

addStyle();
addFilterButton();
addBlockButton();

function addFilterButton() {
  let ptop = document.querySelector('#paginator-top');
  let menu = ptop.querySelector('menu');
  if (menu) addFilterButtonTo(menu); //posts page
  else {
    //artists page
    new MutationObserver(() => {
      let menu = ptop.querySelector('menu');
      if (menu) addFilterButtonTo(menu);
    }).observe(ptop, {childList: true, subtree: false});
  }
}

function addFilterButtonTo(menu) {
  let btn_switch = document.createElement('a');
  btn_switch.classList.add('filter-switch');
  btn_switch.innerHTML = '<b>Filter</b>';
  if (filter_enabled) menu.closest('section').classList.add('filter-enabled');
  else btn_switch.classList.add('pagination-button-disabled');
  menu.insertBefore(btn_switch, menu.firstChild);
  btn_switch.onclick = () => {
    filter_enabled = !filter_enabled;
    menu.closest('section').classList.toggle('filter-enabled');
    btn_switch.classList.toggle('pagination-button-disabled');
    GM_setValue('filter_enabled', filter_enabled);
  };
}

function addBlockButton() {
  //posts page
  document.querySelectorAll('article.post-card').forEach(post_card => {
    let service = post_card.dataset.service;
    let user = post_card.dataset.user;
    addBlockButtonTo(post_card, service, user, true);
  });
  //artists page
  let items = document.querySelector('.card-list__items');
  if (items) {
    new MutationObserver(mutations => {
      mutations[0].addedNodes.forEach(user_card => {
        let service = user_card.href.split('/')[3];
        let user = user_card.href.split('/').pop();
        addBlockButtonTo(user_card, service, user);
      });
    }).observe(items, {childList: true, subtree: false});
  }
}

function addBlockButtonTo(target, service, user, posts_page) {
  let blocked = blacklists.indexOf(service + '_' + user) >= 0;
  if (blocked) target.dataset.blocked = true;
  let btn_block = document.createElement('label');
  btn_block.classList.add('btn-block');
  btn_block.innerHTML = `<b></b>`;
  (target.querySelector('footer') || target).appendChild(btn_block);
  btn_block.onclick = e => {
    e.preventDefault();
    btn_block.closest('a').blur();
    blockUser(service, user, target.dataset.blocked, posts_page ? null : target);
  };
  if (posts_page) {
    btn_block.onmouseover = () => hintUser(service, user, target.dataset.blocked, true);
    btn_block.onmouseout = () => hintUser(service, user);
  }
}

function blockUser(service, user, blocked, user_card) {
  const blockTarget = (target, blocked) => {
    if (blocked) target.removeAttribute('data-blocked');
    else target.setAttribute('data-blocked', true);
  }
  //block user_card in artists page
  if (user_card) blockTarget(user_card, blocked);
  else {
    //block all posts from same user in posts page
    document.querySelectorAll(`article.post-card[data-service="${service}"][data-user="${user}"]`)
    .forEach(user_post => blockTarget(user_post, blocked));
  }
  //update and save blacklists
  let user_id = service + '_' + user;
  if (blocked) {
    blacklists = blacklists.filter(id => id !== user_id);
  } else {
    blacklists.push(user_id);
  }
  GM_setValue('blacklists', blacklists);
}

function hintUser(service, user, blocked, onmouseover) {
  document.querySelectorAll(`article.post-card[data-service="${service}"][data-user="${user}"]`)
  .forEach(post_card => {
    if (onmouseover) {
      post_card.setAttribute(blocked ? 'data-hint-unblock' : 'data-hint-block', true);
    } else {
      post_card.removeAttribute('data-hint-block');
      post_card.removeAttribute('data-hint-unblock');
    }
  });
}

function addStyle() {
  let css = `
menu > a.filter-switch {color: orange;}
.filter-enabled [data-blocked] {display: none;}
.user-card, .post-card > a {transition: box-shadow .25s ease, opacity .25s ease;}
.user-card[data-blocked], .post-card[data-blocked] > a {opacity: 0.75; box-shadow: 0 0 4px 2px orangered;}
.post-card[data-hint-block] > a {box-shadow: 0 0 4px 2px orange;}
.post-card[data-hint-unblock][data-blocked] > a {opacity: 1; box-shadow: 0 0 4px 2px orange;}
.user-card:not([data-blocked]) .btn-block:not(:hover) b {visibility: hidden;}
.post-card:not([data-blocked]) footer:not(:hover) .btn-block {display: none;}
.btn-block {padding: 10px; position: absolute; right: -5px; bottom: -5px;}
.btn-block > b {color: white; background-color: orangered; border: 1px solid black; border-radius: 4px; padding: 0 4px;}
.btn-block > b::before {content: 'Block User'}
[data-blocked] .btn-block > b::before {content: 'Blocked';}
[data-blocked] .btn-block:hover > b {background-color: lightgreen;}
[data-blocked] .btn-block:hover > b::before {content: 'Unblock';}
`;
  document.head.insertAdjacentHTML('beforeend', `<style>${css}</style>`);
}