// ==UserScript== // @name 银河复读放置+1功能 // @namespace http://tampermonkey.net/ // @version 2025-04-06 // @description 牛牛+1脚本,点击按钮填充文本将第53,54行取消注释可以加入汪汪队,拒绝猫娘从我做起 // @author Greenwaln // @match https://www.milkywayidle.com/* // @match https://test.milkywayidle.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=milkywayidle.com // @grant none // @license GPL-3.0 // @downloadURL none // ==/UserScript== (function () { 'use strict'; const chatSelector = 'div.ChatHistory_chatHistory__1EiG3'; const messageSelector = '.ChatMessage_chatMessage__2wev4'; const inputSelector = 'input.Chat_chatInput__16dhX'; // 获取原生 setter,确保在 React 受控输入框上更新值有效 const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; // 插入自定义样式 const style = document.createElement('style'); style.innerHTML = ` .tm-plus1-btn { display: inline-block; vertical-align: middle; color: transparent; /* 默认文字透明 */ background: none; border: none; font-size: inherit; cursor: pointer; width: 4ch; /* 固定宽度4字符 */ text-align: center; line-height: 1; transition: color 0.2s; } .tm-plus1-btn:hover { color: white; /* 悬停时显示白色 */ } `; document.head.appendChild(style); // 创建 +1 按钮 function createPlusOneButton(text) { const btn = document.createElement('button'); btn.innerText = '+1'; btn.classList.add('tm-plus1-btn'); btn.addEventListener('click', (e) => { e.stopPropagation(); const input = document.querySelector(inputSelector); if (input) { // text = text.replace(/喵/g, '汪'); // text += '汪'; nativeInputValueSetter.call(input, text); input.dispatchEvent(new Event('input', { bubbles: true })); } }); return btn; } // 从消息节点中提取纯文本内容及其所在的元素(排除时间戳、用户名等) function extractPureTextAndElement(msgElement) { const spans = msgElement.querySelectorAll('span'); for (let i = spans.length - 1; i >= 0; i--) { const span = spans[i]; if (span.childElementCount === 0 && span.textContent.trim() !== '') { if (!span.closest('.ChatMessage_name__1W9tB') && !span.closest('.ChatMessage_timestamp__1iRZO')) { return { text: span.textContent.trim(), element: span }; } } } return { text: '', element: null }; } // 为每条消息添加 +1 按钮 function addButtons() { const messages = document.querySelectorAll(messageSelector); messages.forEach((msg) => { if (!msg.dataset.hasPlusOne) { const { text, element } = extractPureTextAndElement(msg); if (text && element) { if (!msg.querySelector('.tm-plus1-btn')) { const btn = createPlusOneButton(text); msg.appendChild(btn); // 插入整个消息节点的末尾 } } msg.dataset.hasPlusOne = 'true'; } }); } // 监听聊天区域的变化(针对 SPA 动态加载新消息) const chatContainer = document.querySelector(chatSelector); if (chatContainer) { const observer = new MutationObserver(() => { addButtons(); }); observer.observe(chatContainer, { childList: true, subtree: true }); } // 定时检查,确保新消息能及时添加按钮 setInterval(addButtons, 500); })();