substack_popup_dismisser

dismiss popups from substack

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        substack_popup_dismisser
// @namespace   https://github.com/Safrone
// @version     0.2
// @description dismiss popups from substack
// @license     Unlicense
// @homepageURL https://github.com/Safrone/substack_popup_dismisser
// @supportURL  https://github.com/Safrone/substack_popup_dismisser/issues
// @match       https://*.substack.com/*
// @match       http://*/*
// @match       https://*/*
// @grant       GM.xmlHttpRequest
// @run-at      document-end
// ==/UserScript==

"use strict";

if(!isSubstackSite())
{
  return;
}

console.debug("substack_popup_dismisser active");

(new MutationObserver(makeLogWrappedCallback(checkForPopups))).observe(
  document,
  {childList: true, subtree: true});

function isSubstackSite()
{
  // Always match *.substack.com
  if(location.hostname.endsWith(".substack.com"))
  {
    return true;
  }

  // Check for substackcdn.com resources (scripts, stylesheets, preconnect links)
  if(document.querySelector('script[src*="substackcdn.com"], link[href*="substackcdn.com"]'))
  {
    return true;
  }

  // Check for Substack's webpack chunk on window
  if(typeof window.webpackChunksubstack !== "undefined")
  {
    return true;
  }

  return false;
}

function execLogWrappedFunc(funcToWrap, ...funcArgs)
{
  try
  {
    return funcToWrap(...funcArgs);
  }
  catch(err)
  {
    console.error(err);
    throw err;
  }
}

function makeLogWrappedCallback(funcToWrap)
{
  return function(...funcArgs) { return execLogWrappedFunc(funcToWrap, ...funcArgs); };
}

function getXPathResult(xpathExpression)
{
  var xPathResult = document.evaluate(xpathExpression, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  //console.debug("xPathResult", xPathResult);
  var firstResult = xPathResult.iterateNext();
  //console.debug("firstResult", firstResult);
  return firstResult;
}

function checkForPopups(changes, observer)
{
  let xpaths = [
    "//div[text()='Continue reading']",
    "//button[text()='Continue reading']",
    "//div[text()='No thanks']",
    "//button[text()='No thanks']",
  ];

  for(let xpath of xpaths)
  {
    let dismissalElem = getXPathResult(xpath);
    if(dismissalElem)
    {
      dismissalElem.click();
      console.debug(
        "substack_popup_dismisser clicked \"" + xpath + "\", relevant mutation list",
        changes);
    }
  }
}