// ==UserScript== // @name 银河复读放置+1功能 // @namespace http://tampermonkey.net/ // @version 1.1.0 // @description 牛牛+1脚本,点击按钮填充文本将第57,58行取消注释可以加入汪汪队,拒绝猫娘从我做起 // @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 { margin-left: 1ch; width: 4ch; padding: 0; text-align: right; background-color: rgba(0,0,0,0.001); color: rgba(0,0,0,0.001); border: none; } .tm-plus1-btn:hover { margin-left: 1ch; border-radius: 4px; width: 4ch; padding: 0; border: none; font-family: "Roboto"; text-align: center; background-color: var(--color-primary); color: var(--color-text-dark-mode); user-select: none; cursor: pointer; align-items: center; } `; 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 textParts = []; // 要排除的 class 列表 const excludedSelectors = [ '.ChatMessage_name__1W9tB', '.ChatMessage_timestamp__1iRZO' ]; // 判断某个节点是否应该被排除 function isExcluded(node) { return excludedSelectors.some(sel => node.closest && node.closest(sel)); } // 遍历所有子元素 msgElement.querySelectorAll('*').forEach(el => { if (isExcluded(el)) return; // 忽略冒号 if (el.textContent === ': ') return; // 链接处理:直接取 href if (el.tagName === 'A' && el.href) { textParts.push(el.href); } else if (el.childElementCount === 0) { const text = el.textContent.trim(); if (text) textParts.push(text); } }); return { text: textParts.join(' '), element: msgElement }; } // 为每条消息添加 +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); })();