前言
在洛谷私信发一大段消息的时候,由于无法自动聚焦输入框,经常要点击,极大地影响了我们水谷的速度(
目前仅有自动聚焦输入框一个功能。
运行逻辑
查找输入框
我懒,基本把常见的输入框都涵盖了。
-
匹配常见的
textarea类名、contenteditable区域、占位符等。 -
遍历页面上所有
<textarea>,筛选出可见且占位符包含“私信”/“消息”或在聊天区域内的元素。 -
查找
[contenteditable="true"]且位于聊天区域的元素。
可见性通过 offsetParent !== null 判断,确保输入框未被隐藏。
聚焦输入框
- 调用
getInputElement()获取输入框元素。 - 若找到,则执行
input.focus()。
- 对于可编辑的
contenteditable元素,将光标折叠到末尾。 - 若未找到,则在 ms 后重试,最多重试 次。
流程图
源码
Luogu Chat Better!
// ==UserScript==
// @name Luogu Chat Better!
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 洛谷私信自动聚焦输入框
// @author Forever_Rain
// @match https://www.luogu.com.cn/chat
// @match https://www.luogu.com.cn/chat/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const inputSelectors = [
'textarea.lg-message-input',
'textarea[name="content"]',
'.chat-input textarea',
'.message-editor textarea',
'div[contenteditable="true"]',
'textarea[placeholder*="私信"]',
'textarea[placeholder*="消息"]',
'textarea.lfe-form-sz-middle',
'.input-wrap textarea',
'#chat-input textarea'
];
const sendButtonSelectors = [
'button[type="submit"]',
'button.send-btn',
'button:has(span:contains("发送"))',
'.send-button',
'.chat-send-btn'
];
function getInputElement() {
for (const selector of inputSelectors) {
const element = document.querySelector(selector);
if (element && element.offsetParent !== null) { // 确保可见
return element;
}
}
const allTextareas = document.querySelectorAll('textarea');
for (const ta of allTextareas) {
if (ta.offsetParent !== null && (ta.placeholder?.includes('私信') || ta.placeholder?.includes('消息') || ta.closest('.chat-area'))) {
return ta;
}
}
const editableDivs = document.querySelectorAll('[contenteditable="true"]');
for (const div of editableDivs) {
if (div.offsetParent !== null && div.closest('.chat-area')) {
return div;
}
}
return null;
}
function focusInput(retries = 3) {// 重试:3次
const input = getInputElement();
if (input) {
input.focus();
if (input.isContentEditable) {
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(input);
range.collapse(false); // 折叠到末尾
sel.removeAllRanges();
sel.addRange(range);
}
} else if (retries > 0) {
setTimeout(() => focusInput(retries - 1), 100);// 间隔0.1s
}
}
function isSendButton(el) {
if (!el) return false;
const tag = el.tagName.toLowerCase();
if (tag === 'button' || tag === 'input' && (el.type === 'submit' || el.type === 'button')) {
const text = el.innerText || el.value || '';
if (text.includes('发送') || text.includes('Send')) return true;
if (el.classList && (el.classList.contains('send-btn') || el.classList.contains('chat-send-btn'))) return true;
if (el.getAttribute('aria-label') === '发送') return true;
}
if (el.querySelector && el.querySelector('svg[data-icon="paper-plane"], svg[data-icon="send"]')) return true;
return false;
}
function onDocumentClick(e) {
let target = e.target;
while (target && target !== document.body) {
if (isSendButton(target)) {
setTimeout(focusInput, 120);
break;
}
target = target.parentElement;
}
}
function onKeyDown(e) {
// 检测 Ctrl+Enter
if (e.ctrlKey && e.key === 'Enter') {
const activeEl = document.activeElement;
if (activeEl && (activeEl.tagName === 'TEXTAREA' || activeEl.isContentEditable)) {
setTimeout(focusInput, 100);
}
}
}
document.addEventListener('click', onDocumentClick, true);
document.addEventListener('keydown', onKeyDown);
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList' && mutation.addedNodes.length) {
const activeTag = document.activeElement?.tagName;
if (activeTag !== 'TEXTAREA' && !document.activeElement?.isContentEditable) {
const input = getInputElement();
if (input && document.hasFocus()) {
setTimeout(() => {
if (document.activeElement !== input) {
input.focus();
}
}, 200);
}
}
break;
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
// 页面初始加载后,如果已有输入框且未聚焦,尝试聚焦一次(可选)
window.addEventListener('load', () => {
setTimeout(focusInput, 500);
});
})();正文和流程图由deepseek v3润色。
