smart-browser-detection
Version:
Smart browser detection library with anti-spoofing capabilities, multi-method detection, and mobile browser support. Outperforms UA-Parser-JS with superior accuracy and spoofing resistance.
193 lines (191 loc) • 5.01 kB
TypeScript
/**
* Smart Browser Detection Library
* Advanced browser detection with anti-spoofing capabilities
*
* @version 1.0.0
* @author Smart Browser Detection Team
* @license MIT
*/
interface BrowserDetectionResult {
browser: string;
browserVersion: string;
engine: string;
engineVersion: string;
platform: string;
os: string;
osVersion: string;
isMobile: boolean;
isTablet: boolean;
isDesktop: boolean;
confidence: number;
detectionMethods: string[];
userAgent: string;
vendor: string;
timestamp: number;
}
interface DetectionMethodResult {
browser: string;
confidence: number;
method: string;
}
interface EngineInfo {
engine: string;
version: string;
}
interface OSInfo {
os: string;
osVersion: string;
}
interface CacheStats {
size: number;
keys: string[];
}
interface RegexPatterns {
chrome: RegExp;
firefox: RegExp;
safari: RegExp;
edge: RegExp;
opera: RegExp;
ie: RegExp;
android: RegExp;
ios: RegExp;
windows: RegExp;
macos: RegExp;
}
type DetectionMethod = 'apiDetection' | 'vendorDetection' | 'userAgentDetection' | 'cssDetection';
type BrowserType = 'Chrome' | 'Firefox' | 'Safari' | 'Edge' | 'Opera' | 'Internet Explorer' | 'Unknown';
type PlatformType = 'Desktop' | 'Mobile' | 'Tablet';
type OSType = 'Windows' | 'macOS' | 'Linux' | 'Android' | 'iOS' | 'Unknown';
type EngineType = 'Blink' | 'Gecko' | 'WebKit' | 'Trident' | 'Unknown';
interface ChromeAPI {
runtime?: {
onConnect?: EventTarget;
};
webstore?: {
install?: (url: string, callback?: (error?: Error) => void) => void;
};
}
interface SafariAPI {
pushNotification?: {
permission?: string;
requestPermission?: () => Promise<string>;
};
}
interface OperaAPI {
version?: string;
}
interface UserAgentData {
brands?: Array<{
brand: string;
version: string;
}>;
mobile?: boolean;
platform?: string;
}
declare global {
interface Window {
chrome?: ChromeAPI;
InstallTrigger?: boolean;
safari?: SafariAPI;
opera?: OperaAPI;
}
interface Navigator {
userAgentData?: UserAgentData;
}
interface Document {
documentMode?: number;
}
interface CSSStyleDeclaration {
webkitTransform: string;
webkitAppearance: string;
}
}
/**
* Smart Browser Detection Class
* Provides multi-method browser detection with confidence scoring
*/
declare class SmartBrowserDetection {
private cache;
private detectionMethods;
private regexPatterns;
constructor();
/**
* Main detection function that combines multiple methods
* @returns Browser detection result with confidence scoring
*/
detectBrowser(): BrowserDetectionResult;
/**
* Smart result selection algorithm with priority handling
* @param methodResults - Array of detection method results
* @returns Best detection result
*/
private selectBestResult;
/**
* API-based detection using browser-specific APIs
* @returns API detection result
*/
private apiDetection;
/**
* Vendor-based detection using navigator.vendor and other vendor info
* @returns Vendor detection result
*/
private vendorDetection;
/**
* Enhanced user agent detection with better parsing
* @returns User agent detection result
*/
private userAgentDetection;
/**
* CSS-based detection using browser-specific CSS features
* @returns CSS detection result
*/
private cssDetection;
/**
* Detect if running on mobile device
* @returns True if mobile device
*/
detectMobile(): boolean;
/**
* Detect if running on tablet device
* @returns True if tablet device
*/
detectTablet(): boolean;
/**
* Get browser version from user agent
* @param browser - Browser name
* @returns Browser version
*/
getBrowserVersion(browser: BrowserType): string;
/**
* Get engine information for browser
* @param browser - Browser name
* @returns Engine information
*/
getEngineInfo(browser: BrowserType): EngineInfo;
/**
* Get OS information
* @returns OS information
*/
getOSInfo(): OSInfo;
/**
* Get platform type
* @returns Platform type
*/
getPlatform(): PlatformType;
/**
* Get complete browser information
* @returns Complete browser information
*/
getCompleteInfo(): BrowserDetectionResult;
/**
* Clear detection cache
*/
clearCache(): void;
/**
* Get cache statistics
* @returns Cache statistics
*/
getCacheStats(): CacheStats;
}
export { SmartBrowserDetection, SmartBrowserDetection as default };
export type { BrowserDetectionResult, BrowserType, CacheStats, DetectionMethod, DetectionMethodResult, EngineInfo, EngineType, OSInfo, OSType, PlatformType, RegexPatterns };