denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
35.21k stars 2.55k forks source link

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

Open shiv19 opened 4 months ago

shiv19 commented 4 months ago
#!/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 commented 4 months ago

@denysdovhan Please remove the label from this issue.

shiv19 commented 4 months ago

My motivation to write this script was because simply running

npm install -g wtfjs
wtfjs

is currently broken. And even when you get it working, it just prints the entire readme to the console.

I rather just have it to show me 1 example at a time.