Kemono.Party - User Filter

Block specified user in artists page and posts page.

当前为 2023-07-28 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Kemono.Party - User Filter
// @description  Block specified user in artists page and posts page.
// @version      1.10
// @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);
  else {
    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() {
  let is_posts_page = location.pathname.indexOf('/posts') == 0;
  if (is_posts_page) document.querySelectorAll('article.post-card').forEach(card => addBlockButtonTo(card, true));
  let items = document.querySelector('.card-list__items');
  if (items) {
    new MutationObserver(mutations => mutations.forEach(mutation => mutation.addedNodes.forEach(card => {
      if (card.classList.contains('post-card') || card.classList.contains('user-card')) addBlockButtonTo(card, is_posts_page);
    }))).observe(document.querySelector('.card-list__items'), {childList: true, subtree: false});
  }
}

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

function blockUser(service, user, is_blocked, user_card) {
  if (user_card) blockCard(user_card, is_blocked);
  else {
    let post_cards = document.querySelectorAll(`article.post-card[data-service="${service}"][data-user="${user}"]`);
    post_cards.forEach(post_card => blockCard(post_card, is_blocked));
  }
  //update and save blacklists
  let user_id = service + '_' + user;
  if (is_blocked) {
    blacklists = blacklists.filter(id => id !== user_id);
  } else {
    blacklists.push(user_id);
  }
  GM_setValue('blacklists', blacklists);
}

function blockCard(card, is_blocked) {
  if (is_blocked) card.removeAttribute('data-blocked');
  else card.setAttribute('data-blocked', true);
}

function hintUser(service, user, is_blocked, onmouseover) {
  let post_cards = document.querySelectorAll(`article.post-card[data-service="${service}"][data-user="${user}"]`);
  post_cards.forEach(post_card => {
    if (onmouseover) {
      post_card.setAttribute(is_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';}
/* UI fix for AutoPagerize */
.autopagerize_page_separator, .autopagerize_page_info {flex: unset; width: 100%;}
`;
  document.head.insertAdjacentHTML('beforeend', `<style>${css}</style>`);
}