Skip to content

Commit

Permalink
Make links in OOC chat clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
OmniTroid committed Nov 18, 2023
1 parent d5c5cc6 commit 8a3a7bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ <h3 id="client_version">version</h3>
<template id="ooc">
<meta name="frame-title" lang="en" content="Server">
<div style="height: 100%; display: flex; flex-direction: column;">
<textarea id="client_ooclog" style="flex: 1 auto" readonly></textarea>
<div id="client_ooclog" style="flex: 1 auto" readonly></div>
<span id="client_oocinput">
<input id="OOC_name" name="OOC_name" type="text">
<input id="client_oocinputbox" type="text" onkeypress="onOOCEnter(event)">
Expand Down
15 changes: 13 additions & 2 deletions webAO/packets/handlers/handleCT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ const { mode } = queryParser();
export const handleCT = (args: string[]) => {
if (mode !== "replay") {
const oocLog = document.getElementById("client_ooclog")!;
oocLog.innerHTML += `${prepChat(args[1])}: ${prepChat(args[2])}\r\n`;
const username = prepChat(args[1]);
let message = addLinks(prepChat(args[2]));
// Replace newlines with br
message = message.replace(/\n/g, "<br>");

oocLog.innerHTML += `${username}: ${message}<br>`;
if (oocLog.scrollTop > oocLog.scrollHeight - 600) {
oocLog.scrollTop = oocLog.scrollHeight;
}
}
}
}

// If the incoming message contains a link, add a href hyperlink to it
function addLinks(message: string) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
return message.replace(urlRegex, (url) => `<a href="${url}" target="_blank">${url}</a>`);
}

0 comments on commit 8a3a7bb

Please sign in to comment.