ChatGPT Text File Scaler

ChatGPT utility to cut and transfer text files

Fra 20.03.2023. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         ChatGPT Text File Scaler
// @version      1.0
// @author       refracta
// @description  ChatGPT utility to cut and transfer text files
// @match        https://chat.openai.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @license      MIT
// @namespace https://greasyfork.org/users/467840
// ==/UserScript==

(async function () {
    'use strict';
	localStorage.buffer = localStorage.buffer ? localStorage.buffer : '';
    function waitFor(valueFunction, inspectPeriod = 100) {
        return new Promise(resolve => {
            let interval = setInterval(async _ => {
                try {
                    let value = valueFunction();
                    value = value instanceof Promise ? await value : value;
                    if (value) {
                        clearInterval(interval);
                        resolve(value);
                    }
                } catch (e) {}
            }, inspectPeriod);
        });
    }

    function writeMessage(text) {
        document.querySelector('textarea').value = text;
    }

    function clickSendButton() {
        document.querySelector('button.absolute').click();
    }

    async function waitResponse() {
        await waitFor(_ => document.querySelector('button.absolute.p-1 > svg'));
    }

    function clearMessages(messageLimit) {
        let messages = Array.from(document.querySelectorAll('.flex-1.overflow-hidden > div > div > div > div:nth-child(1) > div'));
        while (messages.length > messageLimit) {
            messages.shift().remove();
        }
    }

    async function sendMessage(text) {
        writeMessage(text);
        clickSendButton();
        await waitResponse();
    }

    function getText() {
        return new Promise(resolve => {
            let input = document.createElement("input");
            input.type = "file";
            input.accept = "text/plain";
            input.onchange = (event) => {
                let file = event.target.files[0];
                let reader = new FileReader();
                reader.onload = () => {
                    resolve(reader.result);
                };
                reader.readAsText(file);
            };
            input.click();
        });
    }

    function createElement(html) {
        var div = document.createElement('div');
        div.innerHTML = html.trim();
        return div.firstChild;
    }

    function updateLoadTextFileSpan() {
        let loadTextFileSpan = document.querySelector('#load-text-file');
        if (loadTextFileSpan) {
            loadTextFileSpan.textContent = `Load text file (${localStorage.buffer.length})`;
        }
    }

    async function sendTextFile() {
        const firstMessage = 'I am going to provide you a book in multiple messages.';
        const insertMessage = 'Here is the next page, please do not respond aside from confirmation:\n\n';
        const splitLength = 2000;
        const messageLimit = 30;

        await sendMessage(firstMessage);
        while (localStorage.buffer.length > 0) {
            let length = splitLength - insertMessage.length;
            let text = localStorage.buffer.slice(0, length);
            await sendMessage(insertMessage + text);
            localStorage.buffer = localStorage.buffer.slice(length);
            updateLoadTextFileSpan();
            clearMessages(messageLimit);
        }
    }

    let isUIProcessing = false;
    setInterval(async _ => {
        if (!document.querySelector('#load-text-file') && !isUIProcessing) {
            isUIProcessing = true;
            let loadTextFileButton = createElement(`<a href="#" 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"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg><span id="load-text-file">Load text file  (${localStorage.buffer.length})</span></a>`);
            loadTextFileButton.addEventListener('click', async() => {
                localStorage.buffer = await getText();
                updateLoadTextFileSpan();
            });

            let sendTextFileButton = createElement(`<a href="#" 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"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>Send text file</a>`);
            sendTextFileButton.addEventListener('click', sendTextFile);

            let clearConversationButton = await waitFor(_ => Array.from(document.querySelectorAll('.py-3')).filter(e => e.textContent === 'Clear conversations')[0]);
            clearConversationButton.parentElement.insertBefore(loadTextFileButton, clearConversationButton);
            clearConversationButton.parentElement.insertBefore(sendTextFileButton, clearConversationButton);
            isUIProcessing = false;
        }
    }, 100);
})();