Greasy Fork

Find on Nyaa

Will open nyaa on a new window and search for the anime title

目前为 2022-10-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         Find on Nyaa
// @namespace    MALFindNya
// @version      2.3
// @description  Will open nyaa on a new window and search for the anime title
// @author       Samu
// @match        https://myanimelist.net/anime/*
// @match        https://myanimelist.net/anime.php?id=*
// @match        https://anilist.co/*
// @match        https://kitsu.io/*
// @match        https://www.anime-planet.com/*
// @grant        none
// ==/UserScript==

/**
  icon source
  https://icons8.com/icon/82838/external-link
**/

//** SETTINGS **//
let sortBy = "seeders";                       // "comments", "size", "id", "seeders", "leechers", or "downloads"
let filter = "0";                             // 0 = No Filter | 1 = No remakes | 2 = Trusted Only
let category = "AnimeEnglishTranslated";      // Check values below
let favoriteGroups = ["Judas", "Erai-raws"];  // List is ordered, favorite should be first
let searchPrefix = "";
let searchSufix = "";
//** END OF SETTINGS **//

const cat = {
  Allcategories: "0_0",
  Anime: "1_0",
  AnimeMusicVideo: "1_1",
  AnimeEnglishTranslated: "1_2",
  AnimeNonEnglishTranslated: "1_3",
  Raw: "1_4",
  Audio: "2_0",
  Lossless: "2_1",
  Lossy: "2_2",
  Literature: "3_0",
  LiteratureEnglishTranslated: "3_1",
  LiteratureNonEnglishTranslated: "3_2",
  LiteratureRaw: "3_3",
  LiveAction: "4_0",
  LiveActionEnglishTranslated: "4_1",
  IdolPromotionalVideo: "4_2",
  LiveActionNonEnglishTranslated: "4_3",
  LiveActionRaw: "4_4",
  Pictures: "5_0",
  Graphics: "5_1",
  Photos: "5_2",
  Software: "6_0",
  Applications: "6_1",
  Games: "6_2",
};

const buttonId = "FindOnNyaaButtonId";

(function() {
  'use strict';

  let pushState = history.pushState;
  let replaceState = history.replaceState;

  history.pushState = function() {
      pushState.apply(history, arguments);
      window.dispatchEvent(new Event('pushstate'));
      window.dispatchEvent(new Event('locationchange'));
  };

  history.replaceState = function() {
      replaceState.apply(history, arguments);
      window.dispatchEvent(new Event('replacestate'));
      window.dispatchEvent(new Event('locationchange'));
  };

  window.addEventListener('popstate', function() {
      window.dispatchEvent(new Event('locationchange'))
  });

  let host = window.location.host;
  let path = window.location.pathname;

  window.addEventListener('locationchange', function() {
    path = window.location.pathname;
    lookForTitleElement(host, path);
  });

  lookForTitleElement(host, path);

})();

function lookForTitleElement(host, path) {
  if (host === "myanimelist.net") {
    waitForElm('.title-name').then((elm) => init(elm));
  } else if (host === "anilist.co" && path.startsWith("/anime/")) {
    waitForElm('.page-content .header .content > h1').then((elm) => init(elm, true));
  } else if (host === "kitsu.io" && path.startsWith("/anime/")) {
    waitForElm('.media--title h3').then((elm) => init(elm));
  } else if (host === "www.anime-planet.com" && path.startsWith("/anime/")) {
    waitForElm('#siteContainer h1').then((elm) => init(elm, true));
  } else {
    let nyaaButton = document.getElementById(buttonId);
    if (nyaaButton != null) {
      nyaaButton.parentElement.removeChild(nyaaButton);
    }
  }
}

function init(element, dark = false) {
  let url = createUrl(element);
  let nyaaButton = document.getElementById(buttonId);
  if (nyaaButton == null) {
    nyaaButton = createButton(url, dark);
    element.appendChild(nyaaButton);
  } else {
    nyaaButton.setAttribute("href", url);
  }
}

function createButton(url, dark) {
  let button = document.createElement("a");
  button.id = buttonId;
  button.setAttribute("href", url);
  button.setAttribute("target", "_blank");
  button.setAttribute("rel", "noopener");
  button.setAttribute("title", "Search on Nyaa");
  button.setAttribute("style", "margin-left: 10px;height: 10px;width: 10px;display: inline-flex;transform: scale(1.8);vertical-align: top;margin-top: 7px;");
  button.innerHTML = `<svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M 5 3 C 3.9069372 3 3 3.9069372 3 5 L 3 19 C 3 20.093063 3.9069372 21 5 21 L 19 21 C 20.093063 21 21 20.093063 21 19 L 21 12 L 19 12 L 19 19 L 5 19 L 5 5 L 12 5 L 12 3 L 5 3 z M 14 3 L 14 5 L 17.585938 5 L 8.2929688 14.292969 L 9.7070312 15.707031 L 19 6.4140625 L 19 10 L 21 10 L 21 3 L 14 3 z"/></svg>`;
  if (dark) {
    button.querySelector("svg").setAttribute("fill", "#fff");
  }
  return button;
}

function createUrl(element) {
  let textFragment = ":~:text=" + favoriteGroups.join("&text=");
  let title = element.textContent.trim().replace(/[^a-zA-Z0-9]+/g, "+");
  let query = [searchPrefix, title.trim(), searchSufix].join(" ").trim();
  let queryURI = encodeURI(query);
  let baseUrl = "https://nyaa.si/";
  let path = `?f=${filter}&c=${cat[category]}&s=${sortBy}&o=desc&q=${queryURI}`;
  return baseUrl + path + "#" + textFragment;
}

function waitForElm(selector) {
  return new Promise(resolve => {
    if (document.querySelector(selector)) {
      return resolve(document.querySelector(selector));
    }

    const observer = new MutationObserver(mutations => {
      if (document.querySelector(selector)) {
        resolve(document.querySelector(selector));
        observer.disconnect();
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true
    });
  });
}