ShikiLinker

Редирект-кнопка для Шикимори, которая перенаправляет на Anime365

2022-02-05 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name            ShikiLinker
// @description     Редирект-кнопка для Шикимори, которая перенаправляет на Anime365
// @description:en  Redirect button for Shikimori that redirects to Anime 365
// @namespace       https://shikimori.one/animes
// @include         /^https?://shikimori\.o(?:ne|rg)/(.*)$/
// @connect         smotret-anime.online
// @grant           GM_xmlhttpRequest
// @compatible      chrome
// @icon            https://www.google.com/s2/favicons?domain=shikimori.one
// @author          Jogeer
// @license         MIT
// @version         2.0.0
// @contributionURL https://paypal.me/Jogeer
// @contributionAmount 5
// ==/UserScript==
const DEBUG = false;
const PAGEURL = new RegExp(/^https?:\/\/shikimori\.o(?:ne|rg)\/animes\/[A-z]?(\d*)-(.*)$/);
const A365URL = 'https://smotret-anime.online/';
const A365API = `${A365URL}api/`;
const SHIKIAPI = 'https://shikimori.one/api/';
const NYAASI = 'https://nyaa.si/?';
const DISTRIB = 'Erai-raws';
const PARENSSTYLES = "display:flex;flex-direction:row;flex-wrap:wrap;align-content:center;justify-content:center;align-items:center;margin-top:10px";
const CHILDSTYLES = "flex:1 1 auto;text-align:center;padding:5px;background:#18181b;color:white;margin: 0 10px";
class ShikiLinker {
    static async MakeRequest(url) {
        return await new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                headers: { "Content-type": "application/json" },
                url: `${url}`,
                onload: async (data) => {
                    data = await JSON.parse(data.response);
                    DEBUG ? console.log(`${url}`, data) : null;
                    resolve(data);
                },
                onerror: async (data) => {
                    DEBUG ? console.error(`${url}`, data) : null;
                    reject(null);
                }
            });
        });
    }
    static ParseUserData() {
        return JSON.parse(document.querySelector('body').getAttribute('data-user'));
    }
    static BuildElement(element) {
        return `<a class=\"link-button\" target=\"_blank\" href=\"${element.url}\" style=\"${CHILDSTYLES}\">${element.title}</a>`;
    }
    static async AddDomParentContainer(toSelector) {
        let documentDom = document.querySelector(toSelector);
        documentDom.insertAdjacentHTML('beforeend', `<div class=\"watch-online\" id=\"shikilinker-inject\" style=\"${PARENSSTYLES}\"></div>`);
    }
    static async AddElement(element) {
        let documentDom = document.querySelector('#shikilinker-inject');
        DEBUG ? console.log('Add:', element) : null;
        documentDom.insertAdjacentHTML('beforeend', ShikiLinker.BuildElement(element));
    }
    static async HaveNonWatchedEpisode(a365Data, shikiData) {
        DEBUG ? console.log("hadsadas:", a365Data, shikiData) : null;
        if (!shikiData || !shikiData.episodes) {
            shikiData = JSON.parse('{"status": "none", "episodes": 0}');
        }
        if (["completed", "dropped"].includes(shikiData.status) || shikiData.episodes >= a365Data.numberOfEpisodes) {
            return false;
        }
        return true;
    }
    static async Execute() {
        await ShikiLinker.AddDomParentContainer('.c-info-right');
        let matches = PAGEURL.exec(window.location.href);
        let userData = await JSON.parse(document.querySelector('body').getAttribute('data-user'));
        DEBUG ? console.log('PR done:', matches, userData) : null;
        let a365Data = await ShikiLinker.MakeRequest(`${A365API}series?myAnimeListId=${matches[1]}`);
        let shikiData = await ShikiLinker.MakeRequest(`${SHIKIAPI}v2/user_rates?user_id=${userData.id}&target_id=${matches[1]}&target_type=Anime`);
        DEBUG ? console.log('Req done:', a365Data, shikiData) : null;
        await ShikiLinker.AddElement({ title: 'A365', url: a365Data.data[0].url });
        if (await ShikiLinker.HaveNonWatchedEpisode(a365Data.data[0], shikiData[0])) {
            DEBUG ? console.log('Have nonwatched eps') : null;
            try {
                await ShikiLinker.AddElement({ title: `${shikiData[0].episodes + 1}`, url: `${A365URL}episodes/${a365Data.data[0].episodes[shikiData[0].episodes].id}` });
            }
            catch (error) {
                null;
            }
        }
        await ShikiLinker.AddElement({ title: 'Nyaa', url: `${NYAASI}u=${DISTRIB}&q=${document.querySelector('meta[property="og:title"]').content}` });
    }
}
function ready(func) {
    //var document = document.documentElement;
    document.addEventListener('turbolinks:load', func);
    if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
        func();
    }
    else {
        document.addEventListener('DOMContentLoaded', func);
    }
}
ready(ShikiLinker.Execute);