Last active
December 9, 2023 05:45
-
-
Save judge2020/de22e113a41adf26dbff53e4fd248817 to your computer and use it in GitHub Desktop.
Replace x.com/twitter.com with vxtwitter.com in clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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