Optimize work experience at Microsoft

Optimize work experience at Microsoft!

As of 15. 09. 2025. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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/*
// @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);
}