// ==UserScript== // @name Rizz Your Waifu // @version 1.1.2 // @author kevoting // @description Views deleted messages and auto swipes // @match https://beta.character.ai/* // @icon https://www.google.com/s2/favicons?sz=64&domain=character.ai // @grant none // @run-at document-start // @namespace https://greasyfork.org/users/1077492 // @downloadURL none // ==/UserScript== (function() { const STREAMING_URL = "https://beta.character.ai/chat/streaming/"; const CANCEL_URL = "https://beta.character.ai/chat/history/msgs/cancel/"; const SENTRY_URL = "sentry.io"; const E_VERSION = "v1.1.2"; const fetchFn = window.fetch; const sendSocketfn = window.WebSocket.prototype.send; const PROTOCOL_LEGACY = "LEGACY"; const PROTOCOL_NEO = "NEO"; var current_protocol = PROTOCOL_LEGACY; var readyqueue = []; var last_user_msg_uuid = null; var mainelem = null; var lastheaders = null; var lastbody = null; var override_primary = null; var ishided = false; var activerequests = 0; var req_version = 0; var okmessages = 0; var templates = { "msg" : null }; /*Chat2 support*/ var neo_socket = null; var neo_waiting_for_turn = false; var neo_payload_origin = ""; var neo_capture_next_request = false; var neo_captured_next_request = ""; var neo_readyqueue = []; var neo_requests = new Map(); var neo_last_turn = null; var neo_swiper = null; //https://gist.github.com/scwood/3bff42cc005cc20ab7ec98f0d8e1d59d function uuidV4() { const uuid = new Array(36); for (let i = 0; i < 36; i++) { uuid[i] = Math.floor(Math.random() * 16); } uuid[14] = 4; // set bits 12-15 of time-high-and-version to 0100 uuid[19] = uuid[19] &= ~(1 << 2); // set bit 6 of clock-seq-and-reserved to zero uuid[19] = uuid[19] |= (1 << 3); // set bit 7 of clock-seq-and-reserved to one uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; return uuid.map((x) => x.toString(16)).join(''); } window.WebSocket.prototype.send = function(...args) { try { var json = JSON.parse(args[0]); switch(json.command) { case "create_and_generate_turn": { if (!neo_waiting_for_turn) { neo_payload_origin = json.origin_id; neo_waiting_for_turn = true; let newver = ++req_version; req_version = newver; } current_protocol = PROTOCOL_NEO; try { mainelem.querySelector("#cmode").classList.add("modechanger_disabled"); } catch(ex) { } break; } case "generate_turn_candidate": { if (neo_capture_next_request) { neo_capture_next_request = false; neo_captured_next_request = json.request_id; return; } break; } } } catch (ex) { alert("RIP"); console.error(ex); } if (neo_socket == null) { neo_socket = this; neo_socket.addEventListener("message", neoSocketMessage); neo_socket.addEventListener("close", neoSocketDeadLikeMeIn5Years); } sendSocketfn.call(this, ...args); } function neoSocketMessage(e) { var json = JSON.parse(e.data); if (json.hasOwnProperty("command")) { switch(json.command) { case "add_turn": { if (json.hasOwnProperty("turn") && (!json.turn.author.hasOwnProperty("is_human") || !json.turn.author.is_human)) { if (neo_waiting_for_turn) { neo_waiting_for_turn = false; neo_last_turn = json.turn; neo_swiper = neoSwiperController(); var msgbox = mainelem.querySelector(".details"); msgbox.innerHTML = ""; } } break; } case "update_turn": { var request = neo_requests.get(json.request_id); if (request !== undefined) { request.bubble.websocketEvent(json.command, json); } else { console.log("no request id", json.request_id); } break; } } } } function neoSocketDeadLikeMeIn5Years(e) { neo_socket = null; } function neoSwiperController() { var version = req_version + 0; var state = 0; var data = null; var timeout = new Date().getTime() + (300 * 1000); try { mainelem.querySelector("#cmode").classList.add("modechanger_disabled"); } catch (ex) {} return new Promise((resolve, reject) => { var tmer = null; function check() { if (req_version != version) { resolve(true); return; } if (!neo_capture_next_request) { if ((new Date().getTime()) < timeout) { if (activerequests < 2) { createNeoSwipe(); } } switch(state) { case 0: { if (neo_readyqueue.length > 0) { data = neo_readyqueue.shift(); state = 1; } break; } case 1: { state = 0; data.request_id = neo_captured_next_request; neo_socket.dispatchEvent(new MessageEvent("message", { bubbles: true, cancelable: false, data : JSON.stringify(data)})); break; } } } tmer = setTimeout(check, 100); } tmer = setTimeout(check, 100); }); } function createNeoSwipe() { if (neo_last_turn == null) { return; } var payload = { "command": "generate_turn_candidate", "request_id": uuidV4(), "payload": { "character_id": neo_last_turn.author.author_id, "turn_key": neo_last_turn.turn_key, }, "origin_id": neo_payload_origin }; var bubble = new bubbleDOMController(0); var msgbox = mainelem.querySelector(".details"); msgbox.appendChild(bubble.dom); neo_requests.set(payload.request_id, { "payload" : payload.payload, "bubble" : bubble}); neo_socket.send(JSON.stringify(payload)); activerequests++; } async function handlefetch(...args) { if (args[0] == STREAMING_URL && (mainelem != null)) { current_protocol = PROTOCOL_LEGACY; var json = JSON.parse(args[1].body); var isClean = false; var mode = getCurrentMode(); lastheaders = args[1].headers; lastbody = json; if (json.hasOwnProperty("parent_msg_uuid")) { if (json.parent_msg_uuid == null) { isClean = true; } else { if (json.parent_msg_uuid == last_user_msg_uuid) { if (mode == "nsfw") { alert("Please note that if you swipe then submit the answer, it will become the chosen answer and if you choose the old one again it won't work. You should not use swipes like this with this extension."); } else { if (activerequests == 0) { okmessages = 0; requestSwipes(args[1], req_version); } } return constructAwaiter(); } } if (mode == "nsfw") { //Nothing to do in normal mode if (json.primary_msg_uuid == null) { if (override_primary != null) { json.primary_msg_uuid = override_primary; args[1].body = JSON.stringify(json); override_primary = null; } } } } else { //Weird CAI bug, happens when swipe alert("Dev fuck up, extension dead?"); return constructAwaiter(); } if (isClean) { var firstrequest = tryGetNewMessage(args[1], 0, mode); return constructAwaiter(); } } else { if (args[0].indexOf(SENTRY_URL) != -1) { return new Promise((resolve, reject) => { reject(); }); } } let response = fetchFn(...args); return response; } var f = async (...args) => { return handlefetch(...args); } class EventEmitter { on(name, callback) { var callbacks = this[name]; if (!callbacks) this[name] = [callback]; else callbacks.push(callback); } dispatch(name, event) { var callbacks = this[name]; if (callbacks) callbacks.forEach(callback => callback(event)); } } class bubbleDOMController { constructor(attempt = 0) { this.dom = templates.msg.cloneNode(true); this.botname = this.dom.querySelector(".botname"); this.botmsg = this.dom.querySelector(".botmsg"); this.reqstatus = this.dom.querySelector(".reqstatus"); this.streamcontroller = null; this.errored = false; this.sendedtoui = false; this.stopped = false; this.status = 0; this.lastchunk = null; this.lastdata = null; let btns = this.dom.querySelector(".topbtns").getElementsByClassName("abtn"); for(var i = 0; i < btns.length; i++) { btns[i].addEventListener("click", this.onBtnClick.bind(this)); } this.botname.innerText = ""; this.botmsg.innerText = ""; this.reqstatus.innerText = (attempt > 0) ? ("Got error, retry... x" + attempt) : "Waiting for the server..."; this.updateBtnStatusses(); } assignController(controller) { var self = this; this.streamcontroller = controller; controller.events.on("chunk", function(chunk) { if (self.dom == null) { return; } if (chunk.hasOwnProperty("src_char")) { if (self.status == 0) { self.status = 1; } self.lastchunk = chunk; self.botname.innerText = chunk.src_char.participant.name; self.botmsg.innerText = chunk.replies[0].text; self.updateBtnStatusses(); } }); controller.events.on("result", function(status) { if (self.dom == null) { return; } switch(status.status) { case "errored": { self.status = 2; self.errored = true; self.reqstatus.innerText = status.error; break; } case "done": { if (!self.errored) { self.status = 3; self.reqstatus.innerText = "OK"; } break; } } self.updateBtnStatusses(); }); } websocketEvent(eventType, data) { var self = this; switch(eventType) { case "update_turn": { if (data.hasOwnProperty("turn")) { if (self.status == 0) { self.status = 1; } self.lastdata = data; self.botname.innerText = data.turn.author.name; var finished = false; if (data.turn.candidates[0].hasOwnProperty("safety_truncated")) { self.status = 3; self.reqstatus.innerText = "Filtered"; this.dom.classList.add("warned"); } if (data.turn.candidates[0].hasOwnProperty("raw_content")) { self.botmsg.innerText = data.turn.candidates[0].raw_content; } else { this.dom.classList.remove("warned"); this.dom.classList.add("errored"); self.errored = true; self.status = 2; } if (data.turn.candidates[0].hasOwnProperty("is_final")) { if (self.status == 1) { self.status = 3; self.reqstatus.innerText = "OK"; } finished = true; } if (finished) { activerequests = Math.max(0, activerequests - 1); } self.updateBtnStatusses(); } break; } } } informHTTPError(code) { this.dom.classList.add("errored"); this.reqstatus.innerText = "HTTP " + code; } updateBtnStatusses() { let btns = this.dom.querySelector(".topbtns").getElementsByClassName("abtn"); for(var i = 0; i < btns.length; i++) { btns[i].style.display = "none"; } switch(this.status) { case 1: { //this.dom.querySelector("[data-tag=stopgen]").style.display = "block"; break; } case 2: { this.dom.classList.add("errored"); this.dom.querySelector("[data-tag=remove]").style.display = "block"; break; } case 3: { if (!this.sendedtoui) { this.dom.querySelector("[data-tag=sendui]").style.display = "block"; } break; } case 4: { if (this.stopped) { var btn = this.dom.querySelector("[data-tag=stopgen]"); btn.style.display = "block"; btn.innerText = "Stopped"; } break; } } } selfDestroy() { if (this.dom != null) { this.dom.parentNode.removeChild(this.dom); this.dom = null; } } onBtnClick(e) { var self = this; e.stopImmediatePropagation(); if (neo_waiting_for_turn) { alert("You need to wait m8"); return; } switch(e.target.getAttribute("data-tag")) { case "remove": { self.selfDestroy(); break; } case "sendui": { if (!self.sendedtoui) { self.sendedtoui = true; if (current_protocol == PROTOCOL_LEGACY) { override_primary = self.lastchunk.replies[0].uuid; readyqueue.push(self.streamcontroller.stream); } else { neo_capture_next_request = true; neo_readyqueue.push(self.lastdata); //may cause errors in future document.querySelector(".swiper-button-next").click(); } } break; } case "stopgen": { //Never finished if (!self.stopped) { cancelMessage(lastbody.history_external_id, self.lastchunk.replies[0].uuid, self.lastchunk.replies[0].text.length).then(function() { self.status = 4; }); self.stopped = true; } break; } } self.updateBtnStatusses(); } } function tryGetNewMessage(args, attempt, mode) { var bubble = new bubbleDOMController(attempt); var msgbox = mainelem.querySelector(".details"); msgbox.innerHTML = ""; msgbox.appendChild(bubble.dom); if (attempt == 0) { last_user_msg_uuid = null; } else { var json = JSON.parse(args.body); json.retry_last_user_msg_uuid = last_user_msg_uuid; args.body = JSON.stringify(json); } var req = createAndCancel(args, mode == "nsfw"); let newver = ++req_version; req_version = newver; readyqueue = []; okmessages = 0; req.then(function(result) { bubble.selfDestroy(); if (mode == "sfw") { result.req.then(function(response) { readyqueue.push(response.body); }); } var chunk = result.chunk; var modified = JSON.parse(JSON.stringify(args)); if (!(chunk.last_user_msg_uuid == null) && !(chunk.last_user_msg_uuid == "")) { last_user_msg_uuid = chunk.last_user_msg_uuid; } var modifiedjson = JSON.parse(modified.body); modifiedjson.parent_msg_uuid = chunk.last_user_msg_uuid; modified.body = JSON.stringify(modifiedjson); requestSwipes(modified, newver); }); req.catch(function(chunk) { bubble.selfDestroy(); if (typeof(chunk) == "object" && chunk.hasOwnProperty("last_user_msg_uuid")) { last_user_msg_uuid = chunk.last_user_msg_uuid; setTimeout(function() { tryGetNewMessage(args, attempt + 1, mode); }, Math.min(2000, 5 + (attempt * 10))); } else { alert("Unexpected fatal error. Please reload the page"); } }); return req; } function requestSwipes(body, version) { for(var i = 0; i < 5; i++) { setTimeout(function() { createAndRegister(body, version); }, 1000 + (i * 50)); } } function constructAwaiter() { try { mainelem.querySelector("#cmode").classList.add("modechanger_disabled"); } catch (ex) {} return new Promise((resolve, reject) => { var tmer = null; function check() { if (readyqueue.length > 0) { clearInterval(tmer); let res = new Response(readyqueue.shift(), { "status": 200 }); try { mainelem.querySelector("#cmode").classList.remove("modechanger_disabled"); } catch (ex) {} resolve(res); } } tmer = setInterval(check, 100); }); } function constructStreamController(response) { let reader = response.body.getReader(); let emitter = new EventEmitter(); var lastchunk = null; var chunkcount = 0; let stream = new ReadableStream({ start(controller) { function pump() { reader.read().then(({ done, value }) => { if (done) { controller.close(); if (chunkcount == 0) { emitter.dispatch("result", {"status" : "errored", "error" : "Empty response"}); } else { emitter.dispatch("result", {"status" : "done"}); } return; } let text = new TextDecoder().decode(value); if (chunkcount == 0 && ((text.length == 0) || (text.charCodeAt(0) == 60))) //< { controller.close(); emitter.dispatch("result", {"status" : "errored", "error" : "Unexpected response"}); return; } let clean = text.split(String.fromCharCode(10)); let chunks = []; for (var i = 0; i < clean.length; i++) { try { chunks.push(JSON.parse(clean[i])); chunkcount++; } catch (ex) {} } if (chunks.length != 0) { let chunk = chunks[chunks.length - 1]; if (last_user_msg_uuid != null) { chunk.last_user_msg_uuid = last_user_msg_uuid; } if (chunk.hasOwnProperty("abort")) { emitter.dispatch("result", {"status" : "errored", "error" : chunk.error, "last_user_msg_uuid" : chunk.last_user_msg_uuid}); if (lastchunk != null) { lastchunk.is_final_chunk = true; value = new TextEncoder().encode(JSON.stringify(lastchunk) + String.fromCharCode(13, 10)); emitter.dispatch("chunk", lastchunk); } } else { lastchunk = chunk; value = new TextEncoder().encode(JSON.stringify(lastchunk) + String.fromCharCode(13, 10)); emitter.dispatch("chunk", chunk); if (chunk.is_final_chunk) { emitter.dispatch("result", {"status" : "done"}); } } } controller.enqueue(value); pump(); }) } pump(); } }); return { "stream": stream, "events": emitter }; } function cancelMessage(history,uuid,letters) { let newopts = {}; newopts.method = "POST"; newopts.headers = lastheaders; newopts.body = JSON.stringify({ "history_id" : history, "msg_uuid" : uuid, "num_letters" : letters}); var req = fetchFn(CANCEL_URL, newopts); return req; } function createAndRegister(params, version) { if (version == req_version) { if (activerequests >= 2) { return; } let max = (getCurrentMode() == "nsfw") ? 50 : 10; if (okmessages >= max) { return; } activerequests++; var req = createAndBuild(params); req.then(function(chunk) { activerequests = Math.max(0, activerequests - 1); okmessages++; createAndRegister(params, version); }); req.catch(function(err) { activerequests = Math.max(0, activerequests - 1); if (err.status != 429) { createAndRegister(params, version); } }); } } function createAndBuild(params) { return new Promise((resolve, reject) => { var req = fetchFn(STREAMING_URL, params); var lastchunk = null; var bubble = new bubbleDOMController(); mainelem.querySelector(".details").appendChild(bubble.dom); req.then(function(res) { if (res.ok) { let controller = constructStreamController(res); bubble.assignController(controller); controller.events.on("chunk", function(chunk) { lastchunk = chunk; if (chunk.is_final_chunk) { resolve(chunk); } }); controller.events.on("result", function(result) { switch(result.status) { case "errored": { reject({"status" : 200, "error" : new Error(result.error)}); break; } case "done": { resolve(lastchunk); } } }); } else { bubble.informHTTPError(res.status); reject({"status": res.status, "error" : null}); } }); req.catch(function(error) { reject({"status": 0, "error" : error}); }); }); } function createAndCancel(params, sendcancel) { return new Promise((resolve, reject) => { var req = fetchFn(STREAMING_URL, params); var lastchunk = null; var sendedcancel = !sendcancel; req.then(function(res) { let controller = constructStreamController(res.clone()); controller.events.on("chunk", function(chunk) { lastchunk = chunk; if (chunk.is_final_chunk) { resolve({"chunk": chunk, "req" : req}); } else { if (!sendedcancel) { cancelMessage(lastbody.history_external_id, chunk.replies[0].uuid, 1).catch(function(e) { reject(new Error("Failed to cancel")); }).then(function() { sendedcancel = true }); } } }); controller.events.on("result", function(result) { switch(result.status) { case "errored": { reject(result); break; } case "done": { resolve({"chunk": lastchunk, "req" : req}); } } }); }); req.catch(function(error) { reject(error); }); }); } function getCurrentMode() { var value = mainelem.querySelector('input[name="drone"]:checked').value; return value; } function switchVisibility() { ishided = !ishided; var width = mainelem.clientWidth; if (!ishided) { mainelem.style.transform = ""; } else { mainelem.style.transform = "translateX(" + width + "px)"; } } f.prototype = fetchFn.prototype; window.fetch = f; var timer = null; window.addEventListener("load", function() { timer = setTimeout(function() { try { var style = document.createElement('style'); style.innerHTML = ` .ptrk_main {position: fixed; margin: 0; z-index: 9999; background-color: rgba(33,37,41,0.85); right: 0px; top: 0px; height: 100%; padding: 18px; color: white; font-size: 13px; transition: all 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28); user-select: none; } .ptrk_hide {width: 30px; height: 150px; left: -30px; top: 50%; border-bottom-left-radius: 5px; border-top-left-radius: 5px; position: absolute; cursor: pointer; background-color: rgba(33,37,41,0.85); } .ptrk_hide:after {content: ''; width: 2px; height: 100px; top: 25px; left: 50%; position: absolute; background-color: rgb(59 59 63); } .ptrk_main .modechanger_disabled {opacity: 0.3; pointer-events: none; cursor: no-allowed; } .ptrk_main legend, fieldset {float: initial; line-height: initial; font-size: initial; margin-bottom: initial; padding: initial; width: initial; background: initial; border: initial; border-color: initial; background-color: initial; font-size: 12px; padding-inline-start: 5px; padding-inline-end: 5px; } .ptrk_main label {background: initial; background-color: initial; font-size: 12px; } .ptrk_main fieldset {display: flex; justify-content: center; margin-bottom: 10px; border: 1px solid rgb(59 59 63) !important; border-radius: 3px; font-size: 12px; } .ptrk_main .details {display: relative; overflow-y: scroll; min-width: 400px; max-width: 400px; min-height: 100px; } .ptrk_main .details p {margin: 0; margin-top: 0.5em; font-size: 12px; user-select: text; } .ptrk_main .errored {background: #4f3432 !important; } .ptrk_main .warned {background: #48402e !important; } .ptrk_main .details .mbubble {padding: 10px; border-radius: 3px; margin: 6px; background: rgb(56 59 63); cursor: pointer; position: relative; } .ptrk_main .details .topbtns {position: absolute; width: 100%; right: 10px; display: flex; justify-content: flex-end; height: 30px; } .ptrk_main .details .abtn {padding: 6px; border-radius: 3px; font-weight: bold; z-index: 2; background: rgb(95 99 101); } .ptrk_main .details .abtn:active {background: rgb(118 123 125); } .ptrk_main input[type=radio] {position: absolute; opacity: 0; width: 0; height: 0; } .ptrk_main input[type=radio] + label {filter: grayscale(1.0); background: rgb(56 59 63); border-radius: 2px; padding: 5px; cursor: pointer; display: flex; margin: 3px; min-width: 56px; font-size: 12px; align-items: center; justify-content: center; flex-direction: column; } .ptrk_main input[type=radio]:checked + label {filter: none; background: rgb(77 81 84); !important; } .ptrk_main small {font-size: 12px; } .ptrk_icon {height: 23px; position: absolute; z-index: 9999; background-color: #212529; font-family: 'Arial'; left: 20px; padding: 6px; top: 20px; border-radius: 5px; color: white; opacity: 0.5; transition: all 0.2s; } .ptrk_icon:hover {opacity: 1.0; cursor: pointer } @media (max-width:500px) {.ptrk_main .details {max-width: 300px; min-width: 300px; } } `; document.body.appendChild(style); var maindom = document.createElement('div'); maindom.classList.add("ptrk_main"); maindom.innerHTML = `
Mode
Responses
Send to UI
Remove
Stop gen
%botname

%msg

%STATUS
Stadistics
NaN messages
NaN filter errors
Tracking since %DATE%
0.9.0 `; mainelem = maindom; var msgtemplate = maindom.querySelector(".ptrk_main .details .mbubble"); templates.msg = msgtemplate.cloneNode(true); msgtemplate.parentNode.removeChild(msgtemplate); mainelem.querySelector(".ptrk_hide").addEventListener("click", switchVisibility); mainelem.querySelector("#eversion").innerText = E_VERSION; document.body.appendChild(maindom); } catch (ex) {} }, 1); }); })();