Optimize work experience at Microsoft

Optimize work experience at Microsoft!

// ==UserScript==
// @name         Optimize work experience at Microsoft
// @namespace    https://www.microsoft.com/
// @version      1.0.0
// @description  Optimize work experience at Microsoft!
// @author       Guosen Wang
// @match        https://ms.portal.azure.com/*
// @match        https://m365pulse.microsoft.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
// @run-at       document-start
// @grant        none
// ==/UserScript==
(function () {
  'use strict';
  const host = location.host;

  switch (true) {
    case 'ms.portal.azure.com' === host:
      azure();
      break;
    case 'm365pulse.microsoft.com' === host:
      m365pulse();
      break;
  }
})();

/**
 * General DOM handling function
 * @param {Array} targets - Array of targets to process, each object can include:
 *   - selector: CSS selector
 *   - remove: whether to remove the element
 *   - removeParent: whether to remove the parent element
 *   - text: text to match
 *   - modifyText: function to modify the element's text
 * @param {number} timeout - timeout in milliseconds (optional, default 10000)
 */
function handleDOMTargets(targets, timeout = 10000) {
  const processed = Array(targets.length).fill(false);
  let observer = null;

  function checkAndProcess() {
    targets.forEach((target, index) => {
      if (processed[index]) return;

      const element = document.querySelector(target.selector);
      if (!element) return;

      // Check text match if specified
      if (target.text && element.innerText !== target.text) return;

      // Remove element or parent element
      if (target.removeParent && element.parentElement) {
        element.parentElement.remove();
      } else if (target.remove) {
        element.remove();
      }

      // Modify text if modifyText function is provided
      if (target.modifyText) {
        element.innerText = target.modifyText(element.innerText);
      }

      processed[index] = true;
    });

    // Disconnect observer if all targets have been processed
    if (processed.every(Boolean) && observer) {
      observer.disconnect();
      observer = null;
      clearTimeout(timeoutId);
    }
  }

  // Run initial check immediately
  checkAndProcess();

  // If there are unprocessed targets, create an observer
  if (!processed.every(Boolean)) {
    observer = new MutationObserver(checkAndProcess);
    observer.observe(document.body, { childList: true, subtree: true });

    // Timeout protection
    const timeoutId = setTimeout(() => {
      if (observer) {
        observer.disconnect();
        observer = null;
      }
    }, timeout);
  }
}

function m365pulse() {
  handleDOMTargets([
    { selector: 'a.right:nth-child(1)', text: 'New Version', remove: true },
    { selector: 'div[class^="feedback-"]', removeParent: true }
  ], 10000);
}

function azure() {
  handleDOMTargets([
    { selector: '#_weave_e_6', remove: true },
    {
      selector: '#_weave_e_5 > div.fxs-topbar-internal.fxs-internal-full',
      modifyText: text => text.replace(' (Preview)', '')
    }
  ], 5000);
}