PAC MAN

replace ":v" with the pacman emoji

  1. // ==UserScript==
  2. // @name PAC MAN
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @description replace ":v" with the pacman emoji
  6. // @author Jamir-boop
  7. // @match *://*/*
  8. // @icon https://static.xx.fbcdn.net/images/emoji.php/v9/ef8/1.5/16/PACMAN.png
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to replace ":v" with an image
  17. function replacePacmanEmoji() {
  18. // Define the image to replace with
  19. const pacmanImage = '<img src="https://static.xx.fbcdn.net/images/emoji.php/v9/ef8/1.5/16/PACMAN.png" alt="Pacman" style="width:16px;height:16px;">';
  20.  
  21. // Get all the text nodes in the document
  22. const textNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
  23. let currentNode;
  24. while(currentNode = textNodes.nextNode()) {
  25. // Replace ":v" in the text node with the Pac-Man image
  26. const replacedText = currentNode.nodeValue.replace(/:v/g, pacmanImage);
  27. if(replacedText !== currentNode.nodeValue) {
  28. // Create a temporary div to hold the HTML
  29. const tempDiv = document.createElement('div');
  30. tempDiv.innerHTML = replacedText;
  31. // Replace the current text node with the new nodes
  32. while(tempDiv.firstChild) {
  33. currentNode.parentNode.insertBefore(tempDiv.firstChild, currentNode);
  34. }
  35. currentNode.parentNode.removeChild(currentNode);
  36. }
  37. }
  38. }
  39.  
  40. // Run the replacement function when the document loads
  41. window.addEventListener('load', replacePacmanEmoji);
  42.  
  43. // Optional: Observe for dynamic content changes and apply replacement
  44. const observer = new MutationObserver((mutations) => {
  45. mutations.forEach((mutation) => {
  46. if(mutation.addedNodes.length) replacePacmanEmoji();
  47. });
  48. });
  49.  
  50. observer.observe(document.body, { childList: true, subtree: true });
  51. })();