kinozal.tv: remove external scripts

Remove external scripts (ads/tracking) on kinozal.tv

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          kinozal.tv: remove external scripts
// @description   Remove external scripts (ads/tracking) on kinozal.tv
// @version       2.0
// @author        wOxxOm
// @namespace     https://greatest.deepsurf.us/users/2159-woxxom
// @license       MIT License
// @match         http://kinozal.tv/*
// @run-at        document-start
// ==/UserScript==

stop();

(function overwrite(link) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', link.href);
  xhr.onload = () => {
    var html = xhr.responseText
      .replace(/<script\b[\s\S]*?<\/script>/g, s => {
        if (s.includes('fromCharCode') ||
            s.includes('iframe') ||
            s.includes('document.write') ||
            s.includes('document.createElement("script")') ||
            s.includes("document.createElement('script')") ||
            /^[^>]+?src=['"][^'"/]*\/\//.test(s)) {
          // console.debug(GM_info.script.name + ': ' + s);
          return '';
        } else {
          return s;
        }
      })
      .replace(/<(object|iframe)\s[\s\S]*?<\/\1>/g, ''); // strip swfs
    document.open();
    document.write(html);
    document.close();

    document.querySelector('[id*="ScriptRoot"]').parentNode.remove();

    if (link.nodeName)
      history.pushState(0, document.title, link.href);

    var prevUrl = location.href;
    window.addEventListener('popstate', e => {
      if (!prevUrl.includes('#') && !location.href.includes('#'))
        overwrite(location);
    });

    window.addEventListener('click', e => {
      var a = e.target.closest('a');
      if (a && !a.onclick && a.hostname == location.hostname) {
        e.preventDefault();
        overwrite(a);
      }
    });
  };
  xhr.send();
})(location);