Skip to content

Instantly share code, notes, and snippets.

@remotephone
Created September 1, 2024 03:26
Show Gist options
  • Select an option

  • Save remotephone/9d2f902a776f97003380404d738cf97d to your computer and use it in GitHub Desktop.

Select an option

Save remotephone/9d2f902a776f97003380404d738cf97d to your computer and use it in GitHub Desktop.
gnome desktop extension external IP
#!/bin/bash
# Check if the user provided a name@domain argument
if [ -z "$1" ]; then
echo "Usage: $0 your-extension-name@your-domain"
exit 1
fi
EXTENSION_ID="$1"
EXTENSION_NAME="External IP Viewer"
EXTENSION_DESC="Shows your external IP address and country"
SHELL_VERSION="['44', '45']" # Replace with your GNOME version
# Create the extension directory
EXTENSION_DIR="$HOME/.local/share/gnome-shell/extensions/$EXTENSION_ID"
mkdir -p "$EXTENSION_DIR"
# Create the metadata.json file
cat > "$EXTENSION_DIR/metadata.json" <<EOL
{
"uuid": "$EXTENSION_ID",
"name": "$EXTENSION_NAME",
"description": "$EXTENSION_DESC",
"shell-version": $SHELL_VERSION,
"version": 1
}
EOL
# Create the extension.js file
cat > "$EXTENSION_DIR/extension.js" <<'EOL'
const { St, Clutter } = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const Soup = imports.gi.Soup;
let ipMenuButton;
class IPMenu extends PanelMenu.Button {
constructor() {
super(0.0, 'External IP Viewer', false);
this.buttonText = new St.Label({
text: 'Fetching IP...',
y_align: Clutter.ActorAlign.CENTER
});
this.add_child(this.buttonText);
this._getIPInfo();
}
async _getIPInfo() {
const session = new Soup.Session();
const message = Soup.Message.new('GET', 'https://ipinfo.io/json');
session.send_message(message);
if (message.status_code === 200) {
const data = JSON.parse(message.response_body.data);
const ip = data.ip;
const country = data.country;
this.buttonText.set_text(\`IP: \${ip}, \${country}\`);
} else {
this.buttonText.set_text('Error fetching IP');
}
}
}
function init() {}
function enable() {
ipMenuButton = new IPMenu();
Main.panel.addToStatusArea('ip-menu', ipMenuButton);
}
function disable() {
ipMenuButton.destroy();
}
EOL
# Create stylesheet.css file (optional)
cat > "$EXTENSION_DIR/stylesheet.css" <<'EOL'
#ip-button {
padding: 0 6px;
color: white;
font-weight: bold;
}
EOL
# Set proper permissions for the extension files
chmod 644 "$EXTENSION_DIR/metadata.json" "$EXTENSION_DIR/extension.js" "$EXTENSION_DIR/stylesheet.css"
# Enable the extension
gnome-extensions enable "$EXTENSION_ID"
# Reload GNOME Shell to apply the changes
echo "Extension created and enabled. Reloading GNOME Shell..."
gnome-shell --replace &
disown
EOL
# Make the script executable
chmod +x create-gnome-extension.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment