Add configurable prefix for chatgpt prefixer (gpt)

This commit is contained in:
Sangeeth Sudheer 2024-11-23 16:20:36 +05:30
parent ca47ee3e30
commit bff9abbf7d
Signed by: x
GPG Key ID: F6D06ECE734C57D1

View File

@ -3,23 +3,34 @@
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 2024-11-23T16:11:30+05:30 // @version 2024-11-23T16:11:30+05:30
// @description For TOTALLY legit usage reasons... // @description For TOTALLY legit usage reasons...
// @author Sangeeth Sudheer
// @match https://chatgpt.com/* // @match https://chatgpt.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com // @grant GM_getValue
// @updateURL https://git.sangeeth.dev/x/userscripts/raw/branch/main/chatgpt-chat-prefixer/ChatGPTChatPrefixer.user.js // @grant GM_setValue
// @downloadURL https://git.sangeeth.dev/x/userscripts/raw/branch/main/chatgpt-chat-prefixer/ChatGPTChatPrefixer.user.js // @grant GM_registerMenuCommand
// @grant none
// ==/UserScript== // ==/UserScript==
(function() { (function() {
'use strict'; 'use strict';
const PREFIX = "S: "; 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 = { const sels = {
activeChatSidebarItem: '[data-testid^="history-item"]:has(> [class~="bg-token-sidebar-surface-secondary"])', activeChatSidebarItem: '[data-testid^="history-item"]:has(> [class~="bg-token-sidebar-surface-secondary"])',
renamePopoverItem: '[data-testid="share-chat-menu-item"] + div', renamePopoverItem: '[data-testid="share-chat-menu-item"] + div',
} };
function triggerEnterKey(el) { function triggerEnterKey(el) {
const event = new KeyboardEvent('keydown', { const event = new KeyboardEvent('keydown', {
@ -32,32 +43,7 @@
} }
function wait(ms) { function wait(ms) {
const { resolve, reject, promise } = Promise.withResolvers(); return new Promise(resolve => setTimeout(resolve, ms));
setTimeout(resolve, ms);
return promise;
}
function waitFor(sel, el = document) {
const { resolve, reject, promise } = Promise.withResolvers();
let count = 0;
function _wait() {
if (el.querySelector(sel)) {
resolve();
}
++count;
if (count >= 5) {
reject();
}
setTimeout(_wait, 1000);
}
_wait();
} }
async function getStableLabelText(el) { async function getStableLabelText(el) {
@ -88,7 +74,9 @@
return; return;
} }
if (label.startsWith(PREFIX)) { const prefix = getPrefix();
if (label.startsWith(prefix)) {
console.log("No need to rename chat"); console.log("No need to rename chat");
return; return;
} }
@ -105,7 +93,7 @@
input.focus(); input.focus();
await wait(100); await wait(100);
input.value = `${PREFIX}${label}`; input.value = `${prefix}${label}`;
if (input._valueTracker) { if (input._valueTracker) {
input._valueTracker.setValue(prevValue); input._valueTracker.setValue(prevValue);
@ -123,7 +111,5 @@
} }
} }
setTimeout(main, 5000); setTimeout(main, 5000);
})(); })();