// ==UserScript== // @name Prefix ChatGPT chats with "S:" // @namespace http://tampermonkey.net/ // @version 2024-11-23T16:11:30+05:30 // @description For TOTALLY legit usage reasons... // @match https://chatgpt.com/* // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // ==/UserScript== (function() { 'use strict'; GM_registerMenuCommand('Set Prefix', setPrefix); function setPrefix() { const currentPrefix = GM_getValue('PREFIX', 'S: '); const newPrefix = prompt('Enter the new prefix:', currentPrefix); if (newPrefix !== null) { GM_setValue('PREFIX', newPrefix); alert('Prefix set to: ' + newPrefix); } } function getPrefix() { return GM_getValue('PREFIX', 'S: '); } const sels = { activeChatSidebarItem: '[data-testid^="history-item"]:has(> [class~="bg-token-sidebar-surface-secondary"])', renamePopoverItem: '[data-testid="share-chat-menu-item"] + div', }; function triggerEnterKey(el) { const event = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', bubbles: true }); el.dispatchEvent(event); } function wait(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function getStableLabelText(el) { while (true) { const prev = el.textContent.trim(); await wait(1000); const curr = el.textContent.trim(); if (prev === curr) { return curr; } } } async function main() { try { const menuItem = document.querySelector(sels.activeChatSidebarItem); if (!menuItem) { console.log("Active chat menu item wasn't found. You're either not on the regular chat page or the markup has changed"); return; } const label = await getStableLabelText(menuItem.querySelector("a")); if (/new chat/i.test(label)) { console.log("Chat label isn't yet autopopulated by LLM"); return; } const prefix = getPrefix(); if (label.startsWith(prefix)) { console.log("No need to rename chat"); return; } triggerEnterKey(menuItem.querySelector("button")); await wait(100); document.querySelector(sels.renamePopoverItem).click(); await wait(500); const input = menuItem.querySelector("input"); const prevValue = input.value; input.focus(); await wait(100); input.value = `${prefix}${label}`; if (input._valueTracker) { input._valueTracker.setValue(prevValue); } const inputEvent = new Event('input', { bubbles: true }); input.dispatchEvent(inputEvent); triggerEnterKey(input); await wait(100); console.log("Renamed chat"); } finally { setTimeout(main, 5000); } } setTimeout(main, 5000); })();