Sanakan

Фризит анимированные аватарки на mangalib

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Sanakan
// @version     0.0.4
// @description Фризит анимированные аватарки на mangalib
// @author      abara
// @match       https://mangalib.me/*
// @match       https://ranobelib.me/*
// @icon        https://i.imgur.com/AlTa76l.jpg
// @namespace   https://greasyfork.org/users/209098
// @license     MIT
// ==/UserScript==

const freezeGif = function(img) {
    const canvas = document.createElement('canvas');
    const width = canvas.width = img.width;
    const height = canvas.height = img.height;
    canvas.getContext('2d').drawImage(img, 0, 0, width, height);
    img.src = canvas.toDataURL('image/gif');
}

const freezeGifAvatar = function(chatNode) {
    const avatarImg = chatNode.querySelector('.chat-item__avatar');
    // Guard against multiple calls same image
    if (avatarImg.classList.contains('sana-avfreezed')) return;
    if (!/.gif/i.test(avatarImg.src)) return;

    const loadAvatarInterval = setInterval(() => {
        // Check if image loaded
        if (avatarImg.complete && avatarImg.naturalHeight !== 0) {
            clearInterval(loadAvatarInterval);
            freezeGif(avatarImg);
            avatarImg.classList.add('sana-avfreezed');
        }
    }, 100);
}

const getChatInstance = function() {
    const chatWrap = _CHAT_INSTANCE.$children[0];
    return chatWrap.$children[1] ? chatWrap.$children[1] : chatWrap.$children[0];
}

if (typeof _CHAT_INSTANCE !== 'undefined') {
    const CHAT_INSTANCE = getChatInstance();

    const chatInitInterval = setInterval(() => {
        if (CHAT_INSTANCE._isMounted) {
            clearInterval(chatInitInterval);

            const chatItems = document.querySelector('.chat__items');
            chatItems.querySelectorAll('.chat-item').forEach(node => freezeGifAvatar(node));

            const chatObserver = new MutationObserver(mutationsList => {
                mutationsList.forEach(mutation =>
                    mutation.addedNodes.forEach(node => {
                        if (node.tagName == 'DIV' && node.classList.contains('chat-item')) 
                        freezeGifAvatar(node);
                    })
                )
            });

            chatObserver.observe(chatItems, { childList: true, subtree: false, attributes: false });
        }
    }, 50);
}