☰

F95 Reset Filters

Reset individual filters in the filter drawer on the latest update page.

您需要先安裝使用者腳本管理器擴展,如 Tampermonkey、Greasemonkey 或 Violentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 Tampermonkey或 Violentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 Tampermonkey或 Userscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        F95 Reset Filters
// @namespace   1330126-edexal
// @match       *://f95zone.to/sam/latest_alpha/*
// @grant       none
// @icon        https://external-content.duckduckgo.com/ip3/f95zone.to.ico
// @license     Unlicense
// @version     1.0.19
// @author      Edexal
// @description Reset individual filters in the filter drawer on the latest update page.
// @homepageURL https://sleazyfork.org/en/scripts/588436-reset-filters
// @supportURL  https://github.com/Edexaal/scripts/issues
// @require     https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/utility.js
// @require     https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/metadata.js
// @require     https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/lu-utility.js
// @require     https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/css.js
// ==/UserScript==
(async () => {
  const SELECTOR = {
    INCLUDE_TAGS: "#filter-block_tags",
    EXCLUDE_TAGS: "#filter-block_tags_exclude",
    PREFIXES: "#filter-block_prefixes div.filter-block div.filter-block_content",
    RESET_PREFIX_BTNS: "#filter-block_prefixes div.filter-block div.filter-block_content button"
  };
  const GAME_PREFIXES = [
    {name: "ed-engine_prefixes", codes: Object.values(TAG_CODE.game.engine)},
    {name: "ed-other_prefixes", codes: Object.values(TAG_CODE.game.other)},
    {name: "ed-status_prefixes", codes: Object.values(TAG_CODE.game.status)}
  ];
  const ANIM_PREFIXES = [
    {name: "ed-animation_prefixes", codes: Object.values(TAG_CODE.animation.animation)},
    {name: "ed-other_prefixes", codes: [GLOBAL_TAG_CODE.other.collection]}
  ];
  const ASSET_PREFIXES = [
    {name: "ed-assets_prefixes", codes: Object.values(TAG_CODE.asset.assets)},
    {name: "ed-other_prefixes", codes: [GLOBAL_TAG_CODE.other.collection]}
  ];
  const COMIC_PREFIXES = [
    {name: "ed-other_prefixes", codes: Object.values(TAG_CODE.comic.other)},
    {name: "ed-status_prefixes", codes: Object.values(TAG_CODE.comic.status)}
  ];
  Edexal.addCSS(`
  .ed-filter_btn {
    display: block;
    width: 100%;
    height: 35px;
    margin: 10px auto 5px auto;
    font-size: 0.875em;
    font-weight: bold;
    ${EdexalCSS.btnRules()}
}`);

  function addButton(containerEl, newBtnId, btnEvent) {
    const newBtn = Edexal.newEl({element: "button", class: ["ed-filter_btn"], id: newBtnId, text: "Reset"});
    Edexal.on(newBtn, 'click', btnEvent);
    containerEl.append(newBtn);
  }
  
  function getCurCategory() {
    let curCatLoc = location.href.match(/cat=(\w+)/);
    return curCatLoc ? curCatLoc[1] : 'games';
  }
  
  function addPrefixBtns(prefixDataArr) {
    const prefixContainers = Edexal.$$(SELECTOR.PREFIXES);
    prefixContainers.forEach((node, i) => {
      addButton(node, prefixDataArr[i].name, () => prefixResetEvent(new Set(prefixDataArr[i].codes)));
    });
  }
  
  function prefixBtnsExist(prefixArr) {
    const resetBtns = Edexal.$$(SELECTOR.RESET_PREFIX_BTNS);
    return resetBtns.length === prefixArr.length;
  }
  
  function initPrefixBtns() {
    let prefixArr;
    const categoryPage = getCurCategory();
    switch (categoryPage) {
      case "games":
        prefixArr = GAME_PREFIXES;
        break;
      case "comics":
        prefixArr = COMIC_PREFIXES;
        break;
      case "animations":
        prefixArr = ANIM_PREFIXES;
        break;
      case "assets":
        prefixArr = ASSET_PREFIXES;
        break;
      default:
        return;
    }
    if (prefixBtnsExist(prefixArr)) {
      return;
    }
    addPrefixBtns(prefixArr);
  }

  function initButtons() {
    addButton(Edexal.$(SELECTOR.INCLUDE_TAGS), 'ed-include_tags', () => tagResetEvent(/\/tags=(\d,?)+/, ''));
    addButton(Edexal.$(SELECTOR.EXCLUDE_TAGS), 'ed-exclude_tags', () => tagResetEvent(/\/notags=(\d,?)+/, ''));
    setTimeout(initPrefixBtns, 500);
  }

  function getNewPrefixURL(prefixSet, url, isPrefix) {
    const prefixPhrase = isPrefix ? "prefixes" : "noprefixes";
    const prefixRegex = new RegExp(`/${prefixPhrase}=\\d+(?:,\\d+)*`);
    const urlMatches = url.match(prefixRegex);
    const urlPrefix = urlMatches[0];
    const splitPrefix = urlPrefix.split('=');
    const urlPrefixNumbers = splitPrefix[1].split(',');
    const prefixesToKeep = [];
    for (const prefixNumber of urlPrefixNumbers) {
      if (!prefixSet.has(prefixNumber)) {
        prefixesToKeep.push(prefixNumber);
      }
    }
    if (!prefixesToKeep.length) {
      return url.replace(prefixRegex, '');
    }
    return url.replace(prefixRegex, `/${prefixPhrase}=${prefixesToKeep.join(',')}`);
  }

  function prefixResetEvent(prefixSet) {
    let url = location.href;
    if (url.includes("/prefixes=")) {
      url = getNewPrefixURL(prefixSet, url, true);
    }
    if (url.includes("/noprefixes=")) {
      url = getNewPrefixURL(prefixSet, url);
    }
    if (url !== location.href) {
      goToURL(url);
    }
  }

  function tagResetEvent(regex) {
    const newURL = location.href.replace(regex, '');
    goToURL(newURL);
  }

  function goToURL(url) {
    location.replace(url);
  }
  
  function observePageChanges() {
    const luObserver = new LUPageObserver();
    luObserver.observe(initPrefixBtns);
  }

  function run() {
    observePageChanges();
    initButtons();
  }

  run();
})();