正文
增加 137 行删除 25 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152108 行未改动全部展开161162163164165166167168169## 前言在洛谷私信发一大段消息的时候,由于无法自动聚焦输入框,经常要点击,极大地影响了我们水谷的速度(目前仅有自动聚焦输入框一个功能。## 运行逻辑### 查找输入框~~我懒,基本把常见的输入框都涵盖了。~~1. 匹配常见的 `textarea` 类名、`contenteditable` 区域、占位符等。2. 遍历页面上所有 `<textarea>`,筛选出可见且占位符包含“私信”/“消息”或在聊天区域内的元素。3. 查找 `[contenteditable="true"]` 且位于聊天区域的元素。可见性通过 `offsetParent !== null` 判断,确保输入框未被隐藏。### 聚焦输入框1. 调用 `getInputElement()` 获取输入框元素。2. 若找到,则执行 `input.focus()`。- 对于可编辑的 `contenteditable` 元素,将光标折叠到末尾。- 若未找到,则在 $100$ ms 后重试,最多重试 $3$ 次。### 流程图## 源码:::info[Luogu Chat Better!]{open}```javascript// ==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',108 行未改动全部展开window.addEventListener('load', () => {setTimeout(focusInput, 500);});})();```:::正文和流程图由deepseek v3润色。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455108 行未改动全部展开164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281## 前言在洛谷私信发一大段消息的时候,由于无法自动聚焦输入框,经常要点击,极大地影响了我们水谷的速度,所以做了这个插件(目前有自动聚焦输入框、更改自己的消息颜色和私信图片显示功能。## 源码将代码放到油猴里就行了。整个脚本最前端,有一块代码:```javascriptconst COLOR_CONFIG = {enableOwnMessageColor: true,// 是否启用自定义颜色ownMessageBgColor: "#d9f0ff",// 自己消息框的背景色ownMessageTextColor: "#000000" // 自己消息的文字颜色};```你可以修改参数的值来实现不同的效果。- `enableOwnMessageColor`:是否启用颜色修改功能,`true` 则启用;`false` 则不启用。- `ownMessageBgColor`:自己消息框的背景色,十六进制格式。- `ownMessageTextColor`:自己消息的文字颜色,十六进制格式。:::info[Luogu Chat Better!]{open}```javascript// ==UserScript==// @name Luogu Chat Better!// @namespace http://tampermonkey.net/// @version 3.1// @description 洛谷私信 自动聚焦输入框 + 图片显示 + 颜色修改// @author Forever_Rain// @match https://www.luogu.com.cn/chat// @match https://www.luogu.com.cn/chat/*// @grant none// ==/UserScript==/* 用户自定义颜色配置 */const COLOR_CONFIG = {enableOwnMessageColor: true,// 是否启用自定义颜色ownMessageBgColor: "#d9f0ff",// 自己消息框的背景色ownMessageTextColor: "#000000" // 自己消息的文字颜色};/* 用户自设部分 */// 自动聚焦输入框(function() {'use strict';const inputSelectors = ['textarea.lg-message-input',108 行未改动全部展开window.addEventListener('load', () => {setTimeout(focusInput, 500);});})();// 图片显示(function() {'use strict';// 要搜索的 class 列表const searchClasses = ['message'];function replacePics(element) {if (!element) return;const walker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT, {acceptNode: function(node) {if (node.classList && searchClasses.some(className => node.classList == className)) {return NodeFilter.FILTER_ACCEPT;}return NodeFilter.FILTER_SKIP;}}, false);let node;while (node = walker.nextNode()) {var a = node.innerHTML.indexOf(";if(b == -1) break;var c = node.innerHTML.indexOf(")", b);if(c == -1) break;var link = node.innerHTML.substr(b + 2, c - b - 2);var text = node.innerHTML.substr(a + 2, b - a - 2);if(text == '') text = link;var regex = node.innerHTML.substr(a, c - a + 1);node.innerHTML = node.innerHTML.replace(regex, `<img style = "max-width: 516px;" src="${link}" alt="${text}">`);a = node.innerHTML.indexOf("![")}}}replacePics(document.body);// 监听DOMconst observer = new MutationObserver(mutations => {mutations.forEach(mutation => {if (mutation.type === 'childList') {mutation.addedNodes.forEach(node => {if (node.nodeType === Node.ELEMENT_NODE) {replacePics(node);}});}});});observer.observe(document.body, { childList: true, subtree: true });(function() {'use strict';// 读取const enable = COLOR_CONFIG.enableOwnMessageColor;if (!enable) {return;}const bgColor = COLOR_CONFIG.ownMessageBgColor;const textColor = COLOR_CONFIG.ownMessageTextColor;// 校验const isValidColor = (color) => /^#[0-9A-Fa-f]{6}$/.test(color);const finalBg = isValidColor(bgColor) ? bgColor : "#d9f0ff";const finalText = isValidColor(textColor) ? textColor : "#000000";// 注入自定义样式const styleId = "luogu-own-message-color-style";if (!document.getElementById(styleId)) {const style = document.createElement("style");style.id = styleId;style.textContent = `/* 自己发送的消息气泡背景 & 文字颜色 */.message-block.right .message {background-color: ${finalBg} !important;color: ${finalText} !important;}/* 调整气泡圆角等保持美观,不覆盖原有设计 */.message-block.right .message {border-radius: 12px !important;padding: 6px 12px !important;}`;document.head.appendChild(style);}})();})();```::::::info[运行逻辑(待写)]## 运行逻辑### 查找输入框基本把常见的输入框都涵盖了,所以把 `@match` 的值改一下也可以当其他网站的插件用。1. 匹配常见的 `textarea` 类名、`contenteditable` 区域、占位符等。2. 遍历页面上所有 `<textarea>`,筛选出可见且占位符包含“私信”/“消息”或在聊天区域内的元素。3. 查找 `[contenteditable="true"]` 且位于聊天区域的元素。可见性通过 `offsetParent !== null` 判断,确保输入框未被隐藏。### 聚焦输入框1. 调用 `getInputElement()` 获取输入框元素。2. 若找到,则执行 `input.focus()`。- 对于可编辑的 `contenteditable` 元素,将光标折叠到末尾。- 若未找到,则在 $100$ ms 后重试,最多重试 $3$ 次。### 流程图:::