Skip to content

Instantly share code, notes, and snippets.

@judge2020
Last active December 9, 2023 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judge2020/de22e113a41adf26dbff53e4fd248817 to your computer and use it in GitHub Desktop.
Save judge2020/de22e113a41adf26dbff53e4fd248817 to your computer and use it in GitHub Desktop.
Replace x.com/twitter.com with vxtwitter.com in clipboard
# python -m pip install pyperclip
# credit gpt4
import pyperclip
import time
from urllib.parse import urlparse, urlunparse
# Function to check if the content in the clipboard is a URL
def is_url(content):
try:
result = urlparse(content)
return all([result.scheme, result.netloc])
except:
return False
# Function to replace the hostname for specific URLs
def replace_hostname(url):
parsed_url = urlparse(url)
if parsed_url.netloc in ["www.twitter.com", "www.x.com", "x.com", "twitter.com"]:
return urlunparse(parsed_url._replace(netloc="vxtwitter.com"))
return url
# Main loop to continuously check the clipboard
prev_content = ""
while True:
# Get the current content of the clipboard
current_content = pyperclip.paste()
# Check if the content has changed
if current_content != prev_content:
# Check if the new content is a URL
if is_url(current_content):
# Replace the hostname if necessary and copy back to clipboard
modified_url = replace_hostname(current_content)
if modified_url != current_content:
pyperclip.copy(modified_url)
print(f"Clipboard updated: {modified_url}")
# Update the previous content
prev_content = current_content
# Wait for a short period before checking again
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment