π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
feat: smart blur β offscreen port messaging, blur panel, AI PII detection
Luis Ortizdb5e6da85b2c
diff --git a/package.json b/package.jsonindex 23fa8a5..d21ca94 100644--- a/package.json+++ b/package.json@@ -12,7 +12,7 @@"lint:fix": "biome check --write .","format": "biome format --write .","prepare": "husky",- "postinstall": "mkdir -p public/transformers && cp node_modules/onnxruntime-web/dist/*.wasm public/transformers/ 2>/dev/null || true"+ "postinstall": "mkdir -p public/transformers && find node_modules -path '*/onnxruntime-web/dist/*.wasm' -exec cp {} public/transformers/ \\; && find node_modules -path '*/onnxruntime-web/dist/ort-wasm-simd*.mjs' -exec cp {} public/transformers/ \\; || true"},"keywords": [],"author": "",diff --git a/src/core/blur/manager.ts b/src/core/blur/manager.tsindex b55ebc6..0065e35 100644--- a/src/core/blur/manager.ts+++ b/src/core/blur/manager.ts@@ -84,16 +84,25 @@ export class BlurManager {this.active = false;}) as EventListener;+ private dispatchAiStatus(message: string) {+ document.dispatchEvent(new CustomEvent('mimik-blur:ai-status', { detail: { message } }));+ }+private onToggleAi = ((e: CustomEvent<{ enabled: boolean }>) => {if (e.detail.enabled) {sendMessage('blurAiDetect', { text: document.body.innerText }).then((res) => {- if (res?.patterns) {+ if (res?.patterns?.length) {const regexes = res.patterns.map((p) => new RegExp(p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'));this.scanner.setAiPatterns(regexes);+ this.dispatchAiStatus(`${res.patterns.length} entities found`);+ } else {+ this.dispatchAiStatus('No PII detected');}})- .catch(() => {});+ .catch(() => {+ this.dispatchAiStatus('Detection failed');+ });} else {this.scanner.setAiPatterns([]);}diff --git a/src/core/blur/panel.ts b/src/core/blur/panel.tsindex ced9b23..ef3b499 100644--- a/src/core/blur/panel.ts+++ b/src/core/blur/panel.ts@@ -228,6 +228,30 @@ const STYLES = `}.btn-done:hover { opacity: 0.9; }++ .ai-status {+ font-size: 11px;+ font-weight: 500;+ color: #92400E;+ display: flex;+ align-items: center;+ gap: 6px;+ padding: 0 2px;+ }++ .spinner {+ width: 12px;+ height: 12px;+ border: 2px solid #E8E2DA;+ border-top-color: #F59E0B;+ border-radius: 50%;+ animation: spin 0.8s linear infinite;+ flex-shrink: 0;+ }++ @keyframes spin {+ to { transform: rotate(360deg); }+ }`;function icon(name: 'close' | 'blur' | 'cursor'): string {@@ -255,7 +279,6 @@ export class BlurPanel {mount() {this.host = document.createElement('div');this.host.setAttribute('data-mimik-ignore', '');- this.host.setAttribute('data-mimik-ignore', '');const shadow = this.host.attachShadow({ mode: 'closed' });@@ -385,12 +408,26 @@ export class BlurPanel {label.textContent = 'AI Detection';row.appendChild(label);+ const status = document.createElement('div');+ status.className = 'ai-status';+const toggle = this.buildToggle(this.aiEnabled, (checked) => {this.aiEnabled = checked;+ if (checked) {+ status.innerHTML = '<div class="spinner"></div> Loading model...';+ } else {+ status.textContent = '';+ }document.dispatchEvent(new CustomEvent('mimik-blur:toggle-ai', { detail: { enabled: checked } }));});row.appendChild(toggle);section.appendChild(row);+ section.appendChild(status);++ document.addEventListener('mimik-blur:ai-status', ((e: CustomEvent<{ message: string }>) => {+ status.innerHTML = '';+ status.textContent = e.detail.message;+ }) as EventListener);return section;}diff --git a/src/entrypoints/background/index.ts b/src/entrypoints/background/index.tsindex 5534560..bcae6c1 100644--- a/src/entrypoints/background/index.ts+++ b/src/entrypoints/background/index.ts@@ -11,15 +11,64 @@ import { registerNavigationListeners } from './navigation';import { handleCaptureStep, handleFinalizeInputStep, handleUpdateInputStep } from './step-pipeline';import { broadcastStartCapture, broadcastStopCapture, showNotificationOnTab } from './tab-manager';-async function ensureOffscreen() {- const contexts = await chrome.offscreen.getContexts({- contextTypes: ['OFFSCREEN_DOCUMENT' as chrome.offscreen.ContextType],+let offscreenPort: chrome.runtime.Port | null = null;++async function ensureOffscreen(): Promise<chrome.runtime.Port> {+ if (offscreenPort) return offscreenPort;++ const portReady = new Promise<void>((resolve) => {+ const listener = (port: chrome.runtime.Port) => {+ if (port.name === 'mimik-offscreen') {+ chrome.runtime.onConnect.removeListener(listener);+ offscreenPort = port;+ offscreenPort.onMessage.addListener((msg) => {+ if (msg.type === 'progress') {+ localStorage.set({ blurAiProgress: msg.progress });+ }+ if (msg.type === 'log') {+ logger.info('[OFFSCREEN]', msg.message);+ }+ });+ offscreenPort.onDisconnect.addListener(() => {+ offscreenPort = null;+ });+ resolve();+ }+ };+ chrome.runtime.onConnect.addListener(listener);});- if (contexts.length > 0) return;- await chrome.offscreen.createDocument({- url: 'offscreen/index.html',- reasons: ['WORKERS' as chrome.offscreen.Reason],- justification: 'AI PII detection via Transformers.js',++ try {+ await chrome.offscreen.createDocument({+ url: 'offscreen.html',+ reasons: ['WORKERS' as chrome.offscreen.Reason],+ justification: 'AI PII detection via Transformers.js',+ });+ } catch {}++ await portReady;+ return offscreenPort!;+}++let offscreenMsgId = 0;+function sendToOffscreen(type: string, data: Record<string, unknown> = {}): Promise<any> {+ return ensureOffscreen().then((port) => {+ return new Promise((resolve, reject) => {+ const id = ++offscreenMsgId;+ const timeout = setTimeout(() => {+ port.onMessage.removeListener(handler);+ reject(new Error('Offscreen timeout'));+ }, 120000);+ const handler = (msg: any) => {+ if (msg.id === id) {+ clearTimeout(timeout);+ port.onMessage.removeListener(handler);+ resolve(msg);+ }+ };+ port.onMessage.addListener(handler);+ port.postMessage({ type, id, ...data });+ });});}@@ -232,14 +281,15 @@ export default defineBackground(() => {});onMessage('blurAiDetect', async ({ data }) => {+ logger.info('[BLUR-AI] ensuring offscreen');await ensureOffscreen();- try {- const res: any = await chrome.runtime.sendMessage({ type: 'offscreen:ai:detect', text: data.text });- if (res?.entities) {- const patterns = res.entities.map((e: { text: string }) => e.text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));- return { patterns };- }- } catch {}+ logger.info('[BLUR-AI] sending detect to offscreen, text length:', data.text?.length);+ const res = await sendToOffscreen('offscreen:ai:detect', { text: data.text });+ logger.info('[BLUR-AI] offscreen response:', res);+ if (res?.entities) {+ const patterns = res.entities.map((e: { text: string }) => e.text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));+ return { patterns };+ }return { patterns: [] };});});diff --git a/src/entrypoints/offscreen/main.ts b/src/entrypoints/offscreen/main.tsindex 5ee801d..ca77697 100644--- a/src/entrypoints/offscreen/main.ts+++ b/src/entrypoints/offscreen/main.ts@@ -9,8 +9,10 @@ env.useCustomCache = false;const wasmPath = chrome.runtime.getURL('transformers/');env.backends.onnx.wasm.wasmPaths = wasmPath;+env.backends.onnx.wasm.numThreads = 1;let ner: TokenClassificationPipeline | null = null;+let activePort: chrome.runtime.Port | null = null;async function initModel(): Promise<{ ok: boolean; error?: string }> {if (ner) return { ok: true };@@ -18,6 +20,15 @@ async function initModel(): Promise<{ ok: boolean; error?: string }> {ner = (await pipeline('token-classification', MODEL_ID, {dtype: 'fp32',device: 'wasm',+ progress_callback: (progress: any) => {+ if (activePort && progress.status === 'progress') {+ activePort.postMessage({+ type: 'progress',+ file: progress.file,+ progress: Math.round(progress.progress),+ });+ }+ },})) as TokenClassificationPipeline;return { ok: true };} catch (err) {@@ -112,13 +123,17 @@ async function detect(text: string): Promise<{ entities: Entity[] }> {try {const chunks = chunkText(text);+ console.log('[OFFSCREEN] text length:', text.length, 'chunks:', chunks.length);const allEntities: Entity[] = [];const seen = new Set<string>();for (const chunk of chunks) {const results = await ner!(chunk, { ignore_labels: ['O'] });const tokens = (Array.isArray(results) ? results : [results]) as unknown as TokenResult[];- for (const entity of postProcess(tokens, text)) {+ console.log('[OFFSCREEN] raw tokens:', tokens.length, tokens.slice(0, 5));+ const processed = postProcess(tokens, text);+ console.log('[OFFSCREEN] processed entities:', processed.length, processed.slice(0, 5));+ for (const entity of processed) {const key = `${entity.text}:${entity.label}`;if (!seen.has(key)) {seen.add(key);@@ -127,22 +142,31 @@ async function detect(text: string): Promise<{ entities: Entity[] }> {}}+ console.log('[OFFSCREEN] total entities:', allEntities.length, allEntities);return { entities: allEntities };} catch {return { entities: [] };}}-chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {+const port = chrome.runtime.connect({ name: 'mimik-offscreen' });+activePort = port;+port.onMessage.addListener((msg) => {if (msg.type === 'offscreen:ai:init') {- initModel().then(sendResponse);- return true;+ initModel().then((res) => port.postMessage({ id: msg.id, ...res }));}if (msg.type === 'offscreen:ai:detect') {- detect(msg.text).then(sendResponse);- return true;+ detect(msg.text)+ .then((res) => {+ port.postMessage({+ type: 'log',+ message: `detect done: ${res.entities.length} entities, text length: ${(msg.text || '').length}`,+ });+ port.postMessage({ id: msg.id, ...res });+ })+ .catch((err) => {+ port.postMessage({ type: 'log', message: `detect error: ${err}` });+ port.postMessage({ id: msg.id, entities: [] });+ });}- return false;});--chrome.runtime.sendMessage({ type: 'offscreen:ready' });diff --git a/src/ui/shared/SettingsView.tsx b/src/ui/shared/SettingsView.tsxindex 733613b..b4a9fe8 100644--- a/src/ui/shared/SettingsView.tsx+++ b/src/ui/shared/SettingsView.tsx@@ -1,8 +1,9 @@-import { ArrowLeft, Check, EyeOff, Shield } from 'lucide-react';+import { ArrowLeft, Check, EyeOff, Loader2, Shield } from 'lucide-react';import { useEffect, useState } from 'react';import { PRESET_LABELS, type PresetKey } from '@/core/blur/regexes';import { AI_PROVIDERS, type AIProviderKey } from '@/core/capture/ai/models';import { localStorage } from '@/lib/browser-api';+import { sendMessage } from '@/lib/messaging';import { Button } from '@/ui/components/ui/button';import { Input } from '@/ui/components/ui/input';@@ -24,6 +25,19 @@ export default function SettingsView({ onBack }: SettingsViewProps) {macAddress: false,});const [blurAiEnabled, setBlurAiEnabled] = useState(false);+ const [aiModelStatus, setAiModelStatus] = useState<'idle' | 'loading' | 'ready' | 'error'>('idle');+ const [aiProgress, setAiProgress] = useState(0);++ useEffect(() => {+ if (aiModelStatus !== 'loading') return;+ const handler = (changes: Record<string, chrome.storage.StorageChange>) => {+ if ('blurAiProgress' in changes) {+ setAiProgress(changes.blurAiProgress.newValue ?? 0);+ }+ };+ chrome.storage.onChanged.addListener(handler);+ return () => chrome.storage.onChanged.removeListener(handler);+ }, [aiModelStatus]);useEffect(() => {localStorage.get(['aiApiKey', 'aiProvider', 'aiModel', 'blurPresets', 'blurAiEnabled']).then((result) => {@@ -137,20 +151,51 @@ export default function SettingsView({ onBack }: SettingsViewProps) {</div>))}- <div className="flex items-center justify-between pt-2 border-t border-border">- <span className="text-[13px] font-medium text-foreground">AI Detection</span>- <button- onClick={() => setBlurAiEnabled((prev) => !prev)}- className={`w-9 h-5 rounded-full transition-colors relative ${- blurAiEnabled ? 'bg-amber' : 'bg-border'- }`}- >- <span- className={`absolute top-0.5 left-0.5 w-4 h-4 rounded-full bg-white shadow-sm transition-transform ${- blurAiEnabled ? 'translate-x-4' : 'translate-x-0'+ <div className="pt-2 border-t border-border space-y-2">+ <div className="flex items-center justify-between">+ <span className="text-[13px] font-medium text-foreground">AI Detection</span>+ <button+ onClick={() => {+ const next = !blurAiEnabled;+ setBlurAiEnabled(next);+ if (next && aiModelStatus !== 'ready') {+ setAiModelStatus('loading');+ sendMessage('blurAiDetect', { text: '' })+ .then(() => setAiModelStatus('ready'))+ .catch(() => setAiModelStatus('error'));+ }+ }}+ disabled={aiModelStatus === 'loading'}+ className={`w-9 h-5 rounded-full transition-colors relative ${+ blurAiEnabled ? 'bg-amber' : 'bg-border'}`}- />- </button>+ >+ <span+ className={`absolute top-0.5 left-0.5 w-4 h-4 rounded-full bg-white shadow-sm transition-transform ${+ blurAiEnabled ? 'translate-x-4' : 'translate-x-0'+ }`}+ />+ </button>+ </div>+ {aiModelStatus === 'loading' && (+ <div className="space-y-1.5">+ <div className="flex items-center justify-between text-[11px] text-muted-foreground">+ <div className="flex items-center gap-2">+ <Loader2 size={12} className="animate-spin text-amber" />+ Downloading AI model...+ </div>+ <span className="font-medium">{aiProgress}%</span>+ </div>+ <div className="h-1.5 rounded-full bg-border overflow-hidden">+ <div+ className="h-full rounded-full bg-amber transition-all duration-300"+ style={{ width: `${aiProgress}%` }}+ />+ </div>+ </div>+ )}+ {aiModelStatus === 'ready' && <p className="text-[11px] text-muted-foreground">Model ready</p>}+ {aiModelStatus === 'error' && <p className="text-[11px] text-red-500">Failed to load model</p>}</div></div></div>diff --git a/wxt.config.ts b/wxt.config.tsindex 3c645be..865012e 100644--- a/wxt.config.ts+++ b/wxt.config.ts@@ -29,6 +29,9 @@ export default defineConfig({"offscreen",],host_permissions: ["<all_urls>"],+ content_security_policy: {+ extension_pages: "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';",+ },minimum_chrome_version: "118",action: {},side_panel: {