Remove hard-coded upstream tracking

This commit is contained in:
2026-08-25 16:24:51 +00:00
parent b956829560
commit 52969fd1a8
4 changed files with 102 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
import { readFileSync, writeFileSync } from "node:fs";
const appPath = "src/App.js";
let source = readFileSync(appPath, "utf8");
const analyticsStart = " const [cookieSettings, setCookieSettings] = useState(localStorage.getItem(\"cookie-settings\"));";
const analyticsEnd = " const hasSpeedDial =";
const consentStart = " {!window.isElectron && cookieSettings === null && !sidebarForceHidden && (";
const consentEnd = " <SimpleBar ";
const hasKnownAnalytics = source.includes("G-SLZ5TY9MVN");
const hasKnownState = source.includes(analyticsStart);
const hasKnownConsent = source.includes(consentStart);
if (hasKnownState && hasKnownConsent) {
const analyticsStartIndex = source.indexOf(analyticsStart);
const analyticsEndIndex = source.indexOf(analyticsEnd, analyticsStartIndex);
const consentStartIndex = source.indexOf(consentStart);
const consentEndIndex = source.indexOf(consentEnd, consentStartIndex);
if (analyticsEndIndex === -1 || consentEndIndex === -1) {
throw new Error("Cannot locate the complete upstream Analytics implementation.");
}
source = source.slice(0, analyticsStartIndex) + source.slice(analyticsEndIndex);
const updatedConsentStartIndex = source.indexOf(consentStart);
const updatedConsentEndIndex = source.indexOf(consentEnd, updatedConsentStartIndex);
source = source.slice(0, updatedConsentStartIndex) + source.slice(updatedConsentEndIndex);
const materialImport = "Typography, Button, Grid";
if (!source.includes(materialImport)) {
throw new Error("Cannot remove the Button import used by the upstream consent dialog.");
}
source = source.replace(materialImport, "Typography, Grid");
console.log("Removed the upstream Google Analytics integration and consent dialog.");
} else if (hasKnownAnalytics || hasKnownState || hasKnownConsent) {
throw new Error("Unsupported upstream Analytics implementation.");
} else {
console.log("Upstream does not contain the known Analytics implementation; skip removal.");
}
const unsupportedAnalytics = [
/googletagmanager\.com/i,
/google-analytics\.com/i,
/\bgtag\s*\(/,
/\bdataLayer\b/,
/cookie-settings/,
];
if (unsupportedAnalytics.some(pattern => pattern.test(source))) {
throw new Error("An unsupported Analytics implementation remains in src/App.js.");
}
writeFileSync(appPath, source);
+32
View File
@@ -0,0 +1,32 @@
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import { extname, join } from "node:path";
const sourceRoot = "src";
const upstreamAvatar = "https://charlws.com/me.gif";
const localAvatar = "/logo.png";
const sourceExtensions = new Set([".js", ".jsx", ".ts", ".tsx"]);
let replacementCount = 0;
function updateDirectory(directory) {
for (const entry of readdirSync(directory, { withFileTypes: true })) {
const path = join(directory, entry.name);
if (entry.isDirectory()) {
updateDirectory(path);
} else if (sourceExtensions.has(extname(entry.name))) {
const source = readFileSync(path, "utf8");
const occurrences = source.split(upstreamAvatar).length - 1;
if (occurrences > 0) {
writeFileSync(path, source.replaceAll(upstreamAvatar, localAvatar));
replacementCount += occurrences;
}
}
}
}
updateDirectory(sourceRoot);
if (replacementCount === 0) {
console.log("Upstream does not contain the known fallback avatar; skip replacement.");
} else {
console.log(`Replaced ${replacementCount} upstream fallback avatar reference(s).`);
}