mirror of
https://github.com/JosunLP/UserScriptProjectTemplate.git
synced 2025-10-14 09:00:11 +00:00

- Updated package.json to include new scripts for development, production builds, linting, formatting, and cleaning. - Added ESLint and Prettier for code quality and formatting. - Refactored main application class to extend EventEmitter and manage modules. - Introduced ExampleModule to demonstrate module structure and functionality. - Created utility classes for DOM manipulation, event handling, and persistent storage. - Added TypeScript definitions for UserScript environment. - Improved TypeScript configuration with stricter checks and path aliases. - Updated Vite configuration to handle development and production builds more effectively. - Enhanced user script header generation to support environment-specific configurations.
5 KiB
5 KiB
UserScript Project Template
A modern, scalable UserScript project template for building large and structured TypeScript projects for Tampermonkey or Greasemonkey. Originally designed for the Ingress community, but adaptable for any web-based project.
✨ Features
- 🔧 Modern TypeScript with strict type checking
- 🚀 Vite for fast builds and development
- 📦 Modular Architecture with utilities and event system
- 🎯 Environment-specific configurations (dev/prod)
- 🧹 ESLint + Prettier for code quality
- 💾 Storage utilities for persistent data
- 🎨 DOM utilities for easier manipulation
- 📡 Event system for modular communication
- 🔒 TypeScript definitions for UserScript APIs
🚀 Quick Start
-
Clone or use this template:
git clone https://github.com/JosunLP/UserScriptProjectTemplate.git my-userscript cd my-userscript
-
Install dependencies:
npm install
-
Configure your script:
- Edit
header.config.json
to set your target domains - Update
package.json
with your script details
- Edit
-
Start development:
npm run dev
-
Build for production:
npm run build:prod
📁 Project Structure
src/
├── index.ts # Main application entry point
├── types/ # TypeScript definitions
│ └── userscript.d.ts
├── utils/ # Utility functions
│ ├── storage.ts # Persistent storage helpers
│ ├── dom.ts # DOM manipulation utilities
│ └── events.ts # Event system
├── core/ # Core application logic
└── modules/ # Feature modules
🛠️ Available Scripts
npm run dev
- Build with watch mode for developmentnpm run build
- Build for productionnpm run build:prod
- Build for production (explicit)npm run lint
- Run ESLintnpm run lint:fix
- Fix ESLint issuesnpm run format
- Format code with Prettiernpm run type-check
- Run TypeScript type checkingnpm run clean
- Clean dist folder
⚙️ Configuration
Header Configuration
Edit header.config.json
to configure your UserScript metadata:
{
"environments": {
"development": {
"includes": ["http://localhost:*/*"],
"grants": ["GM_setValue", "GM_getValue", "GM_log"]
},
"production": {
"includes": ["https://your-domain.com/*"],
"grants": ["GM_setValue", "GM_getValue"]
}
}
}
Environment Variables
The build system automatically detects the environment:
NODE_ENV=development
for development buildsNODE_ENV=production
for production builds
🧰 Utilities
Storage
import { Storage } from '@/utils/storage';
// Store data
Storage.set('key', { some: 'data' });
// Retrieve data
const data = Storage.get('key', defaultValue);
// Check existence
if (Storage.has('key')) {
// Key exists
}
DOM Utilities
import { DOMUtils } from '@/utils/dom';
// Wait for element
const element = await DOMUtils.waitForElement('.my-selector');
// Add styles
DOMUtils.addStyles(`
.my-class { color: red; }
`);
// Create element
const button = DOMUtils.createElement('button', {
textContent: 'Click me',
onclick: () => console.log('Clicked!')
});
Events
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' });
🎯 Extending the Template
- Add new modules in
src/modules/
- Register modules in your main App class
- Use the event system for module communication
- Add utilities in
src/utils/
for reusable functionality
Example module:
// src/modules/example.ts
import { EventEmitter } from '@/utils/events';
import { Storage } from '@/utils/storage';
export class ExampleModule extends EventEmitter {
initialize() {
console.log('Example module initialized');
// Your module logic here
}
}
🔧 Development Tips
- Use
console.log()
freely - it's enabled in UserScripts - Test in development mode with localhost includes
- Use TypeScript strict mode for better code quality
- Leverage the storage utilities for persistent data
- Use the event system for loose coupling between modules
📝 License
MIT License - see LICENSE file for details.
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
💡 Originally for Ingress
This template was originally designed for the Ingress community but is versatile enough for any web-based UserScript project. The modular architecture makes it easy to build complex scripts while maintaining code quality and organization.