From 4474cc4b6d683faa8367920e163f88c95699a048 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Sun, 15 Dec 2024 22:00:08 -0500 Subject: [PATCH] commandPreprocessor.js now scrapes links out of chat message before sending to server. --- www/js/channel/commandPreprocessor.js | 38 +++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/www/js/channel/commandPreprocessor.js b/www/js/channel/commandPreprocessor.js index 8aded78..186c34d 100644 --- a/www/js/channel/commandPreprocessor.js +++ b/www/js/channel/commandPreprocessor.js @@ -9,20 +9,22 @@ class commandPreprocessor{ this.command = command; this.sendFlag = true; - this.splitBody(); this.processLocalCommand(); - this.sendRemoteCommand(); + + if(this.sendFlag){ + this.message = command; + this.processLinks(); + this.sendRemoteCommand(); + } } - splitBody(){ + processLocalCommand(){ //Create an empty array to hold the command this.commandArray = []; //Split string by words this.commandArray = this.command.split(/\b/g);//Split by word-borders this.argumentArray = this.command.match(/\b\w+\b/g);//Match by words surrounded by borders - } - processLocalCommand(){ //If this is a local command if(this.commandArray[0] == '/'){ //If the command exists @@ -36,10 +38,30 @@ class commandPreprocessor{ } } + processLinks(){ + //Split message by links + var splitMessage = this.message.split(/(https?:\/\/[^\s]+)/g); + //Create an empty array to hold links + this.links = []; + + splitMessage.forEach((chunk, chunkIndex) => { + if(chunk.match(/(https?:\/\/[^\s]+)/g)){ + //I looked online for obscure characters that no one would use to prevent people from chatting embed placeholders + //Then I found this fucker, turns out it's literally made for the job lmao (even if it was originally intended for paper/magnetic tape) + //Replace link with indexed placeholder + splitMessage[chunkIndex] = `␜${this.links.length}␜` + + //push current chunk as link + this.links.push(chunk); + } + }); + + //Join the message back together + this.message = splitMessage.join(''); + } + sendRemoteCommand(){ - if(this.sendFlag){ - this.client.socket.emit("chatMessage",{msg: this.commandArray.join("")}); - } + this.client.socket.emit("chatMessage",{msg: this.message, links: this.links}); }