Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // u/name chatgpt with memory
- // u/namespace http://tampermonkey.net/
- // u/version 0.1
- // u/description chatgpt with memory
- // u/author simoNCode
- // u/match https://chat.openai.com/chat
- // u/icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
- // u/grant none
- // u/require https://code.jquery.com/jquery-3.6.0.min.js
- // ==/UserScript==
- const generateUUID = () => {
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
- var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
- return v.toString(16);
- });
- }
- // GET THREADS
- const _threads = () => {
- return localStorage.getItem('chatgpt-threads') != null ? JSON.parse(localStorage.getItem('chatgpt-threads')): [];
- }
- const _setThreads = (threads) => {
- localStorage.setItem('chatgpt-threads', JSON.stringify(threads));
- }
- const _getActive = (active) => {
- let threads = _threads();
- return threads.filter(t => t.active)[0];
- }
- const _updateConvIdActive = (active) => {
- let threads = _threads();
- threads.filter(t => t.id == active.id)[0].conv_id = active.conv_id;
- threads.filter(t => t.id == active.id)[0].parent_id = active.parent_id;
- threads.filter(t => t.id == active.id)[0].messages = active.messages;
- _setThreads(threads);
- }
- const _updateMsgsActive = (msgs) => {
- let threads = _threads();
- threads.filter(t => t.active)[0].messages = threads.filter(t => t.active)[0].messages.concat(msgs);
- _setThreads(threads);
- }
- const _init = () => {
- let threads = _threads();
- if (threads.length == 0) {
- let btnObj = {
- id: generateUUID(),
- conv_id: 0,
- parent_id: 0,
- active: true,
- title: 'Chat',
- messages: []
- };
- threads.push(btnObj);
- _setThreads(threads);
- }
- _draw();
- }
- let nav = $('nav');
- let newThreadBtn = $('nav>a:nth-child(1)');
- nav.css('overflow-y', 'visible !important');
- newThreadBtn.click((e) => {
- e.preventDefault();
- let threads = _threads();
- for (let t of threads) {
- t.active = false;
- }
- let btnObj = {
- id: generateUUID(),
- conv_id: 0,
- parent_id: 0,
- active: true,
- title: 'Chat',
- messages: []
- };
- threads.push(btnObj);
- localStorage.setItem('chatgpt-threads', JSON.stringify(threads));
- _draw();
- });
- // BUILD THE MENU BUTTON
- let _button = (id, conv_id, active, title) => {
- let svgHistory = $('<?xml version="1.0" encoding="UTF-8"?><svg alt="Chat History" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 2.777344 9.921875 L 2.777344 19.046875 L 13.332031 19.046875 L 15.199219 17.144531 L 17.0625 15.238281 L 17.0625 0.792969 L 2.777344 0.792969 Z M 16.269531 8.136719 L 16.269531 14.683594 L 12.699219 14.683594 L 12.699219 18.253906 L 3.570312 18.253906 L 3.570312 1.585938 L 16.269531 1.585938 Z M 15.476562 15.59375 C 15.476562 15.675781 15.039062 16.152344 14.484375 16.667969 L 13.492188 17.578125 L 13.492188 16.546875 C 13.492188 15.675781 13.691406 15.476562 14.484375 15.476562 C 15.039062 15.476562 15.476562 15.515625 15.476562 15.59375 Z M 15.476562 15.59375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 5.554688 C 5.953125 5.753906 6.230469 5.953125 6.546875 5.953125 C 6.863281 5.953125 7.144531 5.753906 7.144531 5.554688 C 7.144531 5.316406 6.863281 5.160156 6.546875 5.160156 C 6.230469 5.160156 5.953125 5.316406 5.953125 5.554688 Z M 5.953125 5.554688 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 5.554688 C 8.332031 5.792969 9.523438 5.953125 11.109375 5.953125 C 12.699219 5.953125 13.890625 5.792969 13.890625 5.554688 C 13.890625 5.316406 12.699219 5.160156 11.109375 5.160156 C 9.523438 5.160156 8.332031 5.316406 8.332031 5.554688 Z M 8.332031 5.554688 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 7.9375 C 5.953125 8.136719 6.230469 8.332031 6.546875 8.332031 C 6.863281 8.332031 7.144531 8.136719 7.144531 7.9375 C 7.144531 7.699219 6.863281 7.539062 6.546875 7.539062 C 6.230469 7.539062 5.953125 7.699219 5.953125 7.9375 Z M 5.953125 7.9375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 7.9375 C 8.332031 8.175781 9.523438 8.332031 11.109375 8.332031 C 12.699219 8.332031 13.890625 8.175781 13.890625 7.9375 C 13.890625 7.699219 12.699219 7.539062 11.109375 7.539062 C 9.523438 7.539062 8.332031 7.699219 8.332031 7.9375 Z M 8.332031 7.9375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 10.316406 C 5.953125 10.515625 6.230469 10.714844 6.546875 10.714844 C 6.863281 10.714844 7.144531 10.515625 7.144531 10.316406 C 7.144531 10.078125 6.863281 9.921875 6.546875 9.921875 C 6.230469 9.921875 5.953125 10.078125 5.953125 10.316406 Z M 5.953125 10.316406 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 10.316406 C 8.332031 10.554688 9.523438 10.714844 11.109375 10.714844 C 12.699219 10.714844 13.890625 10.554688 13.890625 10.316406 C 13.890625 10.078125 12.699219 9.921875 11.109375 9.921875 C 9.523438 9.921875 8.332031 10.078125 8.332031 10.316406 Z M 8.332031 10.316406 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 12.699219 C 5.953125 12.898438 6.230469 13.09375 6.546875 13.09375 C 6.863281 13.09375 7.144531 12.898438 7.144531 12.699219 C 7.144531 12.460938 6.863281 12.300781 6.546875 12.300781 C 6.230469 12.300781 5.953125 12.460938 5.953125 12.699219 Z M 5.953125 12.699219 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 12.699219 C 8.332031 12.9375 9.523438 13.09375 11.109375 13.09375 C 12.699219 13.09375 13.890625 12.9375 13.890625 12.699219 C 13.890625 12.460938 12.699219 12.300781 11.109375 12.300781 C 9.523438 12.300781 8.332031 12.460938 8.332031 12.699219 Z M 8.332031 12.699219 "/></g></svg>');
- let svgEdit = $('<?xml alt="Edit chat name" version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style="fill:none;stroke-width:30;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 70.078125 422.34375 L 444.375 48.046875 L 551.953125 155.625 L 177.65625 529.921875 L 48.046875 551.953125 Z M 70.078125 422.34375 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 70.546875 417.773438 L 181.171875 528.398438 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 491.484375 108.398438 L 124.804688 475.078125 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 54.257812 507.304688 L 95.15625 546.796875 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/></g></svg>');
- svgHistory.click((e) => {
- e.preventDefault();
- let threads_ = _threads();
- let msgs = threads_.filter(el => el.id == id)[0].messages;
- let popupContainer = $('<div>');
- popupContainer.attr('id', 'pupup_history');
- popupContainer.css({position: 'absolute', width: '100%', height: '100%', left: 0, top: 0, backgroundColor: 'rgba(0,0,0,.5)', zIndex: 999998});
- let close = $('<div>');
- close.css({ position: 'absolute', fontSize: '30px', color: 'white', top: '10px', right: '10px', zIndex: 999999, cursor: 'pointer' });
- close.html('X');
- close.click(() => {
- $('#pupup_history').remove();
- });
- let popup = $('<div>');
- popup.css({overflow: 'auto', position: 'absolute', width: '80%', height: '80%', left: '10%', top: '10%', backgroundColor: '#343541', zIndex: 999999, border: '3px solid #000000', borderRadius: '5px', padding: '20px'});
- for (let msg of msgs) {
- let msgDiv = $('<p>').css({marginBottom: '5px', marginTop: '5px'}).html(msg.replace(/\n/g, '<br />'));
- popup.append(msgDiv).append('<hr>');
- }
- popupContainer.append(close);
- popupContainer.append(popup);
- $($($($('body').children()[0]).children()[1])).prepend(popupContainer)
- });
- svgEdit.click(() => {
- let newT = prompt("Enter the new chat name (length max 20):", title);
- if (newT != null && newT.trim().length < 20) {
- let threads_ = _threads();
- threads_.filter(el => el.id == id)[0].title = newT;
- _setThreads(threads_);
- _draw();
- } else {
- alert('Max length 20');
- }
- });
- let svgDelete = $('<?xml alt="Delete this chat" version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 16.964844 0.742188 L 12.695312 0.742188 L 12.695312 0.640625 C 12.695312 0.347656 12.460938 0.109375 12.167969 0.109375 L 7.8125 0.109375 C 7.519531 0.109375 7.28125 0.347656 7.28125 0.640625 L 7.28125 0.742188 L 3.011719 0.742188 C 2.71875 0.742188 2.484375 0.980469 2.484375 1.273438 L 2.484375 3.363281 C 2.484375 3.65625 2.71875 3.894531 3.011719 3.894531 L 16.964844 3.894531 C 17.257812 3.894531 17.496094 3.65625 17.496094 3.363281 L 17.496094 1.273438 C 17.496094 0.980469 17.257812 0.742188 16.964844 0.742188 Z M 16.964844 0.742188 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 16.167969 4.964844 L 3.808594 4.964844 C 3.515625 4.964844 3.277344 5.203125 3.277344 5.496094 L 3.277344 19.339844 C 3.277344 19.628906 3.515625 19.867188 3.808594 19.867188 L 16.167969 19.867188 C 16.460938 19.867188 16.699219 19.628906 16.699219 19.339844 L 16.699219 5.496094 C 16.699219 5.203125 16.460938 4.964844 16.167969 4.964844 Z M 7.472656 17.960938 C 7.472656 18.253906 7.238281 18.492188 6.945312 18.492188 L 6.453125 18.492188 C 6.160156 18.492188 5.921875 18.253906 5.921875 17.960938 L 5.921875 6.871094 C 5.921875 6.578125 6.160156 6.339844 6.453125 6.339844 L 6.945312 6.339844 C 7.238281 6.339844 7.472656 6.578125 7.472656 6.871094 Z M 10.765625 17.960938 C 10.765625 18.253906 10.527344 18.492188 10.234375 18.492188 L 9.742188 18.492188 C 9.449219 18.492188 9.214844 18.253906 9.214844 17.960938 L 9.214844 6.871094 C 9.214844 6.578125 9.449219 6.339844 9.742188 6.339844 L 10.234375 6.339844 C 10.527344 6.339844 10.765625 6.578125 10.765625 6.871094 Z M 14.054688 17.960938 C 14.054688 18.253906 13.820312 18.492188 13.527344 18.492188 L 13.035156 18.492188 C 12.742188 18.492188 12.503906 18.253906 12.503906 17.960938 L 12.503906 6.871094 C 12.503906 6.578125 12.742188 6.339844 13.035156 6.339844 L 13.527344 6.339844 C 13.820312 6.339844 14.054688 6.578125 14.054688 6.871094 Z M 14.054688 17.960938 "/></g></svg>');
- svgDelete.click(() => {
- if (confirm(`Delete the chat: ${title}?`)) {
- let threads_ = _threads();
- let newThreads = threads_.filter(el => el.id !== id);
- _setThreads(newThreads);
- if (newThreads.length == 0) {
- _init();
- } else {
- _draw();
- }
- }
- });
- let a = $(`<a data-id="${id}" conv-id="${conv_id}" ${active ? 'style="background-color: rgba(255,255,255,.2)"': ''} class="flex py-3 px-3 items-center gap-3 rounded-md hover:bg-gray-500/10 transition-colors duration-200 text-white cursor-pointer text-sm flex-shrink-0 border border-white/20"></a>`);
- a.append(svgHistory);
- a.append(svgEdit);
- a.append(svgDelete);
- a.append(title);
- return a;
- }
- let _draw = (id) => {
- nav.parent().css('overflow', 'auto');
- // BUILD THE LEFT MENU
- var children = nav.children();
- children = children.not(':eq(0)');
- children.remove();
- let threads = _threads();
- for (let t of threads) {
- let btn = _button(t.id,t.conv_id, t.active, t.title);
- btn.click(() => {
- let threads_ = _threads();
- for (let t2 of threads_) {
- t2.active = false;
- }
- threads_.filter(el => el.id == t.id)[0].active = true;
- _setThreads(threads_);
- _draw();
- });
- if (t.active) {
- $('textarea').attr('placeholder', `The chat is now referring to: ${t.title}`);
- }
- newThreadBtn.after(btn);
- }
- // BUILD THE CHAT
- }
- _init();
- // ############## DISABLE MODERATION
- window.originalFetch = window.fetch
- // Then override fetch function with your new function
- window.fetch = async (... args) => {
- let active = _getActive();
- let body = JSON.parse(args[1].body);
- if (args.length > 0 && args[0].includes('moderations')) {
- let lastMessage = active.messages.length > 0 ? active.messages[active.messages.length - 1]: "";
- let msgToAdd = body.input.replace(lastMessage, "");
- active.messages.push(msgToAdd);
- _updateConvIdActive(active);
- body.input = "HI!";
- args[1].body = JSON.stringify(body);
- }
- if (args.length > 0 && args[0].includes('conversation')) {
- if (active.conv_id != 0) {
- body.conversation_id = active.conv_id;
- }
- if (active.parent_id != 0) {
- body.parent_message_id = active.parent_id;
- }
- args[1].body = JSON.stringify(body);
- }
- let response = await window.originalFetch(...args);
- let responseClone = response.clone();
- let reader = responseClone.body.getReader();
- reader.read().then(function processText({ done, value }) {
- let valueDecoded = new TextDecoder().decode(value);
- if (args.length > 0 && args[0].includes('conversation')) {
- let json = valueDecoded.split('data: ')[1];
- if (json != undefined) {
- json = JSON.parse(json);
- let conversation_id = json.conversation_id;
- active.conv_id = conversation_id;
- let parent_message_id = json.message.id;
- active.parent_id = parent_message_id;
- _updateConvIdActive(active);
- }
- }
- });
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement