Auto-Open URLs with Node.js – A Beginner-Friendly Script

Want to automate opening a specific URL in your browser multiple times? Whether you're testing link behavior, demoing an app, or running an automation sequence — this simple Node.js script does the job.

๐Ÿ› ️ What This Script Does

  • Opens any given URL in Chrome or Edge
  • Lets you choose how many times (tabs) you want it to open
  • Works from the command line with just one command

๐Ÿ“ฆ Prerequisites

  1. Node.js installed on your system
  2. Install the open package via terminal:
npm install open

๐Ÿ’ก The Node.js Script

Create a file named index.js and paste the code below:

// index.js
const open = require("open").default;

// ๐ŸŒ URL to open
const url =
  "https://hiddengemsatlas.blogspot.com/2025/08/auto-open-urls-with-nodejs-beginner.html"; // Replace this with your URL

// ✅ Map browser IDs to app names
const browserMap = {
  1: "chrome",
  2: "msedge",
};

// ๐Ÿงพ Get command-line arguments
const [, , browserIdArg, countArg] = process.argv;
const browserId = parseInt(browserIdArg);
const count = parseInt(countArg);

// ❌ Validate inputs
if (!browserMap[browserId] || isNaN(count) || count <= 0) {
  console.error("❌ Usage: node index.js <browser_id> <number_of_tabs>");
  console.log("   Browser IDs: 1 - Chrome, 2 - Edge");
  process.exit(1);
}

const selectedBrowser = browserMap[browserId];

(async () => {
  console.log(`๐Ÿš€ Opening ${url} in ${selectedBrowser}, ${count} time(s)...`);
  for (let i = 0; i < count; i++) {
    console.log(`๐Ÿงญ Opening tab ${i + 1}`);
    await open(url, { app: { name: selectedBrowser } });
  }
  console.log("✅ All tabs opened.");
})();

๐Ÿš€ How to Run

Open terminal in your project folder and run:

node index.js 1 3

Explanation:

  • 1 = Chrome (use 2 for Edge)
  • 3 = Number of tabs to open

๐Ÿ“Ž Use Cases

  • Automatically open your blog or product page
  • Simulate link click behavior during UI testing
  • Preview marketing links or web apps multiple times

๐ŸŽฏ Pro Tip

Want a more advanced version that auto-scrolls the page, supports custom configs, and even auto-launches with one click?

Check out our advanced tool on Gumroad:

HGDevHub – Automated URL Viewer (Basic Edition)

๐Ÿ“ฃ I’d Love to Hear From You!

Found this script useful? Have suggestions or a related topic you’d like me to explore? I’d love to hear your feedback and recommendations.

๐Ÿ”” Stay connected and follow HGDevHub for more micro tools, automation scripts, and tech walkthroughs:

๐Ÿ’ฌ Your input helps shape what comes next!

๐Ÿ“ฉ Stay Tuned

More automation scripts and micro-tools coming soon. Follow HGDevHub for fresh tools that save time and spark ideas.

Comments

  1. Super helpful and well-explained! ๐Ÿ”ฅ Really appreciate how you kept it simple yet practical — great for quick tests, demos, and automation setups. I’m definitely going to adapt this into some of my own workflows. Loved the real-world examples too ๐Ÿ‘

    ReplyDelete
  2. Great automation script! Tried it out and it’s working perfectly. Thanks for sharing such a helpful and beginner-friendly blog. Keep up the great work!

    ReplyDelete
  3. Awesome tutorial! Loved how beginner-friendly the script was while still being really practical. Perfect for anyone looking to automate URL opening with Node.js.

    ReplyDelete

Post a Comment

Popular posts from this blog

Fluent Assertions in C# with xUnit – Clean and Readable Unit Tests

Top 7 Must-Watch Anime for Beginners: Your Gateway to an Epic Adventure