Auto-remove notifications on fave

This automatically removes submission notifications, when faving a submission.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto-remove notifications on fave
// @namespace    https://github.com/f1r3w4rr10r/fa-utils
// @version      1.0.0
// @description  This automatically removes submission notifications, when faving a submission.
// @author       f1r3w4rr10r
// @match        https://www.furaffinity.net/view/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @license      MIT
// @grant        none
// ==/UserScript==

(async function () {
  "use strict";

  const favLinks = Array.from(
    document.querySelectorAll(
      '.favorite-nav > [href^="/fav/"], .fav > [href^="/fav/"]',
    ),
  );

  for (const favLink of favLinks) {
    if (!(favLink instanceof HTMLAnchorElement))
      throw new Error("'favLink' was not an instance of 'HTMLAnchorElement'.");

    favLink.addEventListener("click", async (event) => {
      event.preventDefault();

      const href = favLink.href;
      const urlMatch = href.match(/\/fav\/(\d+)\//);
      if (!urlMatch) {
        console.error("The fav URL did not match.", href);
        throw new Error("The fav URL did not match.");
      }

      const submissionId = urlMatch[1];
      if (!submissionId) throw new Error("Could not extract a submission ID.");

      const result = await fetch("/msg/submissions/old@24/", {
        method: "POST",
        body: new URLSearchParams({
          "messagecenter-action": "remove_checked",
          "submissions[]": submissionId,
        }),
        redirect: "manual",
      });

      if (result.type !== "opaqueredirect") {
        console.error("Could not remove the submission notification.", result);
        throw new Error("Could not remove the submission notification.");
      }

      window.location.assign(href);
    });
  }
})();