kinozal.tv: remove external scripts

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          kinozal.tv: remove external scripts
// @description   Remove external scripts (ads/tracking) on kinozal.tv
// @version       2.0
// @author        wOxxOm
// @namespace     https://greasyfork.org/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);