// ==UserScript== // @name ChatBot Text Replacer // @namespace http://tampermonkey.net/ // @version 0.2.1 // @description Replace shortcuts with predefined text in ChatBot input // @author Eric // @match https://chatgpt.com/* // @match https://claude.ai/* // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM_setValue // @license GPL Licence // @downloadURL none // ==/UserScript== (function () { 'use strict'; // Load replacements from GM storage let replacements = GM_getValue('replacements', { '/tl': '翻译以下内容:', '/pr': "我正在写一篇计算机领域的英文学术论文,请帮我润色。请以```latex ```格式输出,并注意符合latex格式", '/qa': "完成这道题。请先分析这道题目,再给出答案。" }); // Register Settings Menu Command GM_registerMenuCommand('Settings', openSettings); function openSettings() { // Create Settings Modal const modal = document.createElement('div'); modal.style.display = 'block'; modal.style.position = 'fixed'; modal.style.top = '50%'; modal.style.left = '50%'; modal.style.transform = 'translate(-50%, -50%)'; modal.style.backgroundColor = '#000'; modal.style.color = '#fff'; modal.style.padding = '20px'; modal.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)'; modal.style.zIndex = '1000'; modal.innerHTML = '
const newP = document.createElement('p'); promptTextarea.appendChild(newP); // 将光标设置到新的
中 const selection = window.getSelection(); const range = document.createRange(); range.selectNodeContents(newP); range.collapse(false); selection.removeAllRanges(); selection.addRange(range); break; } } } }); } // 确保 targetNode 被正确获取 function observeTargetNode_GPT() { const targetNode = document.getElementById('prompt-textarea'); if (targetNode) { console.log("Target node found:", targetNode); const observer = new MutationObserver(mutationCallback_ChatGPT); observer.observe(targetNode, { childList: true, subtree: true, characterData: true }); } else { console.log("Target node not found, retrying..."); setTimeout(observeTargetNode_GPT, 1000); // 每秒重试一次 } } function mutationCallback_Claude(mutationList, observer) { console.log(mutationList); mutationList.forEach(mutation => { if (mutation.type === 'characterData'){ const inputString = mutation.target.data; console.log(inputString); // replace shortcuts for (const [shortcut, replacement] of Object.entries(replacements)) { if (inputString.includes(shortcut + ' ')) { mutation.target.data = inputString.replace(shortcut + ' ', replacement); // 增加一个
const promptTextarea = document.querySelector("body > div.flex.min-h-screen.w-full > div > main > div.top-5.z-10.mx-auto.w-full.max-w-2xl.md\\:sticky > div > fieldset > div.flex.flex-col.bg-bg-000.gap-1\\.5.border-0\\.5.border-border-300.pl-4.pt-2\\.5.pr-2\\.5.pb-2\\.5.sm\\:mx-0.items-stretch.transition-all.duration-200.relative.shadow-\\[0_0\\.25rem_1\\.25rem_rgba\\(0\\,0\\,0\\,0\\.035\\)\\].focus-within\\:shadow-\\[0_0\\.25rem_1\\.25rem_rgba\\(0\\,0\\,0\\,0\\.075\\)\\].hover\\:border-border-200.focus-within\\:border-border-200.cursor-text.z-10.rounded-2xl > div.flex.gap-2 > div.mt-1.max-h-96.w-full.overflow-y-auto.break-words.min-h-\\[4\\.5rem\\] > div"); const newP = document.createElement('p'); promptTextarea.appendChild(newP); // 将光标设置到新的
中 const selection = window.getSelection(); const range = document.createRange(); range.selectNodeContents(newP); range.collapse(false); selection.removeAllRanges(); selection.addRange(range); break; } } } }); } function observeTargetNode_ClaudeAI() { const targetNode = document.querySelector("body > div.flex.min-h-screen.w-full > div > main > div.top-5.z-10.mx-auto.w-full.max-w-2xl.md\\:sticky > div > fieldset > div.flex.flex-col.bg-bg-000.gap-1\\.5.border-0\\.5.border-border-300.pl-4.pt-2\\.5.pr-2\\.5.pb-2\\.5.sm\\:mx-0.items-stretch.transition-all.duration-200.relative.shadow-\\[0_0\\.25rem_1\\.25rem_rgba\\(0\\,0\\,0\\,0\\.035\\)\\].focus-within\\:shadow-\\[0_0\\.25rem_1\\.25rem_rgba\\(0\\,0\\,0\\,0\\.075\\)\\].hover\\:border-border-200.focus-within\\:border-border-200.cursor-text.z-10.rounded-2xl > div.flex.gap-2 > div.mt-1.max-h-96.w-full.overflow-y-auto.break-words.min-h-\\[4\\.5rem\\] > div") if (targetNode) { console.log("Target node found:", targetNode); const observer = new MutationObserver(mutationCallback_Claude); observer.observe(targetNode, { childList: true, subtree: true, characterData: true }); } else { console.log("Target node not found, retrying..."); setTimeout(observeTargetNode_ClaudeAI, 1000); // 每秒重试一次 } } function observeTargetNode() { const isClaudeAI = window.location.href.includes('claude.ai'); const isChatGPT = window.location.href.includes('chatgpt.com'); if (isClaudeAI) { observeTargetNode_ClaudeAI(); } else if (isChatGPT) { observeTargetNode_GPT(); } } observeTargetNode(); })();