Greasy Fork

跟踪链接净化(B站, 百度)

净化B站跟踪链接(推荐视频),百度<相关搜索>链接净化

目前为 2023-01-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         跟踪链接净化(B站, 百度)
// @name:zh-TW   跟蹤鏈接凈化(B站, 百度)
// @name:en      Clean Tracking URLs (Bilibili, Baidu)
// @namespace    https://greasyfork.org/en/scripts/456881
// @version      0.3.7.1
// @description  净化B站跟踪链接(推荐视频),百度<相关搜索>链接净化
// @description:zh-tw 凈化B站鏈接跟蹤(推薦視頻),百度<相關搜索>鏈接凈化
// @description:en Clean Bilibili Tracking URLs (Recommended Videos), Baidu <Related Search> URLs.
// @author       cilxe
// @match        *://www.bilibili.com/*
// @match        *://space.bilibili.com/*
// @match        *://search.bilibili.com/*
// @match        *://www.baidu.com/s*
// @icon         https://www.bilibili.com/favicon.ico
// @run-at       document-start
// @license      MIT
// ==/UserScript==

/*
# Changelog
v0.3.7.1 2023-01-02
- Fixed [space.bilibili.com] effects after paging.
- Added support to clean tracking url at [search.bilibili.com].

v0.3.7 2023-01-02
- Naming optimisation.
- Script handling optimisation. (Bilibili)
- Added support to block part of Bilibili Ads.

v0.3.6 2022.12.28
Optimise Baidu related search URL, paging URL processing method.

v0.3.5 2022.12.27
Script logic optimisation.

*/

