Files
chrome-dev-formatter/content.js

122 lines
3.6 KiB
JavaScript

(async () => {
console.log("[content] started");
try {
const {
autoFormat,
themeMode,
theme,
langJson,
langMarkdown,
langPlaintext,
langXml,
langYaml
} = await chrome.storage.sync.get({
autoFormat: false,
themeMode: null,
theme: null,
langJson: true,
langMarkdown: false,
langPlaintext: false,
langXml: false,
langYaml: false
});
console.log("[content] autoFormat =", autoFormat);
const desiredTheme = themeMode || theme || "system";
let resolvedTheme = desiredTheme;
if (desiredTheme === "system") {
resolvedTheme =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
document.documentElement.dataset.jsonTheme = resolvedTheme;
window.__jsonFormatterTheme = resolvedTheme;
// Für manuellen Modus: Immer versuchen, wenn explizit geinjected
// Für Auto: Nur bei autoFormat=true
const rawText = document.body.innerText ?? "";
const rawTrim = rawText.trim();
const isLikelyJsonBody =
document.body &&
document.body.children.length === 1 &&
document.body.firstChild &&
document.body.firstChild.nodeType === Node.TEXT_NODE &&
/^[\[{]/.test(rawTrim) && /[\]}]$/.test(rawTrim);
// Alternative, zuverlässigere Erkennung (Chrome setzt contentType korrekt):
const contentType = (document.contentType || "").toLowerCase();
const isJsonByContentType = contentType.includes("json");
console.log("[content] detect:", { isLikelyJsonBody, isJsonByContentType });
const enabledLanguages = {
json: Boolean(langJson),
markdown: Boolean(langMarkdown),
plaintext: Boolean(langPlaintext),
xml: Boolean(langXml),
yaml: Boolean(langYaml)
};
let languageHint = null;
if (enabledLanguages.json && (isLikelyJsonBody || isJsonByContentType)) {
languageHint = "json";
} else if (enabledLanguages.xml && contentType.includes("xml")) {
languageHint = "xml";
} else if (
enabledLanguages.yaml &&
(contentType.includes("yaml") || contentType.includes("yml"))
) {
languageHint = "yaml";
} else if (enabledLanguages.markdown && contentType.includes("markdown")) {
languageHint = "markdown";
} else if (
enabledLanguages.plaintext &&
contentType.includes("text/plain")
) {
languageHint = "plaintext";
}
// Bei manuellem Trigger: immer formatieren, wenn parsebar
// Bei Auto: nur wenn autoFormat=true und JSON-Seite
const shouldAuto = autoFormat && Boolean(languageHint);
const forceManual = Boolean(window.__forceManualFormat);
if (!shouldAuto && !forceManual) {
// Für manuelles Triggern setzen wir Flag von background (siehe unten optional)
console.log("[content] no auto; waiting for manual");
return;
}
// Versuche zu formatieren
const source = rawText || document.body.textContent || "";
if (!source) {
console.log("[content] no source text");
return;
}
try {
const formatted = await applyFormatting(source, {
enabledLanguages,
languageHint,
contentType,
allowAutoDetect: forceManual && !languageHint
});
if (formatted) {
console.log("[content] formatted");
} else {
console.log("[content] no supported format");
}
} catch (e) {
console.warn("[content] format error", e);
} finally {
if (forceManual) {
window.__forceManualFormat = false;
}
}
} catch (e) {
console.error("[content] fatal", e);
}
})();