From 09c95032fa30c050674a58ff7fe44cd6df4d7a79 Mon Sep 17 00:00:00 2001 From: foxster-mp4 Date: Sat, 29 Apr 2023 16:26:13 -0700 Subject: [PATCH] Better URL regex --- js/app.js | 22 +++------------------- js/home.js | 20 ++++++-------------- js/main.js | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/js/app.js b/js/app.js index f7fab0a..e9323f2 100644 --- a/js/app.js +++ b/js/app.js @@ -91,16 +91,7 @@ fetch(sourceURL) previewScreenshots.insertAdjacentHTML("beforeend", ``); }); - let localizedDescription = app.localizedDescription; - - const perviewDescriptionURLs = [...new Set(localizedDescription.match(urlRegex))]; // Creating set from array to remove duplicates - - perviewDescriptionURLs.forEach(url => { - localizedDescription = localizedDescription.replaceAll(url, `${url}`) - }); - - - previewDescription.innerHTML = localizedDescription.replaceAll("\n", "
"); + previewDescription.innerHTML = formatString(app.localizedDescription); const more = ` @@ -139,20 +130,13 @@ fetch(sourceURL) const units = ["B", "KB", "MB", "GB"]; var appSize = app.size, c = 0; while (appSize > 1024) { - appSize = parseFloat(appSize/1024).toFixed(1); + appSize = parseFloat(appSize / 1024).toFixed(1); c++; } versionSize.textContent = `${appSize} ${units[c]}`; // Version description - var appVersionDescription = app.versionDescription; - const urls = [...new Set(appVersionDescription.match(urlRegex))]; // Creating set from array to remove duplicates - - urls.forEach(url => - appVersionDescription = appVersionDescription.replaceAll(url, `${url}`) - ); - - versionDescription.innerHTML = appVersionDescription.replaceAll("\n", "
"); + versionDescription.innerHTML = formatString(app.versionDescription); if (versionDescription.scrollHeight > versionDescription.clientHeight) versionDescription.insertAdjacentHTML("beforeend", more); diff --git a/js/home.js b/js/home.js index c1da505..2dbd6ac 100644 --- a/js/home.js +++ b/js/home.js @@ -14,11 +14,11 @@ fetch(sourceURL) // If b < a return (new Date(b.versionDate)).valueOf() - (new Date(a.versionDate)).valueOf(); }); - + if (json.news && json.news.length >= 1) { // Sort news in decending order (latest first) json.news.sort((a, b) => (new Date(b.date)).valueOf() - (new Date(a.date)).valueOf()); - + // News if (json.news.length == 1) { document.getElementById("news-items").insertAdjacentHTML("beforeend", newsItemHTML(json.news[0], json.apps, true)); @@ -27,7 +27,7 @@ fetch(sourceURL) for (let i = 0; i < 5 && i < json.news.length; i++) { document.getElementById("news-items").insertAdjacentHTML("beforeend", newsItemHTML(json.news[i], json.apps, true)); } - } + } } else { document.getElementById("news").remove(); } @@ -36,7 +36,7 @@ fetch(sourceURL) let count = 1; json.apps.forEach(app => { // Max: 3 featured apps if not specified - if (count > 3) return; + if (count > 3) return; // Ignore beta apps if (app.beta) return; @@ -49,24 +49,16 @@ fetch(sourceURL) count++; }); - var description = json.description; - + var description = formatString(json.description); if (description) { - const urls = [...new Set(description.match(urlRegex))]; // Creating set from array to remove duplicates - - urls.forEach(url => - description = description.replaceAll(url, `${url}`) - ); - document.getElementById("about").insertAdjacentHTML("beforeend", `
-

${description.replaceAll("\n", "
")}

+

${description}

`); } else { document.getElementById("about").remove(); } - waitForAllImagesToLoad(); }); diff --git a/js/main.js b/js/main.js index a30c5c8..44c2963 100644 --- a/js/main.js +++ b/js/main.js @@ -7,8 +7,18 @@ if (!urlSearchParams.has('source') || !sourceURL) { window.location.replace("index.html"); } -// https://stackoverflow.com/a/31760088 -const urlRegex = /(https?:\/\/[^ ]*)/g; // "g": global flag; without this, match() returns only the first matching result +const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; // https://stackoverflow.com/a/8943487 +function formatString(string) { + if (!string) return undefined; + + // URLs + const urlArray = string.match(urlRegex); + const urlSet = [...new Set(urlArray)]; // Converting to set to remove duplicates + urlSet.forEach(url => string = string.replaceAll(url, `${url}`)); + + // New lines + return string.replaceAll("\n", "
"); +} // If source is not a URL if (!sourceURL.match(urlRegex)) {