Generate Random User Agent - Custom Script
I tried different npm package but they gives me old user agent | npm i @rahulxf/random-user-agent
by Rahul Vishwakarma
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 .