Userscript to remove Hashtag symbol "#" from Links

Strips hashtag symbol from hashtag links
mail@pastecode.io avatar
unknown
javascript
a year ago
1.3 kB
21
Indexable
// ==UserScript==
// @name        Remove Hashtags from Link Text
// @namespace   http://example.com

// Add sites that you wish to remove hashtags on in the below format 
// @include     https://mastodon.online/*
// @include     https://mastodon.social/*

// @version     1
// @grant       none
// ==/UserScript==

// Function to remove "#" from link text
function removeHashtagsFromText() {
  // Get all anchor tags
  const links = document.querySelectorAll('a');

  // Iterate through all links
  links.forEach(link => {
    // Check if the text content includes "#"
    if (link.textContent.includes("#")) {
      // Replace "#" character in the link text
      link.textContent = link.textContent.replace(/#/g, '');
    }
  });
}

// Function to observe changes in the DOM
function observeDOMChanges() {
  // Create a new MutationObserver instance
  const observer = new MutationObserver(() => {
    // Run the function to remove hashtags from link text
    removeHashtagsFromText();
  });

  // Options for the observer
  const config = {
    childList: true,
    subtree: true,
  };

  // Start observing the body of the page
  observer.observe(document.body, config);
}

// Run the function to remove hashtags from link text initially
removeHashtagsFromText();
// Run the function to observe changes in the DOM
observeDOMChanges();