Trumpeta / XTV.CZ anti-adblock

// ==UserScript==
// @name         XTV.CZ anti-adblock
// @namespace    https://greasyfork.org/cs/users/198317-trumpeta
// @version      1.02
// @description  Potlačení otravného okna kvůli AdBlocku
// @author       Trumpeta
// @run-at       document-body
// @match        https://xtv.cz/*
// @match        http://xtv.cz/*
// @copyright    2020, Trumpeta (https://openuserjs.org/users/Trumpeta)
// @license      GPL-3.0-or-later
// ==/UserScript==

var layer = document.querySelector('template#pgblck-tmpl');
if (layer != null) layer.remove();

const scriptPath = '/js/public.min.js',
	  rx = /\b(?:\w\.ads\.init)\(\),/g;

document.onbeforescriptexecute = function(evt) {
  if (!evt.target.src.toLowerCase().includes(scriptPath)) return;
  evt.preventDefault();
  evt.stopPropagation();
  var xhr = new XMLHttpRequest;
  xhr.open('GET', evt.target.src, false);
  xhr.send();
  if (xhr.status < 200 || xhr.status >= 400) {
	console.error('Error loading script:', evt.target.src, xhr)
	return;
  }
  var newScript = document.createElement('script');
  newScript.type = "text/javascript";
  newScript.textContent = xhr.responseText.replace(rx, '');
  document.head.append(newScript);
};

for (var script of document.getElementsByTagName('SCRIPT')) {
  if (!script.src || !script.src.toLowerCase().includes(scriptPath)) continue;
  script.remove();
  var xhr = new XMLHttpRequest;
  xhr.open('GET', script.src, false);
  xhr.send();
  if (xhr.status < 200 || xhr.status >= 400) {
	console.error('Error loading script:', script.src, xhr)
	return;
  }
  script.removeAttribute('src');
  script.type = 'text/javascript';
  script.textContent = xhr.responseText.replace(rx, '');
  document.head.append(script);
}