(() => {
  // const sites = ['bilibili', 'baidu', 'youtube'];
  const TIME_DELAY = 3000;
  const INDEX_STRING_QM = '?';
  // bilibili index strings
  const INDEX_STRING_B1 = 'spm_id_from';
  const INDEX_STRING_B2 = 'vd_source';
  const INDEX_STRING_B3 = 'from_spmid';
  // baidu index strings
  const INDEX_STRING_BD1 = 'rsf';
  const INDEX_STRING_BD2 = 'rsv_pq';

  // Bilibili Restore history state session stack
  function restoreState(indexStr) {
    const URL = window.location.href;
    const VideoIndex = URL.indexOf(indexStr);
    const CleanURL = URL.substring(0, VideoIndex - 1);
    window.history.replaceState({}, 'Restore', CleanURL);
  }

  // remove bilibili Ads
  function removeBiliAds() {
    const divs = document.getElementsByClassName('eva-banner');
    for (let i = 0; i < divs.length; i += 1) {
      divs[i].remove();
    }
  }

  // www.bilibili.com/*, ww.bilibili.com/v/popular/*
  function cleanMainURL() {
    function onFresh() {
      const metas = document.getElementsByTagName('meta');
      for (let i = 0; i < metas.length; i += 1) {
        if (metas[i].name === 'spm_prefix') {
          metas[i].content = '000.0000';
        }
      }
      // 净化滚动卡片链接
      const SCROLL_VIDEOS_1 = document.getElementsByClassName('carousel-inner');
      const SCROLL_VIDEOS_2 = document.getElementsByClassName('carousel-item');
      for (let i = 0; i < SCROLL_VIDEOS_1.length; i += 1) {
        SCROLL_VIDEOS_1[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      for (let i = 0; i < SCROLL_VIDEOS_2.length; i += 1) {
        const url = SCROLL_VIDEOS_2[i].href;
        const index = url.indexOf(INDEX_STRING_B1);
        const leftURL = url.substring(0, index - 1);
        const url2 = SCROLL_VIDEOS_2[i].getAttribute('data-target-url');
        const index2 = url2.indexOf(INDEX_STRING_B1);
        const leftURL2 = url2.substring(0, index2);
        if (url.includes(INDEX_STRING_B1) || url2.includes(INDEX_STRING_B1)) {
          SCROLL_VIDEOS_2[i].href = leftURL;
          SCROLL_VIDEOS_2[i].setAttribute('data-target-url', leftURL2);
        }
      }
      // Remove tracking event
      const frontCardVideos = document.getElementsByClassName('recommended-card');
      for (let i = 0; i < frontCardVideos.length; i += 1) {
        frontCardVideos[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      const frontCardVideos2 = document.getElementsByClassName('bili-video-card__image--wrap');
      for (let i = 0; i < frontCardVideos2.length; i += 1) {
        frontCardVideos2[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      // homepage menu-item
      const menus = document.getElementsByClassName('channel-icons__item');
      for (let i = 0; i < menus.length; i += 1) {
        menus[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      // 视频封面事件
      const links = document.getElementsByTagName('a');
      for (let i = 0; i < links.length; i += 1) {
        links[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      const frontImgsA = document.getElementsByTagName('img');
      for (let i = 0; i < frontImgsA.length; i += 1) {
        frontImgsA[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      const frontImgsB = document.getElementsByTagName('picture');
      for (let i = 0; i < frontImgsB.length; i += 1) {
        frontImgsB[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      // v-img bili-bangumi-card__cover  v-img bili-video-card__cover
      const frontImgsC = document.getElementsByClassName('v-img');
      for (let i = 0; i < frontImgsC.length; i += 1) {
        frontImgsC[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
      // watch-later van-watchlater black
      const afterVideos = document.getElementsByClassName('watch-later');
      for (let i = 0; i < afterVideos.length; i += 1) {
        afterVideos[i].addEventListener('click', (event) => {
          event.stopPropagation();
        }, true);
      }
    }
    // In case of page loading incomplete, add time out.
    setTimeout(() => { onFresh(); }, TIME_DELAY);
    // Loop execution while scrolling
    window.onscroll = () => {
      let topScroll = 0;
      // Scroll range
      const scrolls = document.documentElement.scrollTop || document.body.scrollTop;
      if (scrolls - topScroll > 200) {
        onFresh();
        topScroll = scrolls;
      }
    };
  }

  function cleanURL() {
    // clean <a> links
    const links = document.getElementsByTagName('a');
    for (let i = 0; i < links.length; i += 1) {
      const url = links[i].href;
      const index = url.indexOf(INDEX_STRING_QM);
      if (links[i].href.includes(INDEX_STRING_B1) || links[i].href.includes(INDEX_STRING_B3)) {
        links[i].href = url.substring(0, index);
      }
    }
    const videos = document.getElementsByClassName('video-awesome-img');
    for (let i = 0; i < videos.length; i += 1) {
      videos[i].addEventListener('click', (event) => {
        event.stopPropagation();
      }, true);
      // let url = videos[i].href;
      // let index = url.indexOf(INDEX_STRING_QM);
      // videos[i].href = url.substring(0,index);
    }
    const frontImgsA = document.getElementsByTagName('img');
    for (let i = 0; i < frontImgsA.length; i += 1) {
      frontImgsA[i].addEventListener('click', (event) => {
        event.stopPropagation();
      }, true);
    }
    // www.bilibili.com/v/virtual/  音乐
    const coverDivs = document.getElementsByClassName('spread-module');
    for (let i = 0; i < coverDivs.length; i += 1) {
      coverDivs[i].addEventListener('click', (event) => {
        event.stopPropagation();
      }, true);
    }
  }

  // space.bilibili.com/*
  function cleanSpaceURL() {
    function cleanSpaceLinks() {
      const links = document.getElementsByTagName('a');
      for (let i = 0; i < links.length; i += 1) {
        links[i].addEventListener('click', (e) => {
          e.stopPropagation();
          cleanSpaceLinks();
          setTimeout(() => { cleanSpaceLinks(); }, TIME_DELAY - 1000);
        }, true);
      }
    }
    cleanSpaceLinks();
    // paging
    const pageLinks = document.getElementsByClassName('be-pager');
    for (let i = 0; i < pageLinks.length; i += 1) {
      pageLinks[i].addEventListener('click', () => {
        cleanSpaceLinks();
        setTimeout(() => { cleanSpaceLinks(); }, TIME_DELAY - 1000);
      });
    }
    // navigation bar click
    const naviLinks = document.getElementsByClassName('text');
    for (let i = 0; i < naviLinks.length; i += 1) {
      naviLinks[i].addEventListener('click', () => {
        cleanSpaceLinks();
        setTimeout(() => { cleanSpaceLinks(); }, TIME_DELAY - 1000);
      });
    }
  }

  // www.bilibili.com/video/*
  function cleanVideoURL() {
    // Restore State session
    restoreState(INDEX_STRING_B2);
    // loop execution while scrolling
    window.onscroll = () => {
      let topScroll = 0;
      const scrolls = document.documentElement.scrollTop || document.body.scrollTop;
      if (scrolls - topScroll > 200) {
        cleanURL();
        removeBiliAds();
        topScroll = scrolls;
      }
    };
    const unfoldVideos = document.getElementsByClassName('rec-footer');
    unfoldVideos[0].onclick = () => { cleanURL(); };
  }

  // Baidu related search URL cleaning
  function cleanBaiduRelatedURL() {
    function cleanRS() {
      // <a> class = "c-color-link"
      const links = document.getElementsByClassName('c-color-link');
      for (let i = 0; i < links.length; i += 1) {
        if (links[i].href.includes(INDEX_STRING_BD1)) {
          const index = links[i].href.indexOf(INDEX_STRING_BD1);
          links[i].href = links[i].href.substring(0, index - 1);
        }
      }
    }
    // bottom of html paging block
    function clearPages() {
      const pageLinks = document.getElementById('page').getElementsByTagName('div')[0].getElementsByTagName('a');
      for (let i = 0; i < pageLinks.length; i += 1) {
        if (pageLinks[i].href.includes(INDEX_STRING_BD2)) {
          const index = pageLinks[i].href.indexOf(INDEX_STRING_BD2);
          pageLinks[i].href = pageLinks[i].href.substring(0, index - 1);
        }
      }
    }
    window.onscroll = () => {
      let topScroll = 0;
      const scrolls = document.documentElement.scrollTop || document.body.scrollTop;
      if (scrolls - topScroll > 200) {
        cleanRS();
        clearPages();
        topScroll = scrolls;
      }
    };
  }

  // Handle different sites
  window.onload = () => {
    const realLocation = window.location;
    const isBaidu = realLocation.hostname.includes('baidu.com');

    const isBilibili = realLocation.hostname.includes('bilibili.com');
    const isBMain = realLocation.hostname.includes('www.bilibili.com');
    const isBSpace = realLocation.href.includes('space.bilibili.com');
    const isSearch = realLocation.href.includes('search.bilibili.com');
    // bilibili
    if (isBilibili) {
      // Restore history state session stack
      if (realLocation.href.includes(INDEX_STRING_B1)) {
        restoreState(INDEX_STRING_B1);
      } else if (realLocation.href.includes(INDEX_STRING_B2)) {
        restoreState(INDEX_STRING_B2);
      }
      if (isBMain || isSearch) {
        setTimeout(() => { cleanVideoURL(); }, 500);
        setTimeout(() => { cleanURL(); }, 500);
        setTimeout(() => { cleanVideoURL(); }, TIME_DELAY);
        setTimeout(() => { cleanURL(); }, TIME_DELAY);
        restoreState(INDEX_STRING_B1);
        restoreState(INDEX_STRING_B2);
        cleanMainURL();
      } else if (isBSpace) {
        setTimeout(() => { cleanSpaceURL(); }, 1000);
        setTimeout(() => { cleanSpaceURL(); }, TIME_DELAY);
      }
    } else if (isBaidu) { // baidu <related search>
      cleanBaiduRelatedURL();
    }
  };
})();

// function declaration methods:
// function functionName() {};  window.onload = functionName()
// window.onload = function() {}
// window.onload = () => {}