adam4adam remove ads

removes annoying ads from adam4adam*.com sites, tested in MS Edge and Firefox

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name        adam4adam remove ads
// @namespace   Violentmonkey Scripts
// @icon
// @version     0.1.2
// @license     MIT
//
// @match       http*://*.adam4adam*.com/*
// @grant       none
//
// @author      [email protected]
// @description removes annoying ads from adam4adam*.com sites, tested in MS Edge and Firefox
// ==/UserScript==

/* jshint esversion: 8 */

// each line is a regex anchored to start of class name via '^'
// and to the end of class name via $
var classRegexTargetSelfStr = `
promo-bar-ads
(banner-)?vip.*
ads-.+
header-banner
mys-wrapper
adsbygoogle.*
stories-feed-container
`;

// special case:  easier to identify the child of an elt we need
// to remove than the elt itself
var classRegexTargetParentStr = `
sticky-block
`;

const LOG_TAG = "a4a de-ad-ifier :: ";
var iframesRemoved = false;



function constructRegex(multilineStr) {
  return new RegExp("^(" + multilineStr.split(`\n`)
                                       .join(`|`)
                                       .replace(/^\|/, ``)
                                       .replace(/\|$/, ``) +
                                       ")$");
}
const classRegexTargetSelf = constructRegex(classRegexTargetSelfStr);
const classRegexTargetParent = constructRegex(classRegexTargetParentStr)


function removeByClassRegex(classRegex, removeParent) {
  var pendingRemoval = [];
  Array.from(document.querySelectorAll('*')).forEach(e => {
    if(classRegex.test(e.className)) {
        pendingRemoval.push(e);
    }
  });

  pendingRemoval.forEach(e => {
    console.log(LOG_TAG + "removing element with class='" + e.className + "'");
    try {
      if (removeParent) {
        e.parentNode.remove();
      } else {
        e.remove();
      }
    } catch(error) {
      console.log(LOG_TAG + "Error encountered:  " + error);
    }
  });

  return pendingRemoval.length;
}


var totalItn = 0;

const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function runWithPostDelay(itn, ms) {
  for(var i = 0; i < itn; i++) {
    totalItn++;
    console.log(LOG_TAG + "run #" + totalItn + ":  a4a removing ad-related elements");

    var removedCount = 0;
    removedCount += removeByClassRegex(classRegexTargetParent, true);
    removedCount += removeByClassRegex(classRegexTargetSelf, false);

    if(! iframesRemoved) {
      var numIframesRemoved = 0;
      Array.from(document.getElementsByTagName("iframe")).map(e => {
        iframesRemoved = true;
        e.remove();
      });
    }
    console.log(LOG_TAG + "sleeping for " + (ms/1000) + "sec before running again");
    await wait(ms);
  }
}

// run up to 4 times, each iteration waiting 1.5s before next iteration (or exiting)
runWithPostDelay(4,1500);

// run up to 4 additional times, each iteration waiting 3s before next iteration (or exiting)
runWithPostDelay(4,3000);

console.log(LOG_TAG + "finished!");