leobm / Twitter Switch To Professional Panel Overlay Remover

/**
  The MIT License (MIT)

  Copyright (c) 2021 Jan-Felix Wittmann

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
// ==UserScript==
// @id           TwitterSwitchToProfessionalPanelOverlayRemover
// @name         Twitter Switch To Professional Panel Overlay Remover
// @description  Removes the "Switch to professional profile" panel from the tweet analytics dialog
// @version      1.1
// @author       Jan-Felix Wittmann <jfwittmann7@gmail.com>
// @copyright    2021, leobm (https://openuserjs.org/users/leobm)
// @license      MIT
// @icon         https://www.google.com/s2/favicons?domain=twitter.com
// @match        https://twitter.com/*
// @include      /^https?://twitter\.com/.*$/
// @include      /^https?://twitter\.com/[^/]+/status/\d+/analytics$/
// @run-at       document-start
// @grant        none
// @namespace    https://greasyfork.org/users/844851
// @downloadURL  https://openuserjs.org/install/leobm/Twitter_Switch_To_Professional_Panel_Overlay_Remover.user.js
// @updateURL    https://openuserjs.org/meta/leobm/Twitter_Switch_To_Professional_Panel_Overlay_Remover.meta.js
// ==/UserScript==

/*jshint esversion: 9 */

(function (d, w) {
  // init dialog convert to professional button watcher
  watchForQuerySelectorOnce(d, 'div[role=dialog] a[href="/i/flow/convert_to_professional"]', (obs) => {
    // remove convert to professional notifications panel
    let notifyPanel = obs.foundNode.parentNode.parentNode;
    notifyPanel.remove();

    // init dialog close watcher
    let dialog = query(d, "div[role=dialog]")[0];
    // restart observe of dialog - convert to professional button
    watchForNodeRemovedOnce(d, dialog.parentNode, obs.restart);
  });

  //**********************************************************************************
  // My DOM Utilities
  //**********************************************************************************

  function watchForQuerySelectorOnce(targetNode, selector, callback, options) {
    let obsCallback = (obs) => {
      callback(obs);
      obs.stop();
    };
    watchForQuerySelector(targetNode, selector, obsCallback, options);
  }

  function watchForQuerySelector(targetNode, selector, callback, options) {
    let obsCallback = (obs) => {
      let foundNode = query(targetNode, selector)[0];
      if (foundNode) {
        callback({
          foundNode,
          ...obs
        });
      }
    };
    observeNode(targetNode, obsCallback, options);
  }

  function watchForNodeRemovedOnce(targetNode, affectedNode, callback, options) {
    let obsCallback = (obs) => {
      obs.mutations.forEach((mutation) => {
        let removedNode = Array.from(mutation.removedNodes)
          .find(node => node === affectedNode);
        if (removedNode) {
          callback({
            removedNode,
            ...obs
          });
          obs.stop();
        }
      });
    };
    observeNode(targetNode, obsCallback, options);
  }

  function observeNode(targetNode, callback, options) {
    let meOptions = options || {
      childList: true,
      subtree: true,
    };
    let observer = new MutationObserver((mutations, me) => {
      let obs = {
        restart: () => me.observe(targetNode, meOptions),
        stop: () => me.disconnect(),
        get mutations() {
          return mutations;
        },
      };
      callback(obs);
    });
    observer.observe(targetNode, meOptions);
    return observer;
  }

  function query(startNode, selector) {
    try {
      var nodes = Array.from(startNode.querySelectorAll(selector));
      return nodes;
    }
    catch (e) {}
    return [];
  }
})(document, window);