GitHub license GitHub issues GitHub stars GitHub forks
TypeScript Vite ESLint Prettier Node.js
Tampermonkey Greasemonkey Violentmonkey Mobile Support
Last Commit Contributors Repository Size
A modern, production-ready template for building UserScripts using TypeScript and Vite. This template provides a solid foundation with best practices, type safety, and modern development tools for creating sophisticated Tampermonkey and Greasemonkey scripts.
- π Modern Tech Stack: TypeScript, Vite, ESLint, Prettier
- π‘οΈ Type Safety: Strict TypeScript configuration with comprehensive UserScript API definitions
- π§ Development Tools: ESLint, Prettier, automated build pipeline
- π― Environment Support: Separate development and production configurations
- π¦ Modular Architecture: Component system with reusable utilities
- πΎ Storage Management: Type-safe wrapper for GM_setValue/GM_getValue
- π οΈ Build System: Optimized Vite configuration with automatic header generation
- π¨ DOM Utilities: Helper functions for element manipulation and waiting
- π Error Handling: Comprehensive error boundary system
- β‘ Event System: Type-safe event emitter for module communication
- π± Mobile Support: Touch-optimized interface with mobile browser detection
- π€ Touch Gestures: Built-in touch event handling and gesture recognition
- π² Responsive Design: Mobile-first CSS with safe area support for notched devices
git clone https://github.com/JosunLP/UserScriptProjectTemplate.git
cd UserScriptProjectTemplate
npm install# Install dependencies npm install # Start development mode with auto-rebuild npm run dev # Type checking npm run type-check # Linting and formatting npm run validate
src/ βββ types/ # TypeScript type definitions βββ utils/ # Utility functions (Storage, DOM, Events) βββ core/ # Core application logic βββ modules/ # Feature modules βββ index.ts # Main application entry point tools/ βββ userScriptHeader.ts # UserScript header generator assets/ # Icons and static resources βββ icon.afdesign
The main configuration is in header.config.json. This file controls UserScript metadata generation:
{
"environments": {
"development": {
"includes": ["http://localhost:*/*", "https://localhost:*/*"],
"grants": ["GM_setValue", "GM_getValue", "GM_log", "GM_notification"]
},
"production": {
"includes": ["https://your-domain.com/*"],
"grants": ["GM_setValue", "GM_getValue"]
}
}
}# Development npm run dev # Start development with watch mode npm run dev:build # Single development build with header npm run dev:header # Generate header for existing dev build # Production npm run build # Build for production npm run build:prod # Explicit production build # Quality Assurance npm run validate # Type check + lint npm run lint # ESLint with auto-fix npm run format # Prettier formatting # Utilities npm run clean # Clean dist folder npm run type-check # TypeScript type checking
The template features advanced build optimization for production:
| Build Type | File Size | Compressed | Features |
|---|---|---|---|
| Development | ~115 KB | ~30 KB | Source maps, debug info, readable code |
| Production | ~25 KB | ~6 KB | Minified, tree-shaken, optimized |
Production optimizations include:
- β‘ Terser minification with aggressive compression settings
- π³ Tree-shaking to remove unused code
- π― Dead code elimination for DEV blocks
- π¦ Module inlining for single-file output
- π§ Property mangling for smaller variable names
- π ES2020 target for modern JavaScript features
- πΎ GZIP compression reducing size by ~75%
- Configure your script in
header.config.json - Start development:
npm run dev - Write your code in the
src/directory - Build for production:
npm run build - Install the UserScript from
dist/folder in Tampermonkey/Greasemonkey
The template includes a type-safe storage system:
import { Storage } from '@/utils/storage'; // Save data Storage.set('userData', { name: 'John', visits: 5 }); // Get data with default value const userData = Storage.get('userData', { name: '', visits: 0 }); // Check if key exists if (Storage.has('userData')) { // Key exists } // Remove data Storage.remove('userData');
Helper functions for DOM manipulation:
import { DOMUtils } from '@/utils/dom'; // Wait for element to appear const element = await DOMUtils.waitForElement('.my-selector'); // Add custom styles DOMUtils.addStyles(` .my-class { color: red; } `); // Create element with attributes const button = DOMUtils.createElement('button', { textContent: 'Click me', onclick: () => console.log('Clicked!'), });
Type-safe communication between modules:
import { EventEmitter } from '@/utils/events'; interface MyEvents { userAction: { userId: string }; dataLoaded: { count: number }; } const emitter = new EventEmitter<MyEvents>(); // Listen for events emitter.on('userAction', ({ userId }) => { console.log(`User ${userId} performed an action`); }); // Emit events emitter.emit('userAction', { userId: '123' });
Create reusable, event-driven modules:
import { EventEmitter } from '@/utils/events'; interface ModuleEvents { initialized: void; actionPerformed: { action: string }; } export class MyModule extends EventEmitter<ModuleEvents> { async initialize() { // Module initialization logic this.emit('initialized', undefined); } }
Mobile-specific functionality for touch-enabled devices:
import { MobileUtils } from '@/utils/mobile'; // Detect mobile browser and capabilities const detection = MobileUtils.detect(); console.log('Is Mobile:', detection.isMobile); console.log('Has Touch:', detection.hasTouch); console.log('Browser:', detection.browser); // Add mobile-optimized styles if (detection.isMobile) { MobileUtils.addMobileStyles(); } // Unified touch/mouse event handling MobileUtils.addUnifiedEventListener(element, 'start', event => { const position = MobileUtils.getEventPosition(event); console.log('Touch/click at:', position); }); // Create mobile-friendly buttons const button = mobileModule.createMobileButton('Action', () => { console.log('Button pressed'); }); // Orientation detection console.log('Portrait mode:', MobileUtils.isPortrait());
- Tampermonkey: Full support with all GM_* APIs
- Greasemonkey: Compatible with standard UserScript APIs
- Violentmonkey: Full compatibility
- Safari: Works with userscript managers
Android:
- Kiwi Browser: Full Chrome extension + UserScript support
- Microsoft Edge Mobile: Tampermonkey support
- Firefox Mobile: Greasemonkey, Tampermonkey, Violentmonkey
- Yandex Browser: Chrome extension support
iOS:
- Safari Mobile: Tampermonkey or Userscripts App
- Limited support due to iOS restrictions
- Touch Gestures: Tap, swipe, and pinch detection
- Responsive Design: Mobile-first CSS with viewport adaptation
- Safe Area Support: Automatic handling of notched devices
- Orientation Detection: Portrait/landscape change handling
- Mobile-Optimized UI: Touch-friendly buttons and menus
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and ensure tests pass:
npm run validate - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow TypeScript best practices
- Use meaningful variable and function names
- Add proper error handling
- Write self-documenting code
- Follow the established project structure
- Run
npm run validatebefore committing
This project is licensed under the MIT License.
Jonas Pfalzgraf
- Email: info@josunlp.de
- GitHub: @JosunLP
- Website: josunlp.de
Built with β€οΈ for the UserScript community