功能:会当你在犇犇中输入标点符号(“。”、“?”、“!”)时在前面打一个“喵”字。
这份代码保证不会调用任何洛谷的api(并没有声明网络请求权限)。
代码由AI生成。
更新日志:
- 1.0:初稿。
- 1.1:修复了按标点符号会显示两个喵的问题。
- 1.2:设置只在首页加载。
- 1.3:加入找到犇犇框后不再搜索的功能。
- 1.4:删掉了目标字符串中的三个英文标点,减少网址、图床等识别错的问题。
- 1.5:匹配第一个 || 前的内容,修复了会修改他人犇犇的bug。
// ==UserScript==
// @name 洛谷犇犇喵化助手 v1.5
// @namespace http://tampermonkey.net/
// @version 1.5
// @description 在洛谷犇犇中自动在句末标点前加"喵"字,支持 || 分隔符,跳过他人内容
// @author Your Name
// @match https://www.luogu.com.cn/
// @match https://luogu.com.cn/
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 只在主站运行(排除题目页、比赛页等)
const currentPath = window.location.pathname;
if (currentPath !== '/' && !currentPath.startsWith('/feed')) {
console.log('🐱 非主站/犇犇页面,脚本不运行');
return;
}
console.log('🐱 喵化助手 v1.5 开始加载...');
// ==================== 核心功能 ====================
// 核心函数:智能加"喵"(支持 || 分隔符)
function addMiao(text) {
if (!text || typeof text !== 'string') return text;
// 查找分隔符 "||" 的位置
const separatorIndex = text.indexOf('||');
// 如果没有分隔符,处理全文
if (separatorIndex === -1) {
return processTextSegment(text);
}
// 有分隔符:只处理分隔符之前的部分,之后的部分原样保留
const beforeSeparator = text.substring(0, separatorIndex);
const afterSeparator = text.substring(separatorIndex); // 包含 || 及之后所有内容
// 只处理前半部分,后半部分(他人犇犇)保持不变
const processedBefore = processTextSegment(beforeSeparator);
// 返回组合结果
return processedBefore + afterSeparator;
}
// 辅助函数:处理单个文本片段(不含 || 分隔符)
function processTextSegment(segment) {
if (!segment) return segment;
let result = '';
const targetPunctuation = '。?!'; // 需要加"喵"的标点
for (let i = 0; i < segment.length; i++) {
const char = segment[i];
// 检查是否是目标标点
if (targetPunctuation.includes(char)) {
// 检查前面是否已经是"喵",避免重复
if (i === 0 || segment[i - 1] !== '喵') {
result += '喵';
}
}
result += char;
}
return result;
}
// ==================== 输入框处理 ====================
// 查找犇犇发布框
function findBenbenBox() {
const selectors = [
'textarea[placeholder*="犇"]',
'textarea[placeholder*="说"]',
'textarea[placeholder*="动态"]',
'.feed-input textarea',
'.feeds-container textarea',
'#feed-editor textarea'
];
for (const selector of selectors) {
const element = document.querySelector(selector);
if (element && element.offsetParent !== null) {
console.log('✅ 找到犇犇框:', selector);
return element;
}
}
// 备用:查找所有可见textarea
const allVisibleTextareas = Array.from(document.querySelectorAll('textarea'))
.filter(ta => ta.offsetParent !== null);
return allVisibleTextareas[0] || null;
}
// 修复版:处理输入框内容的函数(精确光标计算)
function processInputElement(element) {
if (!element) return;
// 仅对输入框(textarea/input)进行精确光标处理
if (element.tagName === 'TEXTAREA' || element.tagName === 'INPUT') {
const oldValue = element.value;
if (!oldValue) return; // 空值不处理
// 1. 保存当前的光标位置和选区
const cursorStart = element.selectionStart;
const cursorEnd = element.selectionEnd;
// 2. 获取光标前后的文本
const textBeforeCursor = oldValue.substring(0, cursorStart);
const textAfterCursor = oldValue.substring(cursorEnd);
// 3. 分别处理光标前和全文的文本
const processedTextBefore = addMiao(textBeforeCursor);
const processedFullText = addMiao(oldValue);
// 4. 计算精确的光标偏移量
// 偏移量 = (处理后的光标前文本长度) - (原光标前文本长度)
const cursorOffset = processedTextBefore.length - textBeforeCursor.length;
// 5. 如果全文确实被修改了,则更新
if (processedFullText !== oldValue) {
element.value = processedFullText;
// 6. 设置新的精确光标位置
const newCursorStart = cursorStart + cursorOffset;
const newCursorEnd = cursorEnd + cursorOffset;
element.setSelectionRange(newCursorStart, newCursorEnd);
// 7. 触发事件
element.dispatchEvent(new Event('change', { bubbles: true }));
console.log('📝 已处理文本 (支持 || 分隔符)');
}
} else {
// 对于非输入框,使用简单逻辑
const oldValue = element.textContent || '';
const newValue = addMiao(oldValue);
if (newValue !== oldValue) {
element.textContent = newValue;
element.dispatchEvent(new Event('change', { bubbles: true }));
}
}
}
// ==================== 输入法优化监听器 ====================
// 智能监听器(解决搜狗输入法光标问题)
function setupSmartListener(element) {
if (!element) return;
const marker = 'data-miao-v15-attached';
if (element.getAttribute(marker)) return;
element.setAttribute(marker, 'true');
console.log('🎯 安装智能监听器 v1.5');
// 状态变量
let isComposing = false; // 是否正在输入法组字
let pendingTimer = null; // 待处理的计时器
// 输入法开始事件(中文输入)
element.addEventListener('compositionstart', () => {
isComposing = true;
if (pendingTimer) {
clearTimeout(pendingTimer);
pendingTimer = null;
}
});
// 输入法结束事件
element.addEventListener('compositionend', () => {
isComposing = false;
// 输入法结束后,延迟处理
if (pendingTimer) clearTimeout(pendingTimer);
pendingTimer = setTimeout(() => {
processInputElement(element);
pendingTimer = null;
}, 150);
});
// 输入事件(主要处理英文直接输入)
element.addEventListener('input', (e) => {
if (isComposing) return; // 正在输入法组字,跳过
if (pendingTimer) clearTimeout(pendingTimer);
// 使用防抖技术,延迟处理避免与输入法冲突
pendingTimer = setTimeout(() => {
processInputElement(element);
pendingTimer = null;
}, 300); // 300ms延迟足够让所有输入法完成上屏
});
// 失焦事件(确保最后的内容被处理)
element.addEventListener('blur', () => {
if (pendingTimer) clearTimeout(pendingTimer);
processInputElement(element);
});
// 初始检查
setTimeout(() => processInputElement(element), 500);
}
// ==================== 智能监控系统 ====================
// 智能监控:找到目标后自动停止
let monitorInterval = null;
function startSmartMonitoring() {
console.log('🔍 启动智能监控系统...');
if (monitorInterval) clearInterval(monitorInterval);
let foundCount = 0;
const MAX_ATTEMPTS = 40; // 最多尝试40次(约2分钟)
monitorInterval = setInterval(() => {
foundCount++;
console.log(`🔄 [监控轮次 ${foundCount}] 检查犇犇发布框...`);
const targetElement = findBenbenBox();
if (targetElement) {
console.log('✅ 找到目标,开始绑定监听器...');
setupSmartListener(targetElement);
stopMonitoring();
showNotice('🐱 犇犇喵化助手 v1.5 已就绪');
return;
}
// 安全兜底:达到最大尝试次数后停止
if (foundCount >= MAX_ATTEMPTS) {
console.log('⏱️ 已达到最大监控时长,停止搜索。');
stopMonitoring();
showNotice('未找到犇犇输入框');
}
}, 3000); // 每3秒检查一次
}
function stopMonitoring() {
if (monitorInterval) {
clearInterval(monitorInterval);
monitorInterval = null;
console.log('🛑 智能监控已停止');
}
}
// ==================== 界面反馈 ====================
function showNotice(message) {
// 移除旧通知
const oldNotice = document.getElementById('miao-notice');
if (oldNotice) oldNotice.remove();
// 创建新通知
const notice = document.createElement('div');
notice.id = 'miao-notice';
notice.textContent = message;
notice.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 10px 16px;
border-radius: 8px;
font-size: 13px;
z-index: 10000;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
animation: miaoSlideIn 0.3s ease;
max-width: 300px;
`;
// 添加动画样式
if (!document.querySelector('#miao-notice-style')) {
const style = document.createElement('style');
style.id = 'miao-notice-style';
style.textContent = `
@keyframes miaoSlideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
`;
document.head.appendChild(style);
}
document.body.appendChild(notice);
// 5秒后自动消失
setTimeout(() => {
if (notice.parentNode) {
notice.style.opacity = '0';
notice.style.transition = 'opacity 0.5s';
setTimeout(() => notice.remove(), 500);
}
}, 5000);
}
// ==================== 主初始化 ====================
function init() {
console.log('🚀 初始化喵化助手 v1.5');
// 立即尝试查找
const immediateResult = findBenbenBox();
if (immediateResult) {
console.log('✅ 立即找到输入框,跳过监控');
setupSmartListener(immediateResult);
showNotice('🐱 犇犇喵化助手 v1.5 已就绪');
} else {
console.log('⏳ 未立即找到,启动智能监控...');
showNotice('正在寻找犇犇输入框...');
startSmartMonitoring();
}
}
// ==================== 启动脚本 ====================
// 等待页面加载
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(init, 1000);
});
} else {
setTimeout(init, 1000);
}
// ==================== 调试工具 ====================
// 提供测试命令
window.testMiao = function(text) {
const result = addMiao(text);
console.log('测试:', {输入: text, 输出: result});
return result;
};
// 提供状态查询
window.getMiaoStatus = function() {
const element = findBenbenBox();
return {
version: '1.5',
isMonitoring: !!monitorInterval,
foundElement: !!element,
elementType: element ? element.tagName : 'none',
isListenerAttached: element ? element.hasAttribute('data-miao-v15-attached') : false
};
};
console.log('✅ 喵化助手 v1.5 初始化完成');
})();