The Dynamic Favicon Updater is a lightweight JavaScript class designed to enhance user engagement by dynamically updating the favicon based on specific events or conditions. This framework allows web applications to communicate important information through the browser tab itself, even when the user is not actively viewing the page.
- Change favicon dynamically based on events or conditions
- Support for different states (e.g., notifications, status indicators)
- Customizable icons to match branding and application needs
- Automatic restoration to the original favicon when no events are active
- Cross-tab communication to ensure consistency across multiple browser tabs
- Priority management for multiple active states
- Error handling and input validation
- Web Applications: Enhance user experience by displaying real-time notifications or status updates in the favicon.
- Messaging Platforms: Indicate unread messages or new notifications directly in the browser tab.
- Monitoring Tools: Show system or service status updates through favicon changes.
npm install dynamic-favicon-updater// CommonJS
const FaviconUpdater = require('dynamic-favicon-updater');
// ESM
import FaviconUpdater from 'dynamic-favicon-updater';<script src="path/to/favicon-updater.js"></script>Create a new instance of FaviconUpdater with the default favicon URL and any pre-defined states.
const faviconUpdater = new FaviconUpdater({
defaultIcon: 'path/to/default/favicon.ico',
states: {
'notification': 'path/to/notification/favicon.ico',
'error': 'path/to/error/favicon.ico',
'success': 'path/to/success/favicon.ico'
}
});Change the favicon based on a specific event or condition.
faviconUpdater.setState('notification');Remove a specific state from the active states.
faviconUpdater.clearState('notification');Set a custom favicon directly.
faviconUpdater.setFavicon('path/to/custom/favicon.ico');Handle multiple states with priority (e.g., error state takes precedence over notification).
faviconUpdater.setState('notification');
faviconUpdater.setState('error');
faviconUpdater.clearState('error');Specify the priority of each state.
faviconUpdater.setPriority('error', 10);
faviconUpdater.setPriority('notification', 5);Remove all active states and restore the default favicon.
faviconUpdater.clearAllStates();Retrieve the current list of active states.
const activeStates = faviconUpdater.getActiveStates();Retrieve the URL of the currently displayed favicon.
const currentIcon = faviconUpdater.getCurrentIcon();Display a count badge overlaid on the favicon using canvas.
// Show badge with count
faviconUpdater.setBadge(5);
// With custom options
faviconUpdater.setBadge(3, {
backgroundColor: '#FF0000', // Badge circle color (default: '#FF0000')
textColor: '#FFFFFF', // Count text color (default: '#FFFFFF')
size: 0.4, // Badge size relative to favicon, 0-1 (default: 0.4)
position: 'top-right' // 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
});
// Clear the badge
faviconUpdater.clearBadge();Counts greater than 99 are displayed as "99+". Falls back to the unbadged icon if canvas is unavailable.
Start animated favicon effects using requestAnimationFrame.
// Built-in spinner animation
faviconUpdater.startAnimation('spinner');
// Built-in pulse animation
faviconUpdater.startAnimation('pulse');
// With options
faviconUpdater.startAnimation('spinner', {
color: '#4285F4', // Animation color (default: '#4285F4')
speed: 1.0, // Speed multiplier (default: 1.0)
fps: 15 // Frames per second cap (default: 15)
});
// Custom animation function
faviconUpdater.startAnimation((ctx, size, progress, baseImage) => {
ctx.drawImage(baseImage, 0, 0, size, size);
ctx.globalAlpha = progress;
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, size * progress, 4);
ctx.globalAlpha = 1.0;
});
// Stop animation
faviconUpdater.stopAnimation();Badges are composited on top of animations when both are active. Animations automatically pause in background tabs and resume when the tab becomes visible.
Remove all event listeners and release resources. Call this when the updater is no longer needed (e.g., in SPAs on route change).
faviconUpdater.destroy();The framework includes error handling and input validation. If an operation fails (e.g., due to an invalid URL or state), an error will be thrown with a descriptive message.
const faviconUpdater = new FaviconUpdater({
defaultIcon: 'path/to/default/favicon.ico',
states: {
'notification': 'path/to/notification/favicon.ico',
'error': 'path/to/error/favicon.ico',
'success': 'path/to/success/favicon.ico'
}
});
faviconUpdater.setState('notification');
setTimeout(() => faviconUpdater.clearState('notification'), 5000);Contributions to the Dynamic Favicon Updater are welcome! Here are some ways you can contribute:
- Report bugs or request features by opening an issue on the GitHub repository.
- Improve documentation and examples.
- Write code to address open issues or add new features.
To contribute code:
- Fork the repository.
- Create a new branch for your changes.
- Make your changes and add tests if applicable.
- Submit a pull request with a clear description of your changes.
Please ensure your code follows the existing style and passes all tests before submitting a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions, support, or to report issues, please open an issue on the GitHub repository.