SL968

Hide or show Hashtag Symbol, enhance Links Userscript

Aug 15th, 2024 (edited)
20,794
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.01 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name        Toggle Hashtag Visibility and Enhance Links Userscript
  3. // @namespace   http://example.com
  4. // @include     https://mastodon.online/*
  5. // @include     https://mastodon.social/*
  6. // @version     2
  7. // @grant       none
  8. // ==/UserScript==
  9.  
  10. // Options
  11. const linkColor = '#00A0E9'; // Link color
  12. const makeLinksBold = true;  // Bold links
  13. let hideHashtags = true;     // Hide hashtags by default
  14.  
  15. // Store original text content
  16. const originalText = new WeakMap();
  17.  
  18. // Function to toggle hashtag visibility
  19. function toggleHashtagsVisibility() {
  20.   const links = document.querySelectorAll('a.mention.hashtag'); // Target hashtag links
  21.  
  22.   links.forEach(link => {
  23.     if (!originalText.has(link)) {
  24.       originalText.set(link, link.textContent); // Cache original text
  25.     }
  26.  
  27.     const original = originalText.get(link);
  28.     link.textContent = hideHashtags
  29.       ? original.replace(/#/g, '') // Remove hashtags
  30.       : original; // Restore hashtags
  31.   });
  32. }
  33.  
  34. // Function to style links
  35. function enhanceLinks() {
  36.   const links = document.querySelectorAll('a');
  37.  
  38.   links.forEach(link => {
  39.     link.style.color = linkColor; // Apply link color
  40.     link.style.fontWeight = makeLinksBold ? 'bold' : 'normal'; // Apply font weight
  41.   });
  42. }
  43.  
  44. // Debounced function to handle DOM changes
  45. let debounceTimeout;
  46. function handleDOMChanges() {
  47.   clearTimeout(debounceTimeout);
  48.   debounceTimeout = setTimeout(() => {
  49.     toggleHashtagsVisibility();
  50.     enhanceLinks();
  51.   }, 100); // Debounce updates to reduce strain
  52. }
  53.  
  54. // Observe DOM changes
  55. function observeDOMChanges() {
  56.   const observer = new MutationObserver(handleDOMChanges);
  57.  
  58.   observer.observe(document.body, {
  59.     childList: true,
  60.     subtree: true,
  61.   });
  62. }
  63.  
  64. // Toggle hashtags on F2 key
  65. document.addEventListener('keydown', event => {
  66.   if (event.key === 'F2') {
  67.     hideHashtags = !hideHashtags;
  68.     toggleHashtagsVisibility();
  69.   }
  70. });
  71.  
  72. // Initial calls
  73. toggleHashtagsVisibility();
  74. enhanceLinks();
  75. observeDOMChanges();
  76.  
Advertisement
Comments
  • SL968
    291 days
    # text 2.22 KB | 0 0
    1. To customize your Mastodon experience by hiding or displaying hashtags and enhancing link appearances, follow these steps to install and configure the userscript:
    2.  
    3. Step 1: Install a Userscript Manager
    4.  
    5. A userscript manager is required to run custom scripts in your browser. Popular options include:
    6.  
    7. Tampermonkey: Compatible with Chrome, Firefox, Edge, Safari, and more.
    8. Installation link: https://www.tampermonkey.net/
    9. Greasemonkey: Primarily for Firefox users.
    10. Installation link: https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
    11.  
    12. Choose the one that aligns with your browser and install it.
    13.  
    14. Step 2: Add the Userscript
    15.  
    16. Access the Userscript Manager:
    17. Click on the Tampermonkey or Greasemonkey icon in your browser's toolbar.
    18. Create a New Script:
    19. Select the option to create or add a new script.
    20. Retrieve the Script Code:
    21. Navigate to the Pastebin link provided above.
    22. Copy the entire script code from the Pastebin page.
    23. Paste the Script:
    24. In the userscript editor, paste the copied script code.
    25.  
    26. Step 3: Configure the Script
    27.  
    28. The script offers customization options:
    29.  
    30. Link Color:
    31. Find the line const linkColor = '#00A0E9';
    32. Replace '#00A0E9' with your preferred color code (e.g., '#FF5733' for a reddish hue).
    33. Bold Links:
    34. Locate const makeLinksBold = true;
    35. Set it to true to make links bold or false to keep them normal.
    36. Hide Hashtags:
    37. Identify const hideHashtags = true;
    38. Set it to true to hide hashtags or false to display them.
    39.  
    40. Note: If you're using a Mastodon instance other than mastodon.online or mastodon.social, add your instance's URL to the @include section of the script to ensure it functions correctly.
    41.  
    42. Step 4: Save and Activate the Script
    43.  
    44. Save the Script:
    45. Click the save button in the userscript editor to store your changes.
    46. Enable the Script:
    47. Ensure the script is active. You might need to refresh your Mastodon page for the changes to take effect.
    48.  
    49. By following these steps, you can tailor your Mastodon interface to better suit your preferences.
Add Comment
Please, Sign In to add comment