navchandar / Comic View

// ==UserScript==
// @name            Comic View
// @version         0.1
// @description     scroll to view the comic and remove the clutters
// @run-at          document-end
// @author          navchandar
// @copyright       2019, navchandar(https://github.com/navchandar)
// @license         MIT
// @include         https://donthitsave.com/
// @include         https://donthitsave.com
// @include         http://donthitsave.com/
// @match           https://donthitsave.com/*
// @include         https://xkcd.com/
// @include         https://xkcd.com
// @include         https://www.explainxkcd.com/
// @include         https://www.explainxkcd.com
// @match           https://www.explainxkcd.com/*
// @match           https://www.explainxkcd.com*
// @match           https://xkcd.com/*
// @match           http://xkcd.com/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @downloadURL     https://openuserjs.org/install/navchandar/Comic_View.user.js
// @updateURL       https://openuserjs.org/meta/navchandar/Comic_View.meta.js
// ==/UserScript==

/*--- waitForKeyElements():  A utility function, for Greasemonkey scripts,
    that detects and handles AJAXed content.

    Usage example:
        waitForKeyElements (
            "div.comments"
            , commentCallbackFunction
        );

        //--- Page-specific function to do what we want when the node is found.
        function commentCallbackFunction (jNode) {
            jNode.text ("This comment changed by waitForKeyElements().");
        }
    IMPORTANT: This function requires your script to have loaded jQuery.
*/

function waitForKeyElements(selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
  var targetNodes, btargetsFound;
  if (typeof iframeSelector == "undefined")
    targetNodes = $(selectorTxt);
  else
    targetNodes = $(iframeSelector).contents()
    .find(selectorTxt);

  if (targetNodes && targetNodes.length > 0) {
    btargetsFound = true;
    /*--- Found target node(s).  Go through each and act if they
        are new.
    */
    targetNodes.each(function () {
      var jThis = $(this);
      var alreadyFound = jThis.data('alreadyFound') || false;

      if (!alreadyFound) {
        //--- Call the payload function.
        var cancelFound = actionFunction(jThis);
        if (cancelFound)
          btargetsFound = false;
        else
          jThis.data('alreadyFound', true);
      }
    });
  }
  else {
    btargetsFound = false;
  }

  //--- Get the timer-control variable for this selector.
  var controlObj = waitForKeyElements.controlObj || {};
  var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  var timeControl = controlObj[controlKey];

  //--- Now set or clear the timer as appropriate.
  if (btargetsFound && bWaitOnce && timeControl) {
    //--- The only condition where we need to clear the timer.
    clearInterval(timeControl);
    delete controlObj[controlKey]
  }
  else {
    //--- Set a timer, if needed.
    if (!timeControl) {
      timeControl = setInterval(function () {
        waitForKeyElements(selectorTxt, actionFunction, bWaitOnce, iframeSelector);
      }, 300);
      controlObj[controlKey] = timeControl;
    }
  }
  waitForKeyElements.controlObj = controlObj;
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function DontHitSave() {
  if (document.getElementById("banner-bg") != null) {
    document.getElementById("banner-bg").remove();
  }
  if (document.getElementById("menu-container") != null) {
    document.getElementById("menu-container").remove();
  }
  if (document.getElementsByClassName("logobar").length > 0) {
    document.getElementsByClassName("logobar")[0].remove();
  }
  if (document.getElementsByClassName("page-banner").length > 0) {
    document.getElementsByClassName("page-banner")[0].remove();
  }
  if (document.getElementsByClassName("logobar").length > 0) {
    document.getElementsByClassName("logobar")[0].remove();
  }
  if (document.getElementsByClassName("comicfull").length > 0) {
    document.getElementsByClassName("comicfull")[0].scrollIntoView({
      behavior: 'smooth'
    });
  }
}

function XKCD() {
  document.getElementById("ctitle").scrollIntoView({
    behavior: 'smooth'
  });
}

function explainXKCD() {
  document.getElementById("Latest_comic").scrollIntoView({
    behavior: 'smooth'
  });
}

(async function () {
  'use strict';
  await sleep(1000);

  if (window.location.href.indexOf("donthitsave") > -1) {
    waitForKeyElements(".logobar", DontHitSave, true);
  }
  else if (window.location.href.indexOf("explainxkcd") > -1) {
    waitForKeyElements(".mw-headline", explainXKCD, true);
  }
  else if (window.location.href.indexOf("xkcd") > -1) {
    waitForKeyElements(".box", XKCD, true);
  }

})();