[MAL] Additional features

Adds image and torrent search to anime / manga entries.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name           [MAL] Additional features
// @description    Adds image and torrent search to anime / manga entries.
// @version        1.2.0
// @author         MetalTxus

// @match          https://myanimelist.net/anime*
// @match          https://myanimelist.net/manga*

// @icon           https://cdn.myanimelist.net/images/favicon.ico
// @namespace      https://greasyfork.org/users/8682
// ==/UserScript==

/* globals jQuery */

(() => {
  'use strict';

  const MediaType = {
    Anime: { id: '1_2', label: 'anime' },
    Manga: { id: '3_1', label: 'manga'}
  };

  const addLinksToSearch = () => {
    jQuery('#contentWrapper h1.title-name, #contentWrapper h1 span[itemprop="name"]').each(function (i, element) {
      element = jQuery(element);

      const mediaType = location.href.indexOf('https://myanimelist.net/anime') > -1 ? MediaType.Anime : MediaType.Manga;

      const isMultiLine = element.html().includes('<br>');
      const title = isMultiLine ? element.html().split('<br>')[0] : element.text();

      const searchWrapper = jQuery(`<div class="custom-search-wrapper"></div>`);

      const picturesAnchor = jQuery(`<a href="https://www.google.es/search?tbm=isch&q=${encodeURI(title)} ${mediaType.label}"></a>`);
      const picturesIcon = jQuery(`<i class="fa fa-picture-o" title="Search for pictures"></i>`);
      appendSearchAnchor(searchWrapper, picturesAnchor, picturesIcon);

      const torrentSearch = `https://nyaa.si/?f=0&s=seeders&o=desc&c=${mediaType.id}&q=${encodeURI(title)}`;
      const torrentAnchor = jQuery(`<a href="${torrentSearch}"></a>`);
      const torrentIcon = jQuery(`<i class="fa-solid fa-magnet" title="Search for torrents"></i>`);
      appendSearchAnchor(searchWrapper, torrentAnchor, torrentIcon);

      const hsAnchor = jQuery(`<a href="${torrentSearch} SubsPlease 720"></a>`);
      const hsIcon = jQuery(`<i class="fa-solid fa-s" title="Search for SubsPlease torrents"></i>`);
      appendSearchAnchor(searchWrapper, hsAnchor, hsIcon);

      isMultiLine ? element.find('br').before(searchWrapper) : element.append(searchWrapper);
    });
  }

  const appendSearchAnchor = (container, anchor, icon) => {
    anchor.append(icon);
    container.append(' ', anchor);
  }

  const appendStyles = () => {
    jQuery(`
      <style>
       .custom-search-wrapper {
         display: inline-block;
         font-size: 0;
         padding: 0 4px;
       }

       .custom-search-wrapper a {
         box-sizing: border-box;
         display: inline-block;
         font-size: 16px;
         text-align: center;
         text-decoration: none;
         transition: .3s;
         width: 24px;
       }

       .custom-search-wrapper a:hover {
         color: rgba(80, 116, 200, .9);
       }
      </style>
    `).appendTo('head');
  }

  const initialize = () => {
    appendStyles();
    addLinksToSearch();
  }

  initialize();
})();