网页屏蔽器

屏蔽整个网站

目前为 2023-05-17 提交的版本。查看 最新版本

// ==UserScript==
// @name			网页屏蔽器
// @name:en			Website Blocker
// @version			1.3.4
// @description		屏蔽整个网站
// @description:en	Block the whole site
// @namespace		PPPScript
// @license			MIT
// @author			PPPxcy
// @include			*
// @noframes		false
// @grant			unsafeWindow
// @grant			GM_setValue
// @grant			GM_getValue
// @grant			GM_registerMenuCommand
// @run-at			document-start
// ==/UserScript==

var getValue = function(name, defaultValue, isValid) {
	let orig = GM_getValue(name);
	if((isValid && !isValid(orig)) || orig === undefined || orig === null)
		GM_setValue(name, orig = defaultValue);
	return orig;
}, toTable = function(thing) {
	let res = [];
	for(let i in thing)
		res.push(i);
	return res;
}, languagePackage = {
	'zh': {
		name: () => '简体中文',
		settings: () => '设置',
		banner: () => 'PPPxcy 创作的 网页屏蔽器',
		blackList: () => (languagePackage.zh.banner() + ' 屏蔽名单:'),
		whiteList: () => (languagePackage.zh.banner() + ' 白名单:'),
		isBanned: () => '此网站已被屏蔽',
		cancelBan: () => '取消屏蔽',
		comefromWithLink: () => '来自 <a href="https://github.com/PPPxcy" target="_blank">PPPxcy</a> 创作的 网页屏蔽器。',
		comefrom: () => ('来自' + languagePackage.zh.banner() + '。'),
		autoban: () => (languagePackage.zh.banner() + '已自动屏蔽该网站:'),
		bannerOpened: () => (languagePackage.zh.banner() + ' 已开启。'),
		youBannedIt: [() => '你屏蔽了它:', () => ',对吧?'],
		battle: [() => '请确认你的其他脚本没有问题。对抗次数已经达到', () => '次。'],
		cancelConfirm: [() => '您确定要取消屏蔽该网站吗?(', () => ')'],
		dragToMove: () => '拖拽以移动按钮',
		banTheSite: () => '屏蔽该网站',
		hide: () => '隐藏',
		banConfirm: [() => '您确定要屏蔽该网站吗?(', () => ')'],
		hideConfirm: [() => '您确定要隐藏此按钮吗?(在', () => ')'],
		hideForever: () => '要永久隐藏吗?(否则本次会话会隐藏)',
		sureToSettings: () => '你确定要进入设置页面吗?(这会导致该网页刷新!)',
		settingsTitle: () => 'PPPxcy 创作的 网页屏蔽器 设置',
		language: () => '语言:',
		displayIt: () => '点击刷新以查看结果'
	}, 'en': {
		name: () => 'English',
		settings: () => 'Settings',
		banner: () => 'The \x1B[3mWebsite Blocker\x1B[23m written by PPPxcy',
		blackList: () => ('The blocked-list of ' + languagePackage.en.banner() + ': '),
		whiteList: () => ('The white-list of ' + languagePackage.en.banner() + ': '),
		isBanned: () => 'The website is blocked. ',
		cancelBan: () => 'Unblock',
		comefromWithLink: () => 'From the <i>Website Blocker</i> written by <a href="https://github.com/PPPxcy" target="_blank">PPPxcy</a>. ',
		comefrom: () => ('From ' + 'the Website Blocker written by PPPxcy' + '. '),
		autoban: () => (languagePackage.en.banner() + ' automatically blocked this site: '),
		bannerOpened: () => (languagePackage.en.banner() + ' is opened. '),
		youBannedIt: [() => 'You banned the site: ', () => ', currect? '],
		battle: [() => 'Please check your other scripts. The confrontations\' count is at', () => '. '],
		cancelConfirm: [() => 'Are you sure to unblock this site? (', () => ')'],
		dragToMove: () => 'Drag to move the buttons. ',
		banTheSite: () => 'Block the site',
		hide: () => 'Hide',
		banConfirm: [() => 'Are you sure to block this site? (', () => ')'],
		hideConfirm: [() => 'Are you sure to hide the buttons? (At', () => ')'],
		hideForever: () => 'Are you sure to hide these buttons forever? (If you press "No", the buttons will be hiden until you reload this page.)',
		sureToSettings: () => 'Are you sure to settings page? (It will reload this tab!)',
		settingsTitle: () => 'Settings of the <i>Website Blocker</i> written by PPPxcy',
		language: () => 'Language: ',
		displayIt: () => 'Click it to reload to display the settings'
	}
};

