@_redsocs/spam-warden
v0.69.1
Published
Lightweight client-side JavaScript library for real-time spam detection. Trained on Thai spam data using Bernoulli Naive Bayes.
Downloads
316
Maintainers
Readme
SpamWarden.js
Lightweight, client-side JavaScript library for detecting spam and sentence hijacking in real-time. Trained on the model from RedSocs/spam-labeler, bundled for zero-dependency browser usage.
Quick Start
Install
# npm
npm install @_redsocs/spam-warden
# Or download from CDN / GitHub releasesIn the Browser
<!-- Option 1: From RedSocs CDN -->
<script src="https://redsocs.com/js/spam-warden.js"></script>
<!-- Option 2: Self-hosted -->
<script src="dist/spamwarden.min.js"></script>
<script>
const result = window.spamwarden.spamcheck("สมัครสมาชิกวันนี้ รับโบนัส ฟรี!");
console.log(result.isSpam); // true
console.log(result.prob); // 1.0
console.log(result.version); // "v0.69"
</script>As ES Module
import SpamWarden from './dist/spamwarden.min.js';
SpamWarden.spamcheck("Welcome bonus! Deposit now");Quick Boolean Check
if (spamwarden.isSpam(userInput)) {
// block or flag
}In Node.js
const spamwarden = require("./dist/spamwarden.min.js");
const r = spamwarden.spamcheck("Welcome bonus! Deposit now get 200% match");
console.log(r.isSpam); // trueAPI
spamwarden.spamcheck(text) → object
| Field | Type | Description |
|-------|------|-------------|
| isSpam | boolean | true if detected as spam |
| prob | number | Spam probability (0.0–1.0) |
| reason | string? | Present if hard-rule triggered: "currency_symbol" or "spam_link" |
| version | string | Model version (e.g., "v0.69") |
spamwarden.isSpam(text) → boolean
Convenience wrapper — returns only the boolean result.
spamwarden.version → string
Current model version string.
Build
# 1. Copy model from spam-labeler
cp ../spam-labeler/extension/model.json .
# 2. Build (bundles model into JS)
node build.js
# or: ./build.shOutput:
| File | Size |
|------|------|
| dist/spamwarden.js | 3.5 MB (uncompressed) |
| dist/spamwarden.min.js | 61 KB (minified) |
| dist/spamwarden.min.js (gzipped) | 27 KB |
Optional: Better Minification
npm install terser
node build.js # now uses terser instead of simple minificationHow It Works
User posts text
↓
spamwarden.spamcheck(text)
↓
Hard rules check (currency symbols, spam links)
↓
Vectorizer: whitespace tokens + trigrams + quadgrams
↓
Bernoulli Naive Bayes prediction (class 0 = safe, 1 = spam)
↓
Softmax → probability
↓
{ isSpam, prob, version }Model
| Property | Value |
|----------|-------|
| Origin | RedSocs/spam-labeler (Rust, Bernoulli NB) |
| Features | ~63,000 tokens (whitespace + trigrams + quadgrams) |
| Version | v0.69 (680 training samples) |
| Hard Rules | Currency symbols ($€£฿) → auto-spam; Spam links (line.me, @line, lin.ee) → auto-spam |
Train Your Own Model
The model in this repo was trained by RedSocs/spam-labeler. To customize for your own use case:
# 1. Clone the training repo
git clone https://github.com/RedSocs/spam-labeler.git
# 2. Add your own training data
cp your-spam.txt spam-labeler/data/spam.txt
cp your-safe.txt spam-labeler/data/safe.txt
# 3. Retrain and export
cd spam-labeler
cargo run --release --bin export_model
cp extension/model.json ../spam-warden/model.json
# 4. Rebuild SpamWarden
cd ../spam-warden
node build.jsSee the spam-labeler README for the full training pipeline.
Privacy
All processing happens in-memory in the browser. No data is sent to any server.
Related
- RedSocs/spam-labeler — Rust-based training pipeline, TUI app, and Firefox extension for collecting and training the spam detection model.
Project Structure
spam-warden/
├── src/
│ └── spamwarden.js # Library source (MODEL_DATA_PLACEHOLDER)
├── dist/
│ ├── spamwarden.js # Bundled (model inlined, ~3.5 MB)
│ └── spamwarden.min.js # Minified for production (~61 KB, 27 KB gzipped)
├── model.json # Trained model from spam-labeler
├── build.js # Node.js build script
├── build.sh # Shell wrapper
└── README.md # This file