Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions frontend/js/leaderboard/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,17 @@ function renderLeaderboardRow(user, rank) {
if (rankTagEl) {
nameDiv.appendChild(rankTagEl);
}
const nameTextWrapper = document.createElement("span");
nameTextWrapper.className = "name-text";
nameTextWrapper.appendChild(document.createTextNode(user.name));
const nameLink = document.createElement("a");
nameLink.href = `/user/${encodeURIComponent(user.id)}`;
nameLink.className = "name-text leaderboard-user-link";
nameLink.textContent = user.name;

if (rankChangeEl) {
nameTextWrapper.appendChild(document.createTextNode(" "));
nameTextWrapper.appendChild(rankChangeEl);
nameLink.appendChild(document.createTextNode(" "));
nameLink.appendChild(rankChangeEl);
}

nameDiv.appendChild(nameTextWrapper);
nameDiv.appendChild(nameLink);
row.appendChild(nameDiv);

// Username with link and external icon — id is user-controlled (textContent)
Expand Down Expand Up @@ -314,13 +315,22 @@ function renderMobileCard(user, rank) {
const mobileRankTagEl = createRankTagElement(rank);
const mobileRankChangeEl =
user.score > 0 ? createRankChangeElement(user.rankChange) : null;

if (mobileRankTagEl) {
mobileName.appendChild(mobileRankTagEl);
}
mobileName.appendChild(document.createTextNode(user.name));

const mobileNameLink = document.createElement("a");
mobileNameLink.href = `/user/${encodeURIComponent(user.id)}`;
mobileNameLink.className = "leaderboard-user-link";
mobileNameLink.textContent = user.name;

if (mobileRankChangeEl) {
mobileName.appendChild(mobileRankChangeEl);
mobileNameLink.appendChild(document.createTextNode(" "));
mobileNameLink.appendChild(mobileRankChangeEl);
}

mobileName.appendChild(mobileNameLink);
card.appendChild(mobileName);

// Username — id is user-controlled (textContent)
Expand Down
17 changes: 6 additions & 11 deletions frontend/js/user/profile-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import { loadGoalSetter } from "./goal-setter.js";
import { fetchUserData } from "./historical-graphs.js";
import { loadLeaderboardRanks } from "./ranks.js";
import { loadStreakData } from "./streak.js";

function getUsername() {
const pathSegments = window.location.pathname.split("/");
return (
pathSegments[pathSegments.length - 1] ||
pathSegments[pathSegments.length - 2] ||
""
);
}
import { getCurrentUsername } from "./utils.js";

async function initProfile() {
const username = getUsername();
if (!username) return;
const username = getCurrentUsername();
if (!username) {
console.error("Critical error: Username meta tag is missing.");
return;
}

// Set header display variables prior to fetch if elements exist
const usernameHeading = document.getElementById("username-display");
Expand Down
10 changes: 10 additions & 0 deletions frontend/js/user/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function getCurrentUsername() {
const metaTag = document.querySelector('meta[name="current-user"]');

if (!metaTag || !metaTag.content) {
console.warn("Current user meta tag is missing or empty.");
return null;
}

return metaTag.content.trim();
}
12 changes: 12 additions & 0 deletions frontend/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -3226,6 +3226,18 @@ input[type="checkbox"].compare-checkbox:checked::after {
pointer-events: auto;
}

.leaderboard-user-link {
color: inherit;
text-decoration: none;
}

.leaderboard-user-link:hover,
.leaderboard-user-link:visited,
.leaderboard-user-link:active {
color: inherit;
text-decoration: none;
}

@keyframes compareSlideInRight {
from {
transform: translateX(120%);
Expand Down
1 change: 1 addition & 0 deletions frontend/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="current-user" content="__USERNAME__" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title id="page-title">CodePVG</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
Expand Down
42 changes: 29 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,26 @@ app.use((req, res, next) => {

// 4. HTML page routes — inject per-request nonce into __NONCE__ placeholders
const htmlCache = {};
function serveHtml(res, filePath) {
if (htmlCache[filePath]) {
const html = htmlCache[filePath].replace(/__NONCE__/g, res.locals.nonce);
return res.type("html").send(html);
}

fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
function serveHtml(res, filePath, replacements = {}) {
let template = htmlCache[filePath];

if (!template) {
try {
template = fs.readFileSync(filePath, "utf8");
htmlCache[filePath] = template;
} catch (err) {
return res.status(500).send("Error loading page");
}
htmlCache[filePath] = data;
const html = data.replace(/__NONCE__/g, res.locals.nonce);
res.type("html").send(html);
});
}

let html = template.replace(/__NONCE__/g, res.locals.nonce);

for (const [key, value] of Object.entries(replacements)) {
const regex = new RegExp(key, "g");
html = html.replace(regex, value);
}

res.type("html").send(html);
}

/* HOME ROUTES */
Expand Down Expand Up @@ -133,7 +139,10 @@ app.get("/uptime", (req, res) => {
});

app.get("/user/:username", (req, res) => {
serveHtml(res, path.join(__dirname, "frontend", "user.html"));
const username = req.params.username;
serveHtml(res, path.join(__dirname, "frontend", "user.html"), {
__USERNAME__: username,
});
});

// ---- Rate limiter for API endpoint ----
Expand Down Expand Up @@ -242,6 +251,13 @@ app.get("/api/user/:username", async (req, res) => {
}
});

app.get("/user/:username", (req, res) => {
const username = req.params.username;
serveHtml(res, path.join(__dirname, "frontend", "user.html"), {
__USERNAME__: username,
});
});

// 404 handler
app.use((req, res) => {
res.status(404);
Expand Down
Loading