AJR log Anilist

Crea un botón para guardar logs para AJR

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AJR log Anilist
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Crea un botón para guardar logs para AJR
// @author       alexay7
// @license      GNU GPLv3
// @match        *://anilist.co/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=anilist.co
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    function findDifferenceInText(text) {
        // Use regular expressions to extract numbers from the text

        const episodes = text.match(/episode.*?of/g);
        const numbers = episodes[0].match(/\d+/g);

        // Check if there are at least two numbers in the text
        if (numbers && numbers.length >= 2) {
            // Calculate the difference between the two numbers and add one
            const num1 = parseInt(numbers[0]);
            const num2 = parseInt(numbers[1]);
            const difference = num2 - num1 + 1;

            return {diff:difference,start:num1,end:num2};
        } else {
            // If there are not enough numbers in the text, return an error message or handle it as needed
            return {diff:1,start:numbers[0]};
        }
    }

    function addButtonAndCopyText(element) {
        let isWatched=false;
        const [isLog] = element.parentElement.getElementsByClassName("list");
        if(isLog){
            isWatched=isLog.getElementsByClassName("details")[0].getElementsByClassName("status")[0].textContent.includes("Watched");
        }
        if (isWatched&&!element.querySelector('.copy-button')) {
            const button = document.createElement('span');
            button.innerText = 'Copiar log';
            button.className = 'copy-button';
            button.style.background = 'transparent';
            button.style.border="none";
            button.style.color="var(--color-text-lighter)";
            button.style.fontWeight=800;
            button.style.fontSize="1.1rem";
            button.style.cursor="pointer";
            button.style.fontFamily="Overpass,-apple-system,BlinkMacSystemFont,Segoe UI,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif";
            button.style.marginLeft="10px";
            element.appendChild(button);

            button.addEventListener('click', () => {
                const parentEl = element.parentElement;
                const details = parentEl.getElementsByClassName("list")[0].getElementsByClassName("details")[0].getElementsByClassName("status")[0];
                const episodes = findDifferenceInText(details.textContent);
                const name = details.getElementsByClassName("title")[0].textContent;
                console.log(details.textContent)
                if (!episodes.end){
                    GM_setClipboard('.log anime '+episodes.diff+' '+name.trim()+' '+episodes.start, 'text');
                }else{
                    GM_setClipboard('.log anime '+episodes.diff+' '+name.trim()+' '+episodes.start+'-'+episodes.end, 'text');
                }
            });
        }
    }

    // Function to handle mutations
    function handleMutations(mutationsList) {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                // Check if the mutation added elements with class "time"
                const timeElements = document.querySelectorAll('.time');
                timeElements.forEach(addButtonAndCopyText);
            }
        }
    }

    // Create a MutationObserver to watch for changes in the DOM
    const observer = new MutationObserver(handleMutations);

    // Start observing the document
    observer.observe(document.body, { childList: true, subtree: true });

})();