let currentLang = getValue("PPP_banner_currentlanguage", 'zh', value => (languagePackage[value] !== undefined)), langPkg = languagePackage[currentLang];

var PPPbanner = Object.assign(PPPbanner || {}, {top: 0, left: 0, listener: null, selectl: null, optionl: null});

let hre = window.location.origin, menu_command_id = GM_registerMenuCommand(langPkg.settings(), function() {
//	TODO:	Things of settings...
	if(confirm(langPkg.sureToSettings()) === false)
		return;
	let styles = document.createElement('style'), options = document.createElement('PPP-mask');
	styles.innerText = `body {
		overflow: clip;
	}
	PPP-mask {
		all: initial;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		position: fixed;
		z-index: 998244353;
		justify-content: center;
		background-color: #000A;
		-webkit-user-select: none;
		-moz-user-select: none;
		user-select: none;
	}
	PPP-banner-setter {
		margin: 1.5625em 0;
		overflow-x: hidden;
		border-radius: 2.5em;
		padding-bottom: 1.75em;
		background-color: ${getValue("PPP_banner_settingsbackgroundcolor", 'white')};
	}
	PPP-banner-setter > :first-child {
		font-size: 1.5em;
		position: relative;
		text-align: center;
		line-height: 1.5em;
		color: ${getValue("PPP_banner_settingsmaincolordark", '#146')};
		padding: 0.25em 1.75em;
		background-color: ${getValue("PPP_banner_settingsmaincolorlight", '#9EF')};
	}
	PPP-banner-setter > :first-child > :last-child {
		width: 1.5em;
		height: 1.5em;
		color: ${getValue("PPP_banner_settingsbackgroundcolor", 'white')};
		text-align: center;
		margin-left: 0.5em;
		line-height: 1.5em;
		margin-right: -1em;
		display: inline-block;
		border-radius: 0.75em;
		background-color: ${getValue("PPP_banner_settingsmaincolordark", '#146')};
		box-shadow: inset 0.0234375em 0.0234375em 0 0.0625em ${getValue("PPP_banner_settingsmaincolor", '#2CF')};
	}
	PPP-banner-setter > :first-child > :last-child:hover {
		filter: brightness(0.875);
		backdrop-filter: brightness(0.875);
	}
	PPP-banner-setter > :not(PPP-banner-setter > :first-child, PPP-banner-setter > :last-child) {
		border-bottom: 1px solid;
	}
	PPP-banner-setter > :not(PPP-banner-setter > :first-child) {
		padding: 0.625em;
		margin: 0 1.5625em;
	}
	PPP-banner-setter PPP-select {
		color: ${getValue("PPP_banner_settingsmaincolordark", '#146')};
		height: 1.75em;
		min-width: 3.5em;
		overflow-x: clip;
		border: 1px solid;
		border-color: ${getValue("PPP_banner_settingsmaincolordark", '#146')};
		display: inline-block;
		border-radius: 0.25em;
		vertical-align: middle;
		background-color: ${getValue("PPP_banner_settingsbackgroundcolor", 'white')};
	}
	PPP-banner-setter PPP-select > PPP-option {
		width: calc(100% - 0.25em);
		line-height: 1em;
		padding: 0.375em 0.125em;
	}
	PPP-banner-setter PPP-select[expanded] {
		overflow-y: auto;
		border-width: 2px;
		z-index: 998244353;
		position: relative;
		height: calc(var(--things-count) * 1.75em);
		margin: -1px -1px calc(var(--things-count) * -1.75em + 1.75em - 1px) -1px;
	}
	PPP-banner-setter PPP-select > PPP-option:hover {
		filter: brightness(0.875);
		backdrop-filter: brightness(0.875);
	}
	PPP-banner-setter PPP-select[expanded] > PPP-option {
		width: 100%;
		display: block;
		text-align: left;
	}
	PPP-banner-setter PPP-select:focus > PPP-option[chosen] {
		color: ${getValue("PPP_banner_settingsbackgroundcolor", 'white')};
		background-color: ${getValue("PPP_banner_settingsmaincolor", '#2CF')};
	}
	PPP-banner-setter PPP-select > PPP-option[chosen] {
		display: inline-block;
	}
	PPP-banner-setter PPP-select:not(PPP-banner-setter PPP-select[expanded], PPP-banner-setter PPP-select:has(PPP-option[chosen])):hover {
		filter: brightness(0.875);
		backdrop-filter: brightness(0.875);
	}
/*
	PPP-banner-setter PPP-select::after {
		width: 0;
		height: 0;
		margin: 0 0.125em;
		content: '\\FEFF';
		border-style: solid;
		display: inline-block;
		vertical-align: middle;
		border-width: 0.433em 0.25em;
		border-color: black transparent transparent transparent;
	}
*/
	PPP-banner-setter PPP-select:not(PPP-banner-setter PPP-select[expanded]) > PPP-option:not(PPP-banner-setter PPP-select > PPP-option[chosen]) {
		display: none;
	}`, options.innerHTML = `<PPP-banner-setter><div>${langPkg.settingsTitle()}<span>×</span></div><div><PPP-label>${langPkg.language()}</PPP-label><PPP-select forvalue="PPP_banner_currentlanguage" tabindex="0">${(function() {
		let res = '';
		for(let i in languagePackage)
			res += `<PPP-option value="${i}"${i == currentLang ? ' chosen' : ''}>${languagePackage[i].name()}</PPP-option>`;
		return res;
	})()}</PPP-select></div></PPP-banner-setter>`,

	document.head.appendChild(styles),
	document.body.appendChild(options);

	options.querySelector('PPP-banner-setter > :first-child > :last-child').title = `${langPkg.displayIt()}`,
	options.querySelector('PPP-banner-setter > :first-child > :last-child').onclick = function() {
		window.location.reload();
	}, [...options.querySelectorAll('PPP-select')].forEach(function(sel) {
		sel.onclick = function() {
			sel.focus(), sel.setAttribute('expanded', '');
			if(!PPPbanner.optionl) {
				PPPbanner.optionl = [];
				let opts = [...sel.querySelectorAll('PPP-option')];
				for(let i = 0; i < opts.length; i++) {
					opts[i].addEventListener('click', PPPbanner.optionl[i] = function(event) {
						event.stopPropagation(), event.preventDefault();
						if(sel.querySelector('PPP-option[chosen]'))
							sel.querySelector('PPP-option[chosen]').removeAttribute('chosen');
						opts[i].setAttribute('chosen', ''), GM_setValue(sel.getAttribute('forvalue'), opts[i].getAttribute('value'));
						if(PPPbanner.optionl) {
							let opts = [...sel.querySelectorAll('PPP-option')];
							for(let i = 0; i < opts.length; i++)
								opts[i].removeEventListener('click', PPPbanner.optionl[i]);
							PPPbanner.optionl = null, sel.removeAttribute('expanded');
						}
					});
				}
			}
			return false;
		}, sel.onfocus = function() {
			if(!PPPbanner.selectl) {
				document.addEventListener('keydown', PPPbanner.selectl = function(event) {
					switch(event.keyCode) {
						case 38: {
							let lastChosen = sel.querySelector('PPP-option[chosen]');
							if(!lastChosen)
								lastChosen = sel.firstChild;
							if(lastChosen.previousElementSibling || lastChosen.previousSibling) {
								let then = (lastChosen.previousElementSibling || lastChosen.previousSibling);
								lastChosen.removeAttribute('chosen'), then.setAttribute('chosen', ''), GM_setValue(sel.getAttribute('forvalue'), then.getAttribute('value')), then.scrollIntoView({block: 'nearest', behavior: 'smooth'});
							}
							break;
						} case 40: {
							let lastChosen = sel.querySelector('PPP-option[chosen]');
							if(!lastChosen)
								lastChosen = sel.firstChild;
							if(lastChosen.nextElementSibling || lastChosen.nextSibling) {
								let then = (lastChosen.nextElementSibling || lastChosen.nextSibling);
								lastChosen.removeAttribute('chosen'), then.setAttribute('chosen', ''), GM_setValue(sel.getAttribute('forvalue'), then.getAttribute('value')), then.scrollIntoView({block: 'nearest', behavior: 'smooth'});
							}
							break;
						}
					}
					return false;
				});
			}
		}, sel.onblur = function() {
			if(PPPbanner.selectl)
				document.removeEventListener('keydown', PPPbanner.selectl), PPPbanner.selectl = null;
			if(PPPbanner.optionl) {
				let opts = [...sel.querySelectorAll('PPP-option')];
				for(let i = 0; i < opts.length; i++)
					opts[i].removeEventListener('click', PPPbanner.optionl[i]);
				PPPbanner.optionl = null, sel.removeAttribute('expanded');
			}
		}, sel.style = `--things-count: ${Math.min(sel.querySelectorAll('PPP-option').length, 5)};`;
	});
});

