BetterAnimeWorld

Migliora AnimeWorld

目前为 2021-06-30 提交的版本。查看 最新版本

// ==UserScript==
// @name         BetterAnimeWorld
// @namespace    https://pizidavi.altervista.org/
// @description  Migliora AnimeWorld
// @author       pizidavi
// @version      1.6.1
// @copyright    2021, PIZIDAVI
// @license      MIT
// @require      https://cdn.jsdelivr.net/gh/soufianesakhi/node-creation-observer-js@edabdee1caaee6af701333a527a0afd95240aa3b/release/node-creation-observer-latest.min.js
// @match        https://www.animeworld.tv/*
// @run-at       document-body
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    const PAGE_TITLE = document.title;

    NodeCreationObserver.onCreation('#sign > div.signed', function (element) {
        addStyle('#header .head #sign .signed, #header .head #sign .signin { width: unset !important; }');
        element.querySelector('div > ul > li:nth-child(3) > a').href += '?folder=1&sort=2';

        element.querySelector('div > ul > li:nth-child(4) > a').textContent = 'Notifiche';
        element.querySelector('div > ul > li:nth-child(4) > a').href = '/notifications';

        setNotify();
        setInterval(function() {
            getNotify();
        }, 2*60*1000); // 2 minuti
    });

    NodeCreationObserver.onCreation('div.menu-profile', function (element) {
        element.querySelector('a.pulsante-profilo-tabs:nth-child(2)').href += '?folder=1&sort=2';
        if(location.pathname.includes('/watchlist')) {
            document.querySelector('.cover-profilo-aw').style.display = 'none';
        }
    });

    if(location.pathname.includes('/play/')) {
        NodeCreationObserver.onCreation('#player .cover, div.server ul a, #controls > div.prevnext', function (element) {
            element.addEventListener('click', function() {
                window.scrollTo(0, 133);
                const r = document.querySelector('#controls .resize');
                if(r.textContent.includes('Espandi')) r.click();

                setTimeout(function() {
                    document.querySelector('#controls .light').click();
                    document.cookie = 'expandedPlayer=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
                }, 700);
            });
        });
    }

    if(location.pathname.includes('/notifications')) {
        NodeCreationObserver.onCreation('#delete-all', function (element) {
            const parentDiv = element.parentElement;
            const button = element.cloneNode();
            button.id = 'delete-read-all';
            button.innerHTML = '<i class="fas fa-trash"></i> Cancella tutte le notifiche "lette"';
            button.onclick = function() {
                Swal.fire({
                    title: 'Vuoi davvero cancellare tutte le notifiche lette?',
                    icon: 'question',
                    showCancelButton: true,
                    confirmButtonColor: 'rgb(165, 220, 134)',
                    cancelButtonColor: 'rgb(221, 51, 51)',
                    confirmButtonText: 'Si',
                    cancelButtonText: 'No',
                }).then((result) => {
                    if (result.value) {
                        document.querySelectorAll('.profile-page .row .widget:nth-child(2) li.is-notification-read').forEach(function(e, i) {
                            e.querySelector('.actions i.delete-clickable-icon').click();
                        });
                    }
                });
            };
            parentDiv.appendChild(button);
            element.remove();
        });
    }

    NodeCreationObserver.onCreation('#account-delete', function (element) {
        element.parentElement.remove();
    });


    // ------- Function -------
    function getNotify() {
        request({
            url: '/request-serie',
            success: function(data) {
                const html = document.createElement('html'); html.innerHTML = data;
                document.querySelector('#notification > ul').innerHTML = html.querySelector('#notification > ul').innerHTML;
                setNotify();

                document.querySelectorAll('#notification > ul > li[data-id]').forEach(function(li, index) {
                    const span = li.querySelector('.header-notification-read');
                    span.addEventListener('click', function(e) {
                        e.preventDefault(); e.stopPropagation();
                        request({
                            url: '/api/notifications/open/'+li.getAttribute('data-id'),
                            success: function(data) {
                                li.remove();
                                setNotify();
                            }
                        });
                    });
                });
            }
        });
    }

    function setNotify() {
        const notify = document.querySelector('#notification-menu div.number-notify > span');
        notify.textContent = document.querySelectorAll('#notification > ul > li[data-id]').length;
        document.title = (parseInt(notify.textContent) ? notify.textContent+' - ' : '') + PAGE_TITLE;
    }

    function request(options) {
        const onreadystatechange = function() {
            if (this.readyState === 4 && (this.status == 200 || this.status == 201)) {
                options.success(this.responseText);
            } else if (this.readyState === 4) {
                console.warn('%cBetterAnimeWorld%c - Errore nella richiesta', 'color:red;font-size:14px;', '');
            }
        };
        if (typeof GM_xmlhttpRequest != 'undefined') {
            options.onload = onreadystatechange;
            GM_xmlhttpRequest(options);
        } else {
            const xhttp = new XMLHttpRequest();
            xhttp.open(options.method || 'GET', options.url);
            xhttp.onreadystatechange = onreadystatechange;
            xhttp.send();
        }
    }

    function addStyle(CSS) {
        if (typeof GM_addStyle != 'undefined') {
            GM_addStyle(CSS);
        } else {
            const style = document.createElement('style');
            style.innerText = CSS;
            document.head.appendChild(style);
        }
    }

})();