reneklacan / PugLife

// ==UserScript==
// @name         PugLife
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  Replaces all images on FetLife with Pugs to make site SFW.
// @author       You
// @grant        none
// @run-at       document-end
// @license      MIT
// @match        *://fetlife.com/*
// @match        *://*.fetlife.com/*
// ==/UserScript==

(function () {
  'use strict';

  function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
  }

  function ignoreURL() {
    return document.location.href.startsWith("https://fetlife.com/admin/");
  }

  var imgs = [
    "https://i.ytimg.com/vi/wRx3Uvcktm8/maxresdefault.jpg",
    "https://img.freepik.com/free-photo/adorable-pug-puppy-solo-portrait_53876-64819.jpg",
    "https://img.freepik.com/free-photo/adorable-pug-puppy-solo-portrait_53876-64831.jpg",
    "https://img.freepik.com/free-photo/portrait-cute-pug-dog-isolated-dark-background_613910-8984.jpg",
    "https://img.freepik.com/free-photo/pug-dog-isolated-white-background_2829-11416.jpg",
    "https://img.freepik.com/free-photo/pug-dog-isolated-white-background_2829-11505.jpg",
    "https://img.freepik.com/free-photo/pug-is-resting-natural-parquet-tired-mops-dog-lies-floor-top-view_231208-7816.jpg",
    "https://img.freepik.com/free-photo/small-pug-sofa_231208-7777.jpg",
    "https://i.ytimg.com/vi/iCoVkMRc-Fk/maxresdefault.jpg",
    "https://i.ytimg.com/vi/y1D7MtUXWzY/maxresdefault.jpg",
    "https://i.ytimg.com/vi/NGq3QyWdhWY/maxresdefault.jpg",
    "https://i.ytimg.com/vi/v9nwtqqUq74/maxresdefault.jpg",
    "https://i.ytimg.com/vi/3pPR7vk-e3Y/maxresdefault.jpg",
  ];

  function replaceImages() {
    if (ignoreURL()) return;
    document
      .querySelectorAll('img')
      .forEach(function (img) {
        if (!img.attributes.pug) {
          img.src = imgs[getRandomInt(0, imgs.length)];
          img.srcset = "";
          img.setAttribute('pug', 'true');
        }
      });
  }

  // Function to start observing
  function startObserving() {
    // Use MutationObserver to observe changes in the DOM
    var observer = new MutationObserver(function (mutations, observer) {
      for (var mutation of mutations) {
        if (mutation.type === 'childList') {
          replaceImages();
        }
      }
    });

    // Configuration of the observer
    var config = {
      childList: true,
      subtree: true
    };

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

  // Check if the document is ready
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', startObserving);
  }
  else {
    startObserving();
  }

  // Initial replace on page load
  replaceImages();
})();