Skip to content

Commit

Permalink
Replace method turns out to be faster than the custom function, at le…
Browse files Browse the repository at this point in the history
…ast for simple strings and simple regular expressions.
  • Loading branch information
00Fjongl committed Jul 8, 2024
1 parent c76cf7d commit 106bd07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
51 changes: 13 additions & 38 deletions src/randomization.mjs
Original file line number Diff line number Diff line change
@@ -1,60 +1,35 @@
import pkg from "./routes.mjs";
import { existsSync, readFileSync } from "fs";
export { paintSource, tryReadFile };
const {
cookingInserts,
vegetables,
charRandom,
splashRandom,
cacheBustList,
text404,
} = pkg;
export { insertText, paintSource, tryReadFile };

/*
// Try this instead of the .replace method. Might be more performant.
// Will edit str by replacing all matches of lis with newText.
// Usage: insertText(['<Example1>', '<Example2>'],
// '<Example1> Big Giant Paragraph <Example2> Smol Paragraph',
// stringOrFunctionToGenerateNewText);
*/
const insertText = (lis, str, newText) => {
let position;

// The lis argument should be a list of strings containing placeholders.
// Ensure lis is formatted as a list, and loop through each of the
// placeholder strings.
for (let placeholder of [].concat(lis)) {
// Find all matches of a placeholder string and insert new text there.
while ((position = str.indexOf(placeholder)) != -1)
str =
str.slice(0, position) +
(typeof newText == "function" ? newText() : newText) +
str.slice(position + placeholder.length);
}
return str;
};
cookingInserts,
vegetables,
charRandom,
splashRandom,
cacheBustList,
text404,
} = pkg;

// Below are lots of function definitions used to obfuscate the website.
// This makes the website harder to properly categorize, as its source code
// changes with each time it is loaded.
const randomListItem = (lis) => () => lis[(Math.random() * lis.length) | 0],
charset = ["&#173;", "&#8203;", "&shy;", "<wbr>"],
charset = /&#173;|&#8203;|&shy;|<wbr>/gi,
getRandomChar = randomListItem(charRandom),
insertCharset = (str) => insertText(charset, str, getRandomChar),
insertCharset = (str) => str.replace(charset, getRandomChar),
getRandomSplash = randomListItem(splashRandom),
hutaoInsert = (str) => insertText("<!--HUTAOWOA-->", str, getRandomSplash),
hutaoInsert = (str) => str.replaceAll("<!--HUTAOWOA-->", getRandomSplash),
getCookingText = () =>
`<span style="display:none" data-fact="${randomListItem(vegetables)()}">${randomListItem(cookingInserts)()}</span>`,
insertCooking = (str) =>
insertText(
str.replaceAll(
"<!-- IMPORTANT-HUTAOCOOKINGINSERT-DONOTDELETE -->",
str,
getCookingText
),
// This one isn't for obfuscation; it's just for dealing with cache issues.
cacheBusting = (str) => {
for (let item of Object.entries(cacheBustList))
str = insertText(item[0], str, item[1]);
str = str.replaceAll(item[0], item[1]);
return str;
},
// Apply the final obfuscation changes to an entire file.
Expand Down
20 changes: 9 additions & 11 deletions src/templates.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { insertText, tryReadFile } from "./randomization.mjs";
import { tryReadFile } from "./randomization.mjs";
import path from "path";
export { loadTemplates as default };

Expand All @@ -22,17 +22,15 @@ const header = tryReadFile(
settings = tryReadFile(
path.normalize(__dirname + "/views/pages/misc/deobf/settings.html")
),
loadTemplates = (str) => {
str = insertText("<!--HEADER-->", str, header);
str = insertText("<!--FOOTER-->", str, footer);
loadTemplates = (str) =>
str.replace("<!--HEADER-->", header)
.replace("<!--FOOTER-->", footer)

// Used only on docs.html
str = insertText("<!--DOCS-->", str, documentation);
.replace("<!--DOCS-->", documentation)
// Used only on faq.html
str = insertText("<!--FAQ-->", str, faq);
.replace("<!--FAQ-->", faq)
// Used only on terms.html
str = insertText("<!--TOS-->", str, terms);
// Used only on csel.html
str = insertText("<!--SETTINGS-->", str, settings);
return str;
};
.replace("<!--TOS-->", str, terms)
// Used only on header.html
.replace("<!--SETTINGS-->", str, settings);
4 changes: 2 additions & 2 deletions views/assets/js/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* Card Shimmer Mouse Follow Script
/* ----------------------------------------------- */

// Function declarations
// Function declarations
// Track the cursor position with respect to the top left of the card.
// The "this" keyword gets the element that invoked the event listener.
const handleMouseMove = element => {
Expand All @@ -26,7 +26,7 @@ handleMouseLeave = element => {
});
};

// Query and add event listeners
// Query and add event listeners
const shimmerEffects = document.querySelectorAll(".box-card");

// Attach CSS variables, mouse-x and mouse-y, to elements that will be
Expand Down

0 comments on commit 106bd07

Please sign in to comment.