Show a self-XSS warning in the browser console to protect users from social engineering.
navigator.language with en fallback.en-US -> en).\\n in translations is converted to a real line break in console output.npm install console-self-xss-warning
import { showConsoleWarning } from "console-self-xss-warning";
showConsoleWarning();
Full examples for popular frameworks and Vanilla JS are in examples/.
examples/vanilla/README.mdexamples/react/README.mdexamples/nextjs/README.mdexamples/vue/README.mdexamples/nuxt/README.mdexamples/svetle/README.mdexamples/vite/README.mdexamples/angular/README.mdexamples/astro/README.mdexamples/remix/README.mdexamples/gatsby/README.mdexamples/solid/README.mdexamples/qwik/README.md| parameter | default | required | description |
|---|---|---|---|
translations |
built-in translations | no | override texts and styles per language |
forceLang |
auto-detect (navigator.language) |
no | force a specific language |
once |
true |
no | show only once per page load |
productionOnly |
false |
no | show only in production mode |
clearConsole |
false |
no | clear the console before logging |
productionOnlyEnvKey |
default key list | no | read productionOnly from an env variable |
config |
default warning config | no | override styles and behavior defaults |
Language keys and forceLang accept BCP 47 language tags (e.g. en, en-US, pt-BR).
Tags are normalized to lowercase with -, and if a region-specific tag is not
found the primary subtag is used as a fallback (e.g. en-US -> en).
showConsoleWarning({
once: true, // show only once per page load
clearConsole: false, // optionally clear console before warning
productionOnly: false // gate by production environment
});
showConsoleWarning({
translations: {
en: {
title: "STOP!",
message:
"This is for developers only.\nIf someone asks you to paste code here, it is a scam.",
titleStyle: "color:#d00;font-size:52px;font-weight:900;",
messageStyle: "font-size:16px;"
}
}
});
const translations = {
en: {
title: "STOP!",
message: "Private area.\nDo not paste anything here."
},
fr: {
title: "STOP !",
message: "Zone privée.\nNe collez rien ici."
}
};
showConsoleWarning({
translations,
forceLang: "en",
clearConsole: true
});
| key | default | description |
|---|---|---|
defaultTitleStyle |
color:red;font-size:48px;font-weight:bold; |
default title style |
defaultMessageStyle |
font-size:16px; |
default message style |
defaultSpamIntervalMs |
2000 |
repeat interval for the warning in ms |
devtoolsSizeThresholdPx |
160 |
threshold used to detect open devtools |
If you want productionOnly to be driven by an environment variable, pass
your own key name using productionOnlyEnvKey.
showConsoleWarning({
productionOnlyEnvKey: "MY_APP_CONSOLE_WARNING_PROD_ONLY"
});
You can also pass multiple keys (first match wins):
showConsoleWarning({
productionOnlyEnvKey: ["MY_KEY_1", "MY_KEY_2"]
});
Boolean values accepted: 1/0, true/false, yes/no, on/off
Default keys (if you do not pass anything):
CONSOLE_SELF_XSS_WARNING_PRODUCTION_ONLYVITE_CONSOLE_SELF_XSS_WARNING_PRODUCTION_ONLYNEXT_PUBLIC_CONSOLE_SELF_XSS_WARNING_PRODUCTION_ONLYtype Options = {
translations?: {
[lang: string]: {
title: string
message: string
titleStyle?: string
messageStyle?: string
}
}
forceLang?: string
once?: boolean
productionOnly?: boolean
clearConsole?: boolean
productionOnlyEnvKey?: string | string[]
config?: {
defaultTitleStyle?: string
defaultMessageStyle?: string
defaultSpamIntervalMs?: number
devtoolsSizeThresholdPx?: number
}
}
showConsoleWarning(options?)