I am making a request to a SSE stream, using a website. In the response, it starts with…
- User
I am making a request to a SSE stream, using a website. In the response, it starts with : joining queue
data: {"id":"chatcmpl-7Fa82BsbBoDIy6DfrZWy4sOxJU1ZG","object":"chat.completion.chunk - the : joining queue messes up the pre-existing request interpreter in my browser. Is it possible to create an userscript to automatically hide the ": joining queue" before chrome processes it?
- Assistant
Yes, you can create a userscript to modify the server-sent events (SSE) stream and filter out the "joining queue" message. Here's an example of how to create a userscript to do this using JavaScript:
- Install a userscript manager extension for your browser like Tampermonkey.
- Open the Tampermonkey dashboard and create a new script.
- Paste the following code into the editor and modify the
@matchvalue to match the website you are using:
// ==UserScript== // @name Filter SSE Stream // @namespace http://tampermonkey.net/ // @version 1.0 // @description Filters out the 'joining queue' message from a SSE stream // @author You // @match https://your-website.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // Replace the EventSource with a custom version that filters events window.EventSource = class CustomEventSource extends EventSource { constructor(url, options) { super(url, options); this.addFilteredEventListener(); } addFilteredEventListener() { const originalAddEventListener = this.addEventListener; this.addEventListener = (type, callback, ...args) => { const filteredCallback = (event) => { const message = event.data ? event.data.trim() : ''; if (message.includes(': joining queue')) { console.log('Filtered message:', message); } else { callback(event); } }; originalAddEventListener.call(this, type, filteredCallback, ...args); }; } }; })();- Save the script and reload the website in your browser. The userscript should automatically run and hide the ": joining queue" messages in the SSE stream.
Please note that this solution assumes the website uses the native
EventSourceAPI for SSE. If the website uses a different library or method, you will need to adapt the script accordingly.