wtfjs icon indicating copy to clipboard operation
wtfjs copied to clipboard

A script to take you to a random example on the readme for this repo :)

Open shiv19 opened this issue 8 months ago • 2 comments

#!/bin/bash

# URL of the GitHub repository
repo_url="https://github.com/denysdovhan/wtfjs"

# Fetch the README content
readme_content=$(curl -s "$repo_url")

# Extract all anchor links from the README
anchor_links=$(echo "$readme_content" | sed -n 's/.*<a href="#\([^"]*\)".*/\1/p')

# Count the number of anchor links
num_links=$(echo "$anchor_links" | wc -l)

# Generate a random number between 1 and the number of links
random_num=$((RANDOM % num_links + 1))

# Select the random anchor link
random_link=$(echo "$anchor_links" | sed -n "${random_num}p")

# Construct the full URL
full_url="${repo_url}#${random_link}"

# Print the URL (for debugging)
echo "Opening URL: $full_url"

# Open the URL in the default browser
if command -v open &> /dev/null; then
    open "$full_url"  # For macOS
elif command -v xdg-open &> /dev/null; then
    xdg-open "$full_url"  # For Linux
elif command -v start &> /dev/null; then
    start "$full_url"  # For Windows
else
    echo "Unable to open the browser automatically. Please open this URL manually:"
    echo "$full_url"
fi

Save as wtfjs.sh

sh wtfjs.sh

Disclaimer: This script was co-created with Claude 3.5 Sonnet.

shiv19 avatar Jun 25 '24 08:06 shiv19