The UserAgentSmith
class is a JavaScript utility designed to generate random user agent strings for various browsers and operating systems. This can be useful for testing web applications, simulating browser requests, or web scraping scenarios where diverse user agents are needed.
To install the user-agent-smith
package, use one of the following commands:
npm install user-agent-smith
Or via Yarn:
yarn add user-agent-smith
Note: The Yarn command uses yarn add
instead of yarn install
because yarn add
is the standard way to add a new package to your project dependencies, while yarn install
is typically used to install all dependencies listed in package.json
.
Here’s how to import and instantiate the UserAgentSmith
class:
import UserAgentSmith from 'user-agent-smith';
const generator = new UserAgentSmith();
You can then call its methods to generate user agent strings. Run your script with:
node example.mjs
Generates a random user agent string with a bias towards popular browsers (e.g., Chrome, Firefox).
console.log(generator.generate());
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6789.12 Safari/537.36
Generates a random mobile user agent string (e.g., Chrome on Android, Safari on iOS).
console.log(generator.generateMobile());
Mozilla/5.0 (Linux; Android 13.0; OnePlus 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.3456.78 Mobile Safari/537.36
Generates a specified number of unique user agent strings.
-
count
: The number of unique user agents to generate (integer).
console.log(generator.generateMultiple(3));
[
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.5678.90 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0; rv:127.0) Gecko/20230101 Firefox/127.0",
"Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.567.89"
]
Here’s a complete example demonstrating how to use the UserAgentSmith
class in a modern Node.js script with MJS syntax:
// example.mjs
import UserAgentSmith from 'user-agent-smith';
const generator = new UserAgentSmith();
// Generate a single random user agent
console.log("Random User Agent:");
console.log(generator.generate());
// Generate a single mobile user agent
console.log("\nRandom Mobile User Agent:");
console.log(generator.generateMobile());
// Generate multiple unique user agents
console.log("\nMultiple User Agents:");
console.log(generator.generateMultiple(3));
To run this script:
node example.mjs
Random User Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6789.12 Safari/537.36
Random Mobile User Agent:
Mozilla/5.0 (Linux; Android 13.0; OnePlus 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.3456.78 Mobile Safari/537.36
Multiple User Agents:
[
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.5678.90 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0; rv:127.0) Gecko/20230101 Firefox/127.0",
"Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.567.89"
]