// ==UserScript==
// @name 跟踪链接净化(B站, 百度)
// @name:zh-TW 跟蹤鏈接凈化(B站, 百度)
// @name:en Clean Tracking URLs (Bilibili, Baidu)
// @namespace https://greasyfork.org/en/scripts/456881
// @version 0.3.8.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 *://search.bilibili.com/*
// @match *://space.bilibili.com/*
// @match *://live.bilibili.com/*
// @match *://www.baidu.com/s*
// @icon https://www.bilibili.com/favicon.ico
// @run-at document-start
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @license MIT
// ==/UserScript==
(() => {
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';
// If <true> block [Lucky Draw (The Selection)] popups at live.bilibili.com.
const BlockLivePopups = false;
// 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() {
let index = 0;
do {
// Card-Ads
const cardAds = document.getElementsByTagName('a');
for (let i = 0; i < cardAds.length; i += 1) {
if (cardAds[i].hostname.includes('cm.bilibili.com')) {
cardAds[i].remove();
}
}
index += 1;
} while (index < 2);
}
// 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);
}
}
onFresh();
setTimeout(() => {
removeBiliAds();
// button click (换一换)
document.getElementsByClassName('feed-roll-btn')[0].addEventListener('click', () => {
setTimeout(() => { removeBiliAds(); }, 800);
});
}, TIME_DELAY);
// 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(); };
}
// live.bilibili.com
const livePopupBlock = (selection) => {
const iframes = document.getElementsByTagName('iframe');
for (let i = 0; i < iframes.length; i += 1) {
if (iframes[i].src.includes('live-lottery')) {
// document.getElementsByTagName('iframe')[2].style.visibility = 'hidden';
// document.getElementsByTagName('iframe')[2].style.opacity = 0;
// iframes[i].style.display = 'none';
if (selection === true) {
iframes[i].style.visibility = 'hidden';
} else {
iframes[i].style.visibility = '';
}
}
}
};
// Baidu related search URL cleaning
function cleanBaiduURL() {
// all <a> links
function cleanLinks() {
// <a> class = "c-color-link"
const links = document.getElementsByTagName('a');
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);
}
if (links[i].href.includes(INDEX_STRING_BD2)) {
const index = links[i].href.indexOf(INDEX_STRING_BD2);
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);
}
}
}
cleanLinks();
window.onscroll = () => {
let topScroll = 0;
const scrolls = document.documentElement.scrollTop || document.body.scrollTop;
if (scrolls - topScroll > 150) {
cleanLinks();
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.href.includes('www.bilibili.com');
const isBSpace = realLocation.href.includes('space.bilibili.com');
const isBSearch = realLocation.href.includes('search.bilibili.com');
const isBLive = realLocation.href.includes('live.bilibili.com');
// bilibili
if (isBilibili) {
removeBiliAds();
// 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 || isBSearch) {
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 (isBLive) {
// block popups
const liveIntervalId = setInterval(livePopupBlock(BlockLivePopups), 1000);
setTimeout(() => { clearInterval(liveIntervalId); }, TIME_DELAY * 200);
}
} else if (isBaidu) { // baidu <related search>
cleanBaiduURL();
setTimeout(() => { cleanBaiduURL(); }, 1000);
}
};
})();
/*
# Changelog
0.3.8.1 2022.01.13
Clean more links of Baidu.com
0.3.8 2022.01.06
- Block Card-Ads for Bilibili. (And now blocked banner-ads & card-ads for Bilibili)
- Block [Lucky Draw (The Selection)] popup at [live.bilibili.com]. Disabled by default.
- (SET [{BlockLivePopups} = true] to enable it.)
- The script may add menus to unlock custom setting.
v0.3.7.1 2023.01.02
- Fixed [space.bilibili.com] effects after paged, navi-bar clicked or menu-item clicked.
- 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.
*/
// function declaration methods:
// function functionName() {}; window.onload = functionName()
// window.onload = function() {}
// window.onload = () => {}