AniList - Activity Assistant

Script that speeds up Activity Making

目前為 2022-04-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AniList - Activity Assistant
// @namespace    https://www.youtube.com/c/NurarihyonMaou/
// @version      1.0
// @description  Script that speeds up Activity Making
// @author       NurarihyonMaou
// @match        https://anilist.co/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=anilist.co
// @require      https://code.jquery.com/jquery-3.6.0.js
// @require      https://code.jquery.com/ui/1.13.1/jquery-ui.js
// @resource     IMPORTED_CSS https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css
// @grant        GM_getResourceText
// @grant        GM_addStyle
// ==/UserScript==

const $ = window.jQuery;
const x_csrf_token = $("head script:contains('window.al_token')").text().split(/[“"”]+/g)[1];

const imported_CSS = GM_getResourceText("IMPORTED_CSS");
const category_Style = '.ui-autocomplete-category { font-weight: bold; padding: .2em .4em; margin: .8em 0 .2em;line-height: 1.5;}';

GM_addStyle(imported_CSS);
GM_addStyle(category_Style);

let textArea;
let autoComplete = [], selectedEntries = [];
let allWords, currentWord, lastSearched, terms;

(function init() {
    textArea = document.getElementsByClassName("el-textarea__inner");

    if (textArea.length > 0) {

        $(textArea).after('<input type="hidden" id="testArea">');

        function searchQuery(word) {
            var query = `
            query($search:String,$isAdult:Boolean){anime:Page(perPage:8){pageInfo{total}results:media(type:ANIME,isAdult:$isAdult,search:$search){id title{userPreferred}coverImage{medium}type format bannerImage isLicensed startDate{year}}}manga:Page(perPage:8){pageInfo{total}results:media(type:MANGA,isAdult:$isAdult,search:$search){id title{userPreferred}coverImage{medium}type format bannerImage isLicensed startDate{year}}}characters:Page(perPage:8){pageInfo{total}results:characters(search:$search){id name{userPreferred}image{medium}}}staff:Page(perPage:8){pageInfo{total}results:staff(search:$search){id primaryOccupations name{userPreferred}image{medium}}}studios:Page(perPage:13){pageInfo{total}results:studios(search:$search){id name}}users:Page(perPage:8){results:users(search:$search){id name avatar{medium}}}}
            `;

            var variables = {
                search: word
            };

            var url = 'https://anilist.co/graphql',
                options = {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        "x-csrf-token": x_csrf_token
                    },
                    body: JSON.stringify({
                        query: query,
                        variables: variables
                    })
                };

            fetch(url, options).then(handleResponse)
                .then(handleData)
                .catch(handleError);

            function handleResponse(response) {
                return response.json().then(function (json) {
                    return response.ok ? json : Promise.reject(json);
                });
            }

            function handleData(data) {

                autoComplete = [];

                $.each(data.data.anime.results, function (index, element) {
                    autoComplete.push({ 'value': 'https://anilist.co/anime/' + element.id, 'label': element.title.userPreferred, 'category': 'Anime' });
                });

                $.each(data.data.manga.results, function (index, element) {
                    autoComplete.push({ 'value': 'https://anilist.co/manga/' + element.id, 'label': element.title.userPreferred, 'category': 'Manga' });
                });

                $.each(data.data.characters.results, function (index, element) {
                    autoComplete.push({ 'value': '[' + element.name.userPreferred + '](https://anilist.co/character/' + element.id + ")", 'label': element.name.userPreferred, 'category': 'Characters' });
                });

                $.each(data.data.staff.results, function (index, element) {
                    autoComplete.push({ 'value': '[' + element.name.userPreferred + '](https://anilist.co/staff/' + element.id + ")", 'label': element.name.userPreferred, 'category': 'Staff' });
                });

                $.each(data.data.users.results, function (index, element) {
                    autoComplete.push({ 'value': '@' + element.name, 'label': element.name, 'category': 'Users' });
                });

                $.each(data.data.studios.results, function (index, element) {
                    autoComplete.push({ 'value': '[' + element.name + '](https://anilist.co/studio/' + element.id + ")", 'label': element.name, 'category': 'Studios' });
                });

            }

            function handleError(error) {
                alert('Error, check console');
                console.error(error);
            }
        }

        $.widget("custom.catcomplete", $.ui.autocomplete, {
            _create: function () {
                this._super();
                this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
            },
            _renderMenu: function (ul, items) {
                var that = this,
                    currentCategory = "";
                $.each(items, function (index, item) {
                    var li;
                    if (item.category != currentCategory) {
                        ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                        currentCategory = item.category;
                    }
                    li = that._renderItemData(ul, item);
                    if (item.category) {
                        li.attr("aria-label", item.category + " : " + item.label);
                    }
                });
            }
        });

        $(textArea).on('input', function () {
            allWords = $(this).val().split(' ');
            currentWord = allWords[allWords.length - 1];
            if (currentWord.length > 2 && lastSearched != currentWord) {
                searchQuery(currentWord);
                lastSearched = currentWord;
            }
        });

        function split(val) {
            return val.split(/ \s*/);
        }

        function extractLast(term) {
            return split(term).pop();
        }

        $(textArea).catcomplete({
            delay: 500,
            minLength: 3,
            source: function (request, response) {
                response($.ui.autocomplete.filter(
                    autoComplete, extractLast(request.term)));
            },
            focus: function () {
                return false;
            },
            select: function (event, ui) {
                terms = split(this.value);
                terms.pop();
                terms.push(ui.item.value);
                terms.push("");
                this.value = terms.join(" ");
                selectedEntries.push(ui.item.label);
                autoComplete = [];
                return false;
            }
        });

    } else {
        setTimeout(init, 0);
    }
})();