← Back to blog
Generate Random User Agent - Custom Script
Dec 19, 2024 3 min read
A small custom TypeScript utility for generating newer, randomized browser user agents instead of relying on stale package defaults.
TypeScriptAutomationNPMUser Agent
I tried different npm package but they gives me old user agent. Here how i create random user agent using my own function
// generateUserAgent.ts
export function generateUserAgents(count: number): string[] {
const userAgents: string[] = [];
const baseOS = [
"X11; Linux i686",
"X11; Linux x86_64",
"X11; Ubuntu i686",
"X11; Ubuntu x86_64",
"X11; Fedora i686",
"Macintosh; Intel Mac OS X 10_15_7",
"Macintosh; Intel Mac OS X 13_5",
"Macintosh; Intel Mac OS X 14_0",
"Windows NT 10.0; Win64; x64",
"Windows NT 11.0; Win64; x64"
];
// const baseChromeVersion = 111;
const subVersionMax = 500;
const webkitVersion = "537.36";
const safariVersion = "537.36";
for (let i = 0; i < count; i++) {
const os = baseOS[Math.floor(Math.random() * baseOS.length)];
const chromeVersionMajor = Math.floor(Math.random() * (115 - 111 + 1)) + 111;
const chromeVersion = `${chromeVersionMajor}.${Math.floor(
Math.random() * subVersionMax
)}.${Math.floor(Math.random() * 100)}`;
const userAgent = `Mozilla/5.0 (${os}) AppleWebKit/${webkitVersion} (KHTML, like Gecko) Chrome/${chromeVersion} Safari/${safariVersion}`;
userAgents.push(userAgent);
}
return userAgents;
}
generateUserAgents(1000);
Result:
Mozilla/5.0 (X11; Ubuntu x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.259.40 Safari/537.36
Mozilla/5.0 (X11; Fedora i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.357.58 Safari/537.36
Mozilla/5.0 (X11; Fedora i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.455.72 Safari/537.36
Mozilla/5.0 (X11; Fedora i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.480.49 Safari/537.36
Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.443.60 Safari/537.36
Mozilla/5.0 (X11; Ubuntu i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.465.80 Safari/537.36
Mozilla/5.0 (X11; Ubuntu i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.295.6 Safari/537.36
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.269.50 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.76.94 Safari/537.36
Mozilla/5.0 (X11; Ubuntu x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.299.94 Safari/537.36
Here is result of 10 thousand random user agents, you can see on my github gist - Click Here.
- You can check out more about this here - https://www.rahulxf.tech/projects/3-random-user-agent
- Here is NPM Package - Click Here.
More posts
WebScraping Using Puppeteer
Dec 15, 2024 3 min readNotes from experimenting with Puppeteer, stealth plugins, randomized user agents, and scraping tweet content in a more resilient way.
Understanding WebRTC: Building a Real-Time Interactive Portal
Dec 15, 2024 7 min readA guide to building a WebRTC-based collaborative portal with video, screen sharing, messaging, file transfer, Monaco, and Yjs-powered collaboration.
Written by Rahul Vishwakarma — this is the first-party home for this article on rahulxf.com.