// ==UserScript==
// @name Chat Generation Progress Bar
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Show progress bar reflecting streamed token activity in chat
// @match ://yuntian-deng-chatgpt.hf.space/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
code
Assistant
// === SETTINGS ===
const DEBOUNCE_MS = 700; // wait this long after last token to consider generation finished
const PROGRESS_STEP = 5; // how much % to increase per token arrival
const MAX_PROGRESS = 95; // max % before finalizing
// === create progress bar elements ===
const barContainer = document.createElement('div');
barContainer.style.position = 'fixed';
barContainer.style.top = '0';
barContainer.style.left = '0';
barContainer.style.width = '100%';
barContainer.style.height = '4px';
barContainer.style.backgroundColor = 'rgba(0,0,0,0.1)';
barContainer.style.zIndex = '9999';
barContainer.style.transition = 'opacity 0.3s ease';
barContainer.style.opacity = '0'; // initially hidden
const bar = document.createElement('div');
bar.style.height = '100%';
bar.style.width = '0%';
bar.style.backgroundColor = '#39ff14'; // bright green
bar.style.transition = 'width 0.3s ease';
barContainer.appendChild(bar);
document.body.appendChild(barContainer);
// === state ===
let progress = 0;
let timeoutId = null;
let active = false;
// === functions ===
function startProgress() {
if (!active) {
active = true;
bar.style.width = '0%';
barContainer.style.opacity = '1';
progress = 0;
}
incrementProgress();
debounceFinish();
}
function incrementProgress() {
if (progress < MAX_PROGRESS) {
progress = Math.min(progress + PROGRESS_STEP, MAX_PROGRESS);
bar.style.width = progress + '%';
}
}
function finishProgress() {
progress = 100;
bar.style.width = '100%';
setTimeout(() => {
barContainer.style.opacity = '0';
bar.style.width = '0%';
active = false;
}, 500);
}
function debounceFinish() {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
finishProgress();
timeoutId = null;
}, DEBOUNCE_MS);
}
// === observe chat container for streamed tokens ===
const chatContainer = document.querySelector('.bubble-wrap.svelte-gjtrl6');
if (!chatContainer) {
console.warn('Chat container not found for progress bar');
return;
}
const observer = new MutationObserver(mutations => {
let tokensAdded = false;
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (!(node instanceof HTMLElement)) continue;
const text = node.textContent.trim();
if (text.length > 0) {
tokensAdded = true;
break;
}
}
if (tokensAdded) break;
}
if (tokensAdded) {
startProgress();
}
});
observer.observe(chatContainer, { childList: true, subtree: true });
})();
Your script nicely adds a progress bar on the page and updates it based on mutations in the chat container, assuming new tokens appear as new DOM nodes inside .bubble-wrap.svelte-gjtrl6.
Here are a few suggestions and potential improvements you might consider:
If this class changes or there are multiple such containers, the script might fail to find the right element or any element at all.
Suggestion:
Check for the container dynamically or allow a short retry if it's missing at first.
Or let user easily configure the selector.
Or observe the whole document and filter relevant mutations.
Example to retry/find later:
js
function getChatContainer() {
return document.querySelector('.bubble-wrap.svelte-gjtrl6');
}
let chatContainer = getChatContainer();
if (!chatContainer) {
// Try finding repeatedly for a few seconds
const retryInterval = setInterval(() => {
chatContainer = getChatContainer();
if (chatContainer) {
clearInterval(retryInterval);
startObserving(chatContainer);
}
}, 500);
} else {
startObserving(chatContainer);
}
function startObserving(container) {
observer.observe(container, { childList: true, subtree: true });
}
2. Better Token Detection Heuristic
Currently any added node with a textContent.trim().length > 0 triggers the progress increment, but this may misfire if unrelated elements add content.
You might want to refine what counts as “new tokens.” For example, if tokens are added as spans inside a single message bubble, or follow specific CSS selectors.
If tokens appear as text content within specific child nodes or elements, monitor those specifically, e.g.:
js
// Inside MutationObserver callback:
for (const node of mutation.addedNodes) {
if (!(node instanceof HTMLElement)) continue;
// For example: check if node matches or contains token styles
if (node.matches('.token-class') || node.querySelector('.token-class')) {
tokensAdded = true;
break;
}
}
Or you can check attributes, classes, or tag names.
3. Smooth Progress Update
The PROGRESS_STEP = 5 means progress increments by 5% per detected token. In a long generation, this might max out quickly and stall.
You could try decrementing the step dynamically to slow progress as it approaches MAX_PROGRESS, or use another easing function.
4. Avoid Multiple Observers or Stopping Observer During Finish
Currently, the MutationObserver never disconnects. This is fine if the page stays alive and tokens come continuously, but if the progress bar finalizes, you might want to pause or disconnect the observer until next generation.
5. Use requestAnimationFrame or CSS Animations
Animations via CSS transitions are good, but requestAnimationFrame for stepping progress might allow smoother progress, if you tie it into real token stream timing.
Complete improved snippet for container selection and observer start:
js
Let me know if you'd like help extending the script for other platforms or adding features like clicking the bar to cancel generation, or handling multiple chats!
(function() {
'use strict';
// === SETTINGS ===
const DEBOUNCE_MS = 700;
const PROGRESS_STEP = 5;
const MAX_PROGRESS = 95;
// --- create progress bar elements ---
const barContainer = document.createElement('div');
barContainer.style.position = 'fixed';
barContainer.style.top = '0';
barContainer.style.left = '0';
barContainer.style.width = '100%';
barContainer.style.height = '4px';
barContainer.style.backgroundColor = 'rgba(0,0,0,0.1)';
barContainer.style.zIndex = '9999';
barContainer.style.transition = 'opacity 0.3s ease';
barContainer.style.opacity = '0';
const bar = document.createElement('div');
bar.style.height = '100%';
bar.style.width = '0%';
bar.style.backgroundColor = '#39ff14';
bar.style.transition = 'width 0.3s ease';
barContainer.appendChild(bar);
document.body.appendChild(barContainer);
let progress = 0;
let timeoutId = null;
let active = false;
function startProgress() {
if (!active) {
active = true;
bar.style.width = '0%';
barContainer.style.opacity = '1';
progress = 0;
}
incrementProgress();
debounceFinish();
}
function incrementProgress() {
if (progress < MAX_PROGRESS) {
progress = Math.min(progress + PROGRESS_STEP, MAX_PROGRESS);
bar.style.width = progress + '%';
}
}
function finishProgress() {
progress = 100;
bar.style.width = '100%';
setTimeout(() => {
barContainer.style.opacity = '0';
bar.style.width = '0%';
active = false;
}, 500);
}
function debounceFinish() {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
finishProgress();
timeoutId = null;
}, DEBOUNCE_MS);
}
// === wait for container ===
function getChatContainer() {
return document.querySelector('.bubble-wrap.svelte-gjtrl6');
}
let chatContainer = getChatContainer();
if (!chatContainer) {
const retryInterval = setInterval(() => {
chatContainer = getChatContainer();
if (chatContainer) {
clearInterval(retryInterval);
initObserver(chatContainer);
}
}, 500);
} else {
initObserver(chatContainer);
}
function initObserver(container) {
const observer = new MutationObserver(mutations => {
let tokensAdded = false;
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (!(node instanceof HTMLElement)) continue;
if (node.textContent.trim().length > 0) {
tokensAdded = true;
break;
}
}
if (tokensAdded) break;
}
if (tokensAdded) {
startProgress();
}
});
observer.observe(container, { childList: true, subtree: true });
}
})();