DEV Community

Nikolas Evers
Nikolas Evers

Posted on • Originally published at nikol.as on

2 1

WeeklyRandom #1: sindresorhus/ua-string

Welcome to WeeklyRandom, a blog series in which I challenge myself to explore a random GitHub repository starred by me every week.

The entry for week 1 should have been published last week, but life Red Dead Redemption 2 thwarted my plans. 🤠

Never mind! Let's find out which repo I can explore this week.

🥁

node index.js vintagesucks
https://github.com/sindresorhus/ua-string

ua-string by Sindre Sorhus. This shouldn't take long. The description of the repo is as follows:

Get the user agent of a recent Chrome version to pretend to be a browser in network requests

Okay. Let's try a request to whatismybrowser.com without ua-string:

// index.js
const got = require('got');
const HTMLParser = require('fast-html-parser');

(async () => {
    try {
        got('https://developers.whatismybrowser.com/useragents/parse/?analyse-my-user-agent=yes')
        .then((res) =>
            HTMLParser.parse(res.body).querySelector('.useragent')
        )
        .then((result) =>
            console.log(result.childNodes[0].rawText)
        );
    } catch (error) {
        console.log(error.response.body);
    }
})();

Let's run it:

node index.js
got/9.2.2 (https://github.com/sindresorhus/got)

Since I used got (created by Sindre, too 👋) to make the request, the reported user agent is got.

Let's try ua-string now.

npm install ua-string

The script, now using ua-string:

const got = require('got');
const uaString = require('ua-string');
const HTMLParser = require('fast-html-parser');

(async () => {
    try {
        got('https://developers.whatismybrowser.com/useragents/parse/?analyse-my-user-agent=yes', {
            headers: {
                'user-agent': uaString
            }
        })
        .then((res) =>
            HTMLParser.parse(res.body).querySelector('.useragent')
        )
        .then((result) =>
            console.log(result.childNodes[0].rawText)
        );
    } catch (error) {
        console.log(error.response.body);
    }
})();

Here we go:

node index.js
Mozilla/5.0 (Macintosh; Intel Mac OS X 10\_13\_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36

Perfect. 🎉

You can find the complete code on GitHub at vintagesucks/weeklyrandom. Pull requests with improvements are very welcome!

Until next week, when I'll check out my next WeeklyRandom repository.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️