// ==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
// @downloadURL none
// ==/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 = ``;
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 = ``;
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;
}
}
})();