(function() {
	let orig = GM_getValue("PPP_banner_bannedlist");
	if(localStorage.PPP_banned_sign == 'true')
		GM_setValue("PPP_banner_bannedlist", (typeof orig == 'object' ? (orig[hre] = null, orig) : (orig = {}, orig[hre] = null, orig)));
	console.log(langPkg.blackList() + '%O', [...toTable(orig)]);
})(), (function() {
	let orig = GM_getValue("PPP_banner_whitelist");
	if(localStorage.PPP_unbanned_sign == 'true')
		GM_setValue("PPP_banner_whitelist", (typeof orig == 'object' ? (orig[hre] = null, orig) : (orig = {}, orig[hre] = null, orig)));
	console.log(langPkg.whiteList() + '%O', [...toTable(orig)]);
})();

if(localStorage.PPP_unbanned_sign != 'true') {
	if(localStorage.PPP_banned_sign == 'true') {
		let innerHtml = `	<head>
		<title>
			${langPkg.isBanned()}
		</title>
		<link rel="icon" href="https://what" type="image/png">
	</head>
	<body style="position: relative;">
		<style>
			html {
				font-size: 16px;
			}
			PPP-banner {
				top: 1em;
				left: 1em;
				all: initial;
				display: block;
				position: fixed;
				white-space: nowrap;
				-webkit-user-select: none;
				-moz-user-select: none;
				user-select: none;
			}
			PPP-banner-button:hover {
				filter: brightness(87.5%);
			}
			PPP-banner-button {
				all: initial;
				cursor: pointer;
				border-radius: 0.5em;
				padding: 0.25em 0.5em;
				display: inline-block;
				background-color: white;
				border: 1px solid black;
			}
			hr{
				border: 0;
				height: 1px;
				background-image: linear-gradient(to right, #AAAF, #AAA0);
			}
			a:link{
				text-decoration: none;
			}
			a:active{
				text-decoration: none;
			}
			a:visited{
				text-decoration: none;
			}
			a:hover{
				text-decoration: none;
			}
		</style>
		<PPP-banner>
			<PPP-banner-button>${langPkg.cancelBan()}</PPP-banner-button>
		</PPP-banner>
		<div style="margin-top: 25px; margin-left: 25px;">
			<p style="font-size: 3em;">
				${langPkg.isBanned()}
			</p>
			<hr>
			<p style="font-size: 1.5em; color: gray; margin-top: -5px;">
				${langPkg.comefromWithLink()}
			</p>
		</div>
	</body>`;
		console.log(`${langPkg.autoBan()}${window.location.origin}`);
		let last = 1, now, iself, countChange = -1, first = true, dangerLevels = [1000000, 100000, 10000, 3000, 1250, 500, 200, 100, 50, 25, 10, 5, 2, 1];
		for(let i = now = setInterval(function() {}, 1); i > last; i--)
			clearInterval(i), clearTimeout(i);
		last = now, iself = setInterval(function() {
			if(document.documentElement.innerHTML != innerHtml) {
				for(let i = now = setInterval(function() {}, 1); i > last; i--)
					if(i != iself)
						clearInterval(i), clearTimeout(i);
				last = now, countChange++, document.documentElement.innerHTML = innerHtml;
				if(countChange >= dangerLevels[dangerLevels.length - 1])
					console.error(`${langPkg.battle[0]()} ${dangerLevels.pop()} ${langPkg.battle[1]()}`);
				document.querySelector('PPP-banner > PPP-banner-button:last-child').onclick = function() {
					if(confirm(`${langPkg.cancelConfirm[0]()}${hre}${langPkg.cancelConfirm[1]()}`) === true) {
						let orig = GM_getValue("PPP_banner_bannedlist");
						GM_setValue("PPP_banner_bannedlist", typeof orig == 'object' ? (delete orig[hre], orig) : {}),
						delete localStorage.PPP_banned_sign, window.location.reload();
					}
				};
				if(first)
					innerHtml = document.documentElement.innerHTML, first = false;
			}
		}, 16);
	} else {
		let innerHtml = `	<style>
		PPP-banner {
			top: ${localStorage.PPP_banner_lasttop == undefined ? (localStorage.PPP_banner_lasttop = 16) : localStorage.PPP_banner_lasttop}px;
			left: ${localStorage.PPP_banner_lastleft == undefined ? (localStorage.PPP_banner_lastleft = 16) : localStorage.PPP_banner_lastleft}px;
			all: initial;
			display: block;
			position: fixed;
			font-size: 16px;
			z-index: 998244353;
			white-space: nowrap;
			pointer-events: none;
			-webkit-user-select: none;
			-moz-user-select: none;
			user-select: none;
		}
		PPP-banner-dragger:hover, PPP-banner-button:hover {
			filter: brightness(87.5%);
		}
		PPP-banner-dragger {
			width: 2em;
			height: 2em;
			z-index: 998244353;
			position: relative;
			border-radius: 0.5em;
			pointer-events: auto;
			display: inline-block;
			vertical-align: middle;
			background-color: #555;
		}
		PPP-banner-dragger:hover {
			cursor: grab;
		}
		PPP-banner-dragger::before {
			width: 2em;
			padding: 0;
			height: 2em;
			overflow: hidden;
			content: "\\FEFF";
			border-radius: 1em;
			display: inline-block;
			background-image: radial-gradient(white, #EEEE 19%, #AAAA 55%, #5555 77%, transparent);
		}
		PPP-banner-button {
			all: initial;
			cursor: pointer;
			z-index: 998244353;
			position: relative;
			border-radius: 0.5em;
			pointer-events: auto;
			display: inline-block;
			padding: 0.25em 0.5em;
			vertical-align: middle;
			background-color: white;
			border: 1px solid black;
		}
	</style>
	<PPP-banner>
		<PPP-banner-dragger title="${langPkg.dragToMove()}"></PPP-banner-dragger>
		<PPP-banner-button>${langPkg.banTheSite()}</PPP-banner-button>
		<PPP-banner-button>${langPkg.hide()}</PPP-banner-button>
	</PPP-banner>
`;
		console.log(`${langPkg.bannerOpened()}`), setInterval(function() {
			if(document.body && document.querySelector('PPP-banner') == null) {
				document.body.insertAdjacentHTML("beforeend", innerHtml),
				document.body.querySelector('PPP-banner-dragger').onmousedown = function(event) {
					if(PPPbanner.listener !== null)
						return;
					document.querySelector('PPP-banner-dragger').focus(),
					PPPbanner.top = event.clientY - document.querySelector('PPP-banner').style.top.substr(0, document.querySelector('PPP-banner').style.top.length - 2),
					PPPbanner.left = event.clientX - document.querySelector('PPP-banner').style.left.substr(0, document.querySelector('PPP-banner').style.left.length - 2),
					document.querySelector('PPP-banner').style.willChange = "top, left", PPPbanner.listener = function(event) {
						document.querySelector('PPP-banner').style.opacity = '0.75',
						document.querySelector('PPP-banner').style.top = Math.min(Math.max(0, localStorage.PPP_banner_lasttop = event.clientY - PPPbanner.top), window.innerHeight - document.body.querySelector('PPP-banner').getBoundingClientRect().height) + 'px',
						document.querySelector('PPP-banner').style.left = Math.min(Math.max(0, localStorage.PPP_banner_lastleft = event.clientX - PPPbanner.left), window.innerWidth - document.body.querySelector('PPP-banner').getBoundingClientRect().width) + 'px';
					}, document.addEventListener('mousemove', PPPbanner.listener), window.onmouseup = function(event) {
						document.removeEventListener('mousemove', PPPbanner.listener),
						document.querySelector('PPP-banner').style.opacity = document.querySelector('PPP-banner').style.willChange = PPPbanner.listener = window.onmouseup = null;
					};
				};
				document.querySelector('PPP-banner > PPP-banner-button:not(PPP-banner > PPP-banner-button:last-child)').onclick = function(event) {
					if(confirm(`${langPkg.banConfirm[0]()}${hre}${langPkg.banConfirm[1]()}`) === true) {
						localStorage.PPP_banned_sign = 'true';
						let orig = GM_getValue("PPP_banner_bannedlist");
						GM_setValue("PPP_banner_bannedlist", typeof orig == 'object' ? (orig[hre] = null, orig) : (orig = {}, orig[hre]= null, orig)), window.location.reload();
					}
				};
				document.querySelector('PPP-banner > PPP-banner-button:last-child').onclick = function(event) {
					if(confirm(`${langPkg.hideConfirm[0]()} ${hre}${langPkg.hideConfirm[1]()}`) === true) {
						if(confirm(`${langPkg.hideForever()}`) == true) {
							localStorage.PPP_unbanned_sign = 'true';
							let orig = GM_getValue("PPP_banner_whitelist");
							GM_setValue("PPP_banner_whitelist", typeof orig == 'object' ? (orig[hre] = null, orig) : (orig = {}, orig[hre]= null, orig));
						}
						document.querySelector('PPP-banner').style.display = 'none';
					}
				};
				setInterval(function() {
					document.querySelector('PPP-banner').style.top = Math.min(Math.max(0, localStorage.PPP_banner_lasttop), window.innerHeight - document.body.querySelector('PPP-banner').getBoundingClientRect().height) + 'px',
					document.querySelector('PPP-banner').style.left = Math.min(Math.max(0, localStorage.PPP_banner_lastleft), window.innerWidth - document.body.querySelector('PPP-banner').getBoundingClientRect().width) + 'px';
				}, 16);
			}
		}, 16);
	}
}
if(!(localStorage.PPP_unbanned_sign != 'true' && localStorage.PPP_banned_sign == 'true')) {
	let styles = document.createElement('style');
	styles.innerText = `a[href][PPP-blocked] { filter: sepia(1); }`, document.head.appendChild(styles), setInterval(function() {
		let orig = GM_getValue('PPP_banner_bannedlist'), alist = [...document.querySelectorAll('a[href]:not(a[href][PPP-checked], a[href][PPP-blocked])')];
		if(orig != undefined) {
			for(let i = 0; i < alist.length; i++) {
				if(orig[new URL(alist[i].getAttribute('href'), window.location.href).origin] !== undefined)
					alist[i].setAttribute('PPP-blocked', ''), alist[i].setAttribute('title', `${langPkg.youBannedIt[0]()}` + new URL(alist[i].getAttribute('href'), window.location.href).origin + `${langPkg.youBannedIt[1]()}\n\t\t${langPkg.comefrom()}`);
				else
					alist[i].setAttribute('PPP-checked', '');
			}
		}
	}, 16);
}