freeos / Facebook Cleaner

// ==UserScript==
// @name        Facebook Cleaner
// @namespace   https://openuserjs.org/scripts/freeos
// @version     1.12
// @author      -
// @description Cleans the Facebook UI
// @copyright   2019, freeos (https://openuserjs.org/users/freeos)
// @license     MIT; https://opensource.org/licenses/MIT
// @include     https://www.facebook.com/*
// @include     https://facebook.com/*
// @grant       GM_addStyle
// ==/UserScript==

let processOptions = true

let options = {
  hideWatch: true,
  hideMarketplace: true,
  hideGaming: true,
  hideLeftSideBar: false,
  hideRightSideBar: true,
  hideCovid: true,
  hideCreatePostTypes: false,
  hideStories: false,
  hideVideoChat: false,
  hideSuggestedGroups: false,
  hideSponsored: true,
  hidePaidPartnerships: true,
  hideFooter: true,
  hideLoopInterval: 1000
}

let hideClass = 'displayNoneImportant'

let onDocReady = function(){
  //console.log('domain: ' + document.domain)
  if (processOptions) {
    switch (document.domain) {
      case "facebook.com":

        addCSSClasses()
        removeOnce()

        removeLoop()
        setInterval(removeLoop, options.hideLoopInterval)
        break;
    }
  }
}


function removeLoop() {
  if(options.hideCovid) { removeCovid() }
  if(options.hideSponsored) { removeSponsored() }
  if(options.hidePaidPartnerships) { removePaidPartnerships() }
}

function removeSponsored() {
  hideXPath("//div[@aria-label='Sponsored']/ancestor::div[starts-with(@data-pagelet,'FeedUnit')]")
  hideXPath("//a[@aria-label='Sponsored']/ancestor::div[starts-with(@data-pagelet,'FeedUnit')]")
  hideXPath("//div[text()='Sponsored']/ancestor::div[starts-with(@data-pagelet,'FeedUnit')]")
}

function removePaidPartnerships() {
  hideXPath("//span[text()='Suggested Groups']/ancestor::div[starts-with(@data-pagelet,'FeedUnit')]")
}

function removeSuggestedGroups() {
  hideXPath("//div[text()='Paid Partnership']/ancestor::div[starts-with(@data-pagelet,'FeedUnit')]")
}

function removeCovid() {
  hideXPath("//span[text()='COVID-19 Information Center']/ancestor::li")
}

function removeOnce() {
  let cssWatch = `
    div[role="navigation"][aria-label="Facebook"]>ul>li:nth-child(2) { /* watch */
      display: none !important;
    }
  `;
  let cssMarketplace = `
    div[role="navigation"][aria-label="Facebook"]>ul>li:nth-child(3) { /* gaming */
      display: none !important;
    }
  `;
  let cssGaming = `
    div[role="navigation"][aria-label="Facebook"]>ul>li:nth-child(5) { /* gaming */
      display: none !important;
    }
  `;
  let cssLeftSideBar = `
    div[data-pagelet="LeftRail"] {  /* left side bar */
      visibility: hidden !important;
    }
  `;
  let cssRightSideBar = `
    div[role="complementary"] { /* right side bar */
      visibility: hidden !important;
    }
  `;
  let cssFooter = `
    footer {
      visibility: hidden !important;
    }
  `;
  let cssCreatePostTypes = `
    div[aria-label="Create a post"]>div:nth-child(2) {
      display: none !important;
    }
  `;
  let cssStories = `
    div[aria-label="Stories"] {
      display: none !important;
    }
  `;
  let cssVideoChat = `
    div[data-pagelet="VideoChatHomeUnit"] {
      display: none !important;
    }
  `;
  
  if(options.hideWatch) { GM_addStyle(cssWatch); }
  if(options.hideMarketplace) { GM_addStyle(cssMarketplace); }
  if(options.hideGaming) { GM_addStyle(cssGaming); }
  if(options.hideLeftSideBar) { GM_addStyle(cssLeftSideBar); }
  if(options.hideRightSideBar) { GM_addStyle(cssRightSideBar); }
  if(options.hideFooter) { GM_addStyle(cssFooter); }
  if(options.hideCreatePostTypes) { GM_addStyle(cssCreatePostTypes); }
  if(options.hideStories) { GM_addStyle(cssStories); }
  if(options.hideVideoChat) { GM_addStyle(cssVideoChat); }
  if(options.hideSuggestedGroups) { removeSuggestedGroups() }
  
}

function addCSSClasses() {
  let css = `
    .displayNoneImportant {
      display: none !important;
    }
    .visibilityHiddenImportant {
      visibility: hidden !important;
    }
  `;

  GM_addStyle(css);
}

function hideXPath(xPathStr) {
  let matchingElement = document.evaluate(xPathStr, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
  if(matchingElement) {
    for(var i = 0; i < matchingElement.snapshotLength; i++) {
      let curElement = matchingElement.snapshotItem(i)
      if(!curElement.classList.contains(hideClass)) {
        curElement.className += ' ' + hideClass
      }
    }
  }
}

if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {
  onDocReady()
} else {
  document.addEventListener("DOMContentLoaded", onDocReady)
}