DTF return like

Скрипт для замены иконки лайка (сердечка) на стрелку

目前為 2022-12-15 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DTF return like
// @version      0.1
// @description  Скрипт для замены иконки лайка (сердечка) на стрелку
// @author       geuarg1y
// @match        *://dtf.ru/*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/998190
// ==/UserScript==

(function () {
    injectStyles();
    void swapIcons();
})();

async function swapIcons() {
    const like = await getDomElementAsync('#v_like'); // Ждем, когда появится спрайт с svg иконками на странице и получаем икоку лайка
    const likeCopy = createIconCopy('v_dislike', 'v_like'); // Делаем копию дизлайка, но с id лайка
    like.replaceWith(likeCopy); // Заменяем сердечко на иконку дизлайка (стрелку)

    const likeActive = document.querySelector('#v_like_active');
    const likeActiveCopy = createIconCopy('v_dislike_active', 'v_like_active');
    likeActive.replaceWith(likeActiveCopy);
}

function getDomElementAsync(selector, timerLimit = 10000) {
    return new Promise((resolve, reject) => {
        try {
            setTimeout(
                () => reject(`Время ожидания DOM элемента истекло (${timerLimit / 1000}s)`),
                timerLimit
            );

            let timerId;

            const tick = () => {
                const element = document.querySelector(selector);

                if (element) {
                    clearTimeout(timerId);
                    resolve(element);
                } else {
                    timerId = setTimeout(tick, 100);
                }
            };

            tick();
        } catch (e) {
            reject(e);
        }
    });
}

function createIconCopy(fromIconId, toIconId) {
    const source = document.querySelector(`#${fromIconId}`);
    const copy = source.cloneNode(true);

    copy.setAttribute('id', toIconId);

    return copy;
}

function injectStyles() {
    const styles = `
        .content-footer__item:first-child,
        .comment .like-button.like-button--action-like {
            order: 2;
            margin-left: auto;
            margin-right: 7px !important;
        }
        
        .content-footer__item:last-child,
        .comment .like-button.like-button--action-dislike {
            order: 3;
            margin-left: 0;
        }
        
        .comment .like-button--action-like + .comment__action {
            order: 1;
        }
        
        .comment__inline-action {
            order: 4 !important;
            width: 100%;
        }
        
        .like-button.like-button--action-like {
            --like-color-text-hover: #2ea83a;
            --like-color-background-hover: #2ea83a;
            --like-color-active: #2ea83a;
        }
        
        .like-button.like-button--action-dislike {
            --like-color-text-hover: #cf4c59;
            --like-color-background-hover: #cf4c59;
            --like-color-active: #cf4c59;
        }
        
        .like-button--action-like .like-button__icon,
        .like-button--action-dislike .like-button__icon {
            visibility: visible !important;
        }
        
        #v_dislike_active path {
            fill: #cf4c59;
        }
        
        #v_like_active path {
            fill: #2ea83a;
        }
        
        .like-button__lottie {
            display: none;
        }
        
        .like-button--action-like .like-button__icon {
            transform: rotate(180deg);
        }
    `;

    document.head.insertAdjacentHTML("beforeend", `<style type="text/css" id="dtfChangeIconStyles">${styles}</style>`)
}