Greasy Fork

Greasy Fork is available in English.

FSM 一键收藏 不喜欢

Add favorite and dislike buttons to FSM torrent details page

目前为 2025-03-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         FSM 一键收藏 不喜欢
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add favorite and dislike buttons to FSM torrent details page
// @author       You
// @match        https://fsm.name/Torrents/details*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function addButtons() {
        const sideBlk = document.querySelector('.side-blk');
        if (!sideBlk) {
            return;
        }

        const urlParams = new URLSearchParams(window.location.search);
        const tid = urlParams.get('tid');
        if (!tid) return;

        // 创建收藏按钮
        const favoriteDiv = document.createElement('div');
        const favoriteBtn = document.createElement('button');
        favoriteBtn.className = 'el-button el-button--info el-button--large is-plain is-circle side-btn el-tooltip__trigger el-tooltip__trigger';
        favoriteBtn.style.display = 'block';
        favoriteBtn.innerHTML = `<i class="el-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="m512 747.84 228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72zM313.6 924.48a70.4 70.4 0 0 1-102.144-74.24l37.888-220.928L88.96 472.96A70.4 70.4 0 0 1 128 352.896l221.76-32.256 99.2-200.96a70.4 70.4 0 0 1 126.208 0l99.2 200.96 221.824 32.256a70.4 70.4 0 0 1 39.04 120.064L774.72 629.376l37.888 220.928a70.4 70.4 0 0 1-102.144 74.24L512 820.096l-198.4 104.32z"></path></svg></i>`;
        favoriteBtn.addEventListener('click', () => {
            voteTorrent(tid, 'VALUE');
        });
        favoriteDiv.appendChild(favoriteBtn);

        // 创建不喜欢按钮
        const dislikeDiv = document.createElement('div');
        const dislikeBtn = document.createElement('button');
        dislikeBtn.className = 'el-button el-button--info el-button--large is-plain is-circle side-btn el-tooltip__trigger el-tooltip__trigger';
        dislikeBtn.style.display = 'block';
        dislikeBtn.innerHTML = `<i class="el-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="M160 256H96a32 32 0 0 1 0-64h256V95.936a32 32 0 0 1 32-32h256a32 32 0 0 1 32 32V192h256a32 32 0 1 1 0 64h-64v672a32 32 0 0 1-32 32H192a32 32 0 0 1-32-32zm448-64v-64H416v64zM224 896h576V256H224zm192-128a32 32 0 0 1-32-32V416a32 32 0 0 1 64 0v320a32 32 0 0 1-32 32m192 0a32 32 0 0 1-32-32V416a32 32 0 0 1 64 0v320a32 32 0 0 1-32 32"></path></svg></i>`;
        dislikeBtn.addEventListener('click', () => {
            voteTorrent(tid, 'POINTLESS');
        });
        dislikeDiv.appendChild(dislikeBtn);

        sideBlk.appendChild(favoriteDiv);
        sideBlk.appendChild(dislikeDiv);

        // 保存按钮引用,以便在键盘事件中使用
        window.dislikeButton = dislikeBtn;
    }

    // 定义投票函数
    function voteTorrent(tid, value) {
        // 获取必要的认证信息
        const authorization = localStorage.getItem('token')
        const deviceId = localStorage.getItem('DeviceId')
        // 构建FormData
        const formData = new FormData();
        formData.append('tid', tid);
        formData.append('status', value);
        // 发送请求
        fetch('/api/Torrents/voteTorrent', {
            method: 'POST',
            headers: {
                'accept': 'application/json, text/plain, */*',
                'authorization': authorization,
                'deviceid': deviceId,
                'origin': 'https://fsm.name',
                'referer': window.location.href
            },
            body: formData,
            credentials: 'same-origin'
        })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        })
        .then(res => {
            if (res && res.success) {
                if (window.$notify) {
                    window.$notify({
                        message: '操作成功',
                        type: 'success'
                    });
                } else {
                    window.close();
                }
            }
        })
        .catch(error => {
            if (window.$notify) {
                window.$notify({
                    message: '操作失败',
                    type: 'error'
                });
            } else {
                alert('操作失败');
            }
        });
    }

    // 添加键盘事件监听器,当按下"x"键时触发不喜欢按钮
    function addKeyboardShortcuts() {
        document.addEventListener('keydown', function(event) {
            // 检查是否按下了"x"键,并且不是在输入框中
            if (event.key === 'x' &&
                !['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName)) {
                // 如果不喜欢按钮存在,则触发点击事件
                if (window.dislikeButton) {
                    window.dislikeButton.click();
                }
            }
        });
    }

    // 标记是否已经添加了键盘快捷键
    let keyboardShortcutsAdded = false;

    // 使用MutationObserver来确保DOM加载完成
    const observer = new MutationObserver((mutations, obs) => {
        const sideBlk = document.querySelector('.side-blk');
        if (sideBlk) {
            obs.disconnect();
            addButtons();

            // 确保键盘快捷键只添加一次
            if (!keyboardShortcutsAdded) {
                addKeyboardShortcuts();
                keyboardShortcutsAdded = true;
            }
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 同时保留原有的加载检查
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            addButtons();

            // 确保键盘快捷键只添加一次
            if (!keyboardShortcutsAdded) {
                addKeyboardShortcuts();
                keyboardShortcutsAdded = true;
            }
        });
    } else {
        addButtons();

        // 确保键盘快捷键只添加一次
        if (!keyboardShortcutsAdded) {
            addKeyboardShortcuts();
            keyboardShortcutsAdded = true;
        }
    }
})();