2024-11-23 10:12:26 +00:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Prefix ChatGPT chats with "S:"
|
|
|
|
// @namespace http://tampermonkey.net/
|
2024-11-23 10:50:59 +00:00
|
|
|
// @version 2024-11-23T16:20:49+05:30
|
2024-11-23 10:12:26 +00:00
|
|
|
// @description For TOTALLY legit usage reasons...
|
2024-11-23 10:41:52 +00:00
|
|
|
// @match https://chatgpt.com/*
|
2024-11-23 10:50:36 +00:00
|
|
|
// @grant GM_getValue
|
|
|
|
// @grant GM_setValue
|
|
|
|
// @grant GM_registerMenuCommand
|
2024-11-23 10:12:26 +00:00
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2024-11-23 10:50:36 +00:00
|
|
|
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: ');
|
|
|
|
}
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
const sels = {
|
2024-11-23 10:41:52 +00:00
|
|
|
activeChatSidebarItem: '[data-testid^="history-item"]:has(> [class~="bg-token-sidebar-surface-secondary"])',
|
2024-11-23 10:12:26 +00:00
|
|
|
renamePopoverItem: '[data-testid="share-chat-menu-item"] + div',
|
2024-11-23 10:50:36 +00:00
|
|
|
};
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
function triggerEnterKey(el) {
|
|
|
|
const event = new KeyboardEvent('keydown', {
|
|
|
|
key: 'Enter',
|
|
|
|
code: 'Enter',
|
|
|
|
bubbles: true
|
|
|
|
});
|
|
|
|
|
|
|
|
el.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait(ms) {
|
2024-11-23 10:50:36 +00:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
2024-11-23 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-23 10:41:52 +00:00
|
|
|
async function getStableLabelText(el) {
|
|
|
|
while (true) {
|
|
|
|
const prev = el.textContent.trim();
|
|
|
|
await wait(1000);
|
|
|
|
const curr = el.textContent.trim();
|
2024-11-23 10:12:26 +00:00
|
|
|
|
2024-11-23 10:41:52 +00:00
|
|
|
if (prev === curr) {
|
|
|
|
return curr;
|
2024-11-23 10:12:26 +00:00
|
|
|
}
|
2024-11-23 10:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-23 10:12:26 +00:00
|
|
|
|
2024-11-23 10:41:52 +00:00
|
|
|
async function main() {
|
|
|
|
try {
|
2024-11-23 10:12:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-11-23 10:41:52 +00:00
|
|
|
const label = await getStableLabelText(menuItem.querySelector("a"));
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
if (/new chat/i.test(label)) {
|
|
|
|
console.log("Chat label isn't yet autopopulated by LLM");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-23 10:50:36 +00:00
|
|
|
const prefix = getPrefix();
|
|
|
|
|
|
|
|
if (label.startsWith(prefix)) {
|
2024-11-23 10:12:26 +00:00
|
|
|
console.log("No need to rename chat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerEnterKey(menuItem.querySelector("button"));
|
2024-11-23 10:41:52 +00:00
|
|
|
await wait(100);
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
document.querySelector(sels.renamePopoverItem).click();
|
2024-11-23 10:41:52 +00:00
|
|
|
await wait(500);
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
const input = menuItem.querySelector("input");
|
|
|
|
const prevValue = input.value;
|
|
|
|
|
|
|
|
input.focus();
|
2024-11-23 10:41:52 +00:00
|
|
|
await wait(100);
|
2024-11-23 10:12:26 +00:00
|
|
|
|
2024-11-23 10:50:36 +00:00
|
|
|
input.value = `${prefix}${label}`;
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
if (input._valueTracker) {
|
|
|
|
input._valueTracker.setValue(prevValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputEvent = new Event('input', { bubbles: true });
|
|
|
|
input.dispatchEvent(inputEvent);
|
|
|
|
|
|
|
|
triggerEnterKey(input);
|
2024-11-23 10:41:52 +00:00
|
|
|
await wait(100);
|
2024-11-23 10:12:26 +00:00
|
|
|
|
|
|
|
console.log("Renamed chat");
|
|
|
|
} finally {
|
2024-11-23 10:41:52 +00:00
|
|
|
setTimeout(main, 5000);
|
2024-11-23 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 10:41:52 +00:00
|
|
|
setTimeout(main, 5000);
|
2024-11-23 10:12:26 +00:00
|
|
|
})();
|