Anime 2 MAL

Adds a Button that fetches the MAL(My Anime List) link for anime on Anime-Watching-Websites such as Aniwatch.to (Zoro.to)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Anime 2 MAL
// @namespace    https://github.com/longkidkoolstar
// @version      1.0.1
// @author       longkidkoolstar
// @description  Adds a Button that fetches the MAL(My Anime List) link for anime on Anime-Watching-Websites such as Aniwatch.to (Zoro.to)
// @match        https://aniwatch.to/*
// @grant        GM_xmlhttpRequest
// @license      CC BY-NC-ND 4.0
// @icon         https://repository-images.githubusercontent.com/62559339/bef33a00-628e-11e9-8c12-e36b0cb6c734
// ==/UserScript==

(function() {
    'use strict';


  // Find the anime name using DOM manipulation
    const animeNameElement = document.querySelector('h2.film-name.dynamic-name');
    if (animeNameElement) {
        const animeName = animeNameElement.textContent.trim();
        console.log('Anime Name:', animeName);

        // Construct the Jikan API request
        const malApiUrl = `https://api.jikan.moe/v4/anime?q=${encodeURIComponent(animeName)}`;
        console.log('API URL:', malApiUrl);

        GM_xmlhttpRequest({
            method: 'GET',
            url: malApiUrl,
            onload: function(response) {
                const responseData = JSON.parse(response.responseText);
                console.log('API Response:', responseData);

                if (responseData.data && responseData.data.length > 0) {
                    const malAnimeLink = responseData.data[0].url;
                    console.log(`MAL Link for "${animeName}": ${malAnimeLink}`);

                    // Create a new button and add it to the DOM
                    const newButton = document.createElement('a');
                    newButton.setAttribute('class', 'btn btn-radius btn-primary ml-2');
                    newButton.textContent = 'Open In MAL';

                    // Add an event listener to open the MAL link when clicked
                    newButton.addEventListener('click', function() {
                        window.open(malAnimeLink, '_blank');
                    });

                    // Find the existing button and its parent element
                    const existingButton = document.querySelector('.dr-fav a.btn-light');
                    const parentElement = existingButton.parentElement;

                    // Insert the new button next to the existing button
                    parentElement.insertBefore(newButton, existingButton.nextSibling);
                } else {
                    console.log(`Anime "${animeName}" not found on MAL.`);
                }
            },
            onerror: function(error) {
                console.error('Error fetching data from MAL API:', error);
            }
        });
    } else {
        console.log('Anime name element not found.');
    }
})();