Chortowod / Shiki Profile Links Expansion

// ==UserScript==
// @name         Shiki Profile Links Expansion
// @author       Chortowod (https://openuserjs.org/users/Chortowod)
// @description  Добавляет новые ссылки в выпадающее меню и профиль (список ранобэ, история просмотра и др.)
// @namespace    http://shikimori.me/
// @version      1.2.2
// @match        *://shikimori.org/*
// @match        *://shikimori.one/*
// @match        *://shikimori.me/*
// @icon         https://www.google.com/s2/favicons?domain=shikimori.me
// @updateURL    https://openuserjs.org/meta/Chortowod/Shiki_Profile_Links_Expansion.meta.js
// @downloadURL  https://openuserjs.org/install/Chortowod/Shiki_Profile_Links_Expansion.user.js
// @require      https://gist.githubusercontent.com/Chortowod/814b010c68fc97e5f900df47bf79059c/raw/chtw_settings.js?v1
// @license      MIT
// @require      https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js
// @copyright    2023, Chortowod
// ==/UserScript==

let settings = new ChtwSettings('chtwPLE', '<a target="_blank" href="https://openuserjs.org/scripts/Chortowod/Shiki_Profile_Links_Expansion">Различные ссылки</a>');
let debug;

function initSettings() {
    settings.createOption('planned', 'Пункт "В планах"', false);
    settings.createOption('ranobe', 'Пункт "Ранобэ"');
    settings.createOption('history', 'Пункт "История"');
    settings.createOption('isDebug', 'Режим отладки');
    debug = settings.getOption('isDebug');
}

let currentSite = document.querySelector('.logo-container').href;
let myProfileLink = document.getElementsByClassName('submenu-triangle')[1].getAttribute("href");
let ranobeSelector = "/list/manga/kind/novel,light_novel?order=rate_updated";
let mangaSelector = "/list/manga/kind/!novel,!light_novel?mylist=watching&order=rate_updated";
let animePlannedSelector = "/list/anime/status/released,!latest?mylist=planned&order=aired_on";
let redirectToHistory = myProfileLink + "/history";
let redirectToRanobe = myProfileLink + ranobeSelector;
let redirectToManga = myProfileLink + mangaSelector;
let redirectToAnimePlanned = myProfileLink + animePlannedSelector;

function addToRanobe() {
    if (document.getElementById('ctwToRanobe') || !location.href.includes("/list/")) return;
    let mainAppend = document.querySelector('.head.misc > h1');
    if (!mainAppend) return;
    let whatAppend = document.querySelector('.head.misc > h1 > .misc');
    if (!whatAppend) return;
    let isAnotherProfile = document.querySelector('.submenu-triangle.icon-avatar > span');
    let currentProfileName = isAnotherProfile ? isAnotherProfile.innerText : document.querySelector('span.nickname').innerText;
    whatAppend = whatAppend.cloneNode(true);
    whatAppend.href = currentSite + currentProfileName + ranobeSelector;
    whatAppend.id = 'ctwToRanobe';
    whatAppend.title = "к ранобэ";
    whatAppend.textContent = "к ранобэ";
    whatAppend.style.display = 'inline-block';
    mainAppend.insertBefore(whatAppend, mainAppend.childNodes[3]);
    let toManga = document.querySelector('.head.misc > h1 > .misc[title="к манге"]');
    if (toManga) {
        toManga.href = currentSite + currentProfileName + mangaSelector;
        toManga.style.display = 'inline-block';
    }

    let toAnime = document.querySelector('.head.misc > h1 > .misc[title="к аниме"]');
    if (toAnime) {
        toAnime.style.display = 'inline-block';
    }

    if (location.href.includes("/list/manga/kind/novel,light_novel/")) {
        whatAppend.title = "к манге";
        whatAppend.textContent = "к манге";
        whatAppend.href = currentSite + currentProfileName + mangaSelector;
    }
    whatAppend.style.fontSize = window.getComputedStyle(document.querySelector('.head.misc > h1 > .misc')).fontSize;
}

function addRanobe() {
    if (document.getElementById('ctwRanobe') || !settings.getOption('ranobe')) return;
    document.querySelector('a.icon-manga_list').href = redirectToManga;
    let mainAppend = document.querySelector('.menu-dropdown.profile > .submenu');
    if (!mainAppend) return;
    let whatAppend = document.querySelector('a.icon-critiques');
    if (!whatAppend) return;
    whatAppend = whatAppend.cloneNode(true);
    whatAppend.href = redirectToRanobe;
    whatAppend.id = 'ctwRanobe';
    whatAppend.title = "Список ранобэ";
    whatAppend.textContent = "Список ранобэ";
    mainAppend.insertBefore(whatAppend, mainAppend.childNodes[4]);
}

function addAnimePlanned() {
    if (!settings.getOption('planned') || document.getElementById('ctwAnimePlanned')) return;
    let mainAppend = document.querySelector('.menu-dropdown.profile > .submenu');
    if (!mainAppend) return;
    let whatAppend = document.querySelector('a.icon-anime_list');
    if (!whatAppend) return;
    whatAppend = whatAppend.cloneNode(true);
    whatAppend.href = redirectToAnimePlanned;
    whatAppend.id = 'ctwAnimePlanned';
    whatAppend.title = "Аниме (в планах)";
    whatAppend.textContent = "Аниме (в планах)";
    mainAppend.insertBefore(whatAppend, mainAppend.childNodes[3]);
}

function addHistory() {
    if (!settings.getOption('history') || document.getElementById('ctwHistory')) return;
    let mainAppend = document.querySelector('.menu-dropdown.profile > .submenu');
    if (!mainAppend) return;
    let whatAppend = document.querySelector('a.icon-calendar').cloneNode(true);
    whatAppend.href = redirectToHistory;
    whatAppend.id = 'ctwHistory';
    whatAppend.title = "История";
    whatAppend.textContent = "История";
    mainAppend.insertBefore(whatAppend, mainAppend.childNodes[6]);
}

function ready(fn) {
    document.addEventListener('page:load', fn);
    document.addEventListener('turbolinks:load', fn);
    if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") fn();
    else document.addEventListener('DOMContentLoaded', fn);
}

ready(initSettings);
ready(addRanobe);
ready(addHistory);
ready(addToRanobe);
ready(addAnimePlanned);