Skip to content

Instantly share code, notes, and snippets.

@imgnx
Last active December 11, 2024 07:05
Show Gist options
  • Save imgnx/25e4046597bbc2db12d43d8bc2d7de4d to your computer and use it in GitHub Desktop.
Save imgnx/25e4046597bbc2db12d43d8bc2d7de4d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// Open ("Google") Search in your default browser from your CLI.
// Check if search terms were provided
if (process.argv.length <= 2) {
console.log('Usage: gsearch [search terms]');
process.exit(1);
}
// Get the search terms from command line arguments
const searchTerms = process.argv.slice(2).join(' ');
// Create the search URL
const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(searchTerms)}`;
// Open the URL in default browser
const { platform } = process;
const { exec } = require('child_process');
const commands = {
darwin: `open "${searchUrl}"`,
linux: `xdg-open "${searchUrl}"`,
win32: `start "${searchUrl}"`
};
const command = commands[platform];
if (command) {
exec(command, (error) => {
if (error) {
console.error('Error opening browser:', error);
process.exit(1);
}
});
} else {
console.error('Unsupported platform');
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment