豆瓣小组屏蔽用户及屏蔽关键词

提供豆瓣小组屏蔽用户及屏蔽关键词的功能,no sight,no mind

当前为 2022-03-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         豆瓣小组屏蔽用户及屏蔽关键词
// @namespace    http://tampermonkey.net/
// @version      1.1.1
// @license      MIT
// @description  提供豆瓣小组屏蔽用户及屏蔽关键词的功能,no sight,no mind
// @author       https://greasyfork.org/users/574395-frammolz-amanda
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @match        https://www.douban.com/group/*
// @grant        unsafeWindow
// ==/UserScript==
 
(function() {
		function update(){
			var i0=0;
			for(var j = 0; j<blk.length; j++){
				console.log(j)
				var fix1 = new RegExp("https://www.douban.com/people/"+".*");
				var result=fix1.exec(blk[j]);
				if(!result){
					keyword[i0]=blk[j];
					blk.splice(j,1);
					i0++;
					j--;
				}
			}
			localStorage.setItem("blk", JSON.stringify(blk));
			localStorage.setItem("keyword", JSON.stringify(keyword));
			localStorage.setItem("blkver", "1.1.0");
		}
		var Html = `
			<div id="top-nav-blk" class="douban-blk">
				<div class="douban-blk-out"></div>
				<div class="popup">
					<div>
						<img class="img" src="https://s1.ax1x.com/2022/03/08/b6j7pF.png" height="80" width="80" />
						<h1><br /></h1>
						<h1>屏蔽名单</h1>
						<div id="blk"></div>
						<div>
							<br /><input type="text" placeholder="输入主页地址" id="blockItem" />
							<button id="addBlockItem" class="addBlockItem">add</button>
							<p align="center">示例:https://www.douban.com/people/xxxxxxx/<br />(链接后面不要加后缀)</p>
						</div>
						<br />
						<h1>屏蔽词</h1>
						<div id="keyword"></div>
						<div>
							<div class="cb">
								<input type="checkbox" id="applyOnComment" value="1">应用屏蔽词于评论(默认只应用于标题)
							</div>
							<br /><input type="text" placeholder="输入屏蔽词" id="blkword" />
							<button id="addkeyword" class="addBlockItem">add</button>
							<p align="center">示例:抽奖</p>
						</div>
					</div>
					<button id="Done" class="addBlockItem" >完成</button><br /><br />
				</div>
			</div>
		`;
		var style = `
			<style>
				.img{
					position:absolute;
					top:0px;
					right:0px;
				}
				.blk{
					float: left;
					margin: auto auto auto 30px;
				}
				.cb{
					float: left;
					padding-top: 20px;
					margin: auto auto 10px 30px;
				}
				.del{
					float: right;
					cursor: pointer;
					margin-right: 30px;
					padding: 0px 12px;
				}
				.del:hover {
					color: #fff;
				}
				.addBlockItem{
					cursor: pointer;
					padding: 0px 8px;
				}
				.addBlockItem:hover {
					color: #fff;
				}
				.douban-blk {
					width: 100vw;
					height: 100vh;
					position: absolute;
					top: 0;
					left: 0;
					display:none;
				}
				.douban-blk-out {
					position: absolute;
					background: rgba(0,0,0,0);
					width: 100%;
					height: 100%;
					z-index: 99;
				}
				.popup {
					width: 400px;
					text-align: center;
					margin: auto;
					position:absolute;
					right:0px;
					top: 28px;
					background: #fff;
					padding: auto;
					overflow: auto;
					z-index: 100;
					border-style: groove
				}
			</style>
		`;
		var style1 = `
			<style>
				.popup {
					height: auto;
				}
			</style>
		`;
		var style2 = `
			<style>
				.popup {
					height: 800px;
				}
			</style>
		`;

		var blk=JSON.parse(localStorage.getItem("blk"));

		var keyword=JSON.parse(localStorage.getItem("keyword"));

		var ver=localStorage.getItem("blkver");

		var apoc=localStorage.getItem("apoc");
	 
		if(!blk){
			blk=[];
			localStorage.setItem("blk", JSON.stringify(blk));
		}

		if(!keyword){
			keyword=[];
			localStorage.setItem("keyword", JSON.stringify(keyword));
		}
	 
		if(ver!="1.1.0"){
			update();
		}

		if(!apoc){
			apoc = 0;
			localStorage.setItem("apoc", "0");
		}

		$(document.body).append(Html);
		$(document.body).append(style);
	 
		if(blk.length+keyword.length>11){
			$(document.body).append(style2);
		}
		else{
			$(document.body).append(style1);
		}

		const utils = {
			bindedEles: [],
			bindClick: function(selector, callback) {
				this.bindedEles.push(selector);
				$(selector).click(callback);
			}
		}
	 
		const insertPos = $('#db-global-nav .top-nav-doubanapp');
		if (insertPos && insertPos[0]) {
			$(insertPos[0]).after('<div id="blk-config" class="top-nav-info"><span class="addBlockItem">黑名单设置</span></div>');
		}
		const $contain = $('#top-nav-blk');
		const $body = $(document.body);
		utils.bindClick('#blk-config', e => {
			$contain.show();
			$body.css('overflow', 'hidden');
		});
		utils.bindClick('#Done', e => {
			$contain.hide();
			$body.css('overflow', 'initial');
		});
		utils.bindClick('.douban-blk-out', e => {
			$contain.hide();
			$body.css('overflow', 'initial');
		});

		var list=[];

		var list1=[];
	 
		goto();
	 
		function goto(){
	 
			list=[];

			list1=[];
	 
			blk.forEach(function(item,index){
	 
				var listItem='<br /><div class="blockItem"><p class="blk">item</p><button data-index="{index}" class="del">删除</button></div><br />'.replace(/item/g,item)
	 
				listItem=listItem.replace(/{index}/g,index)
	 
				list.push(listItem)
	 
				$("#blk").html(list.join(""));
	 
				$(".del").on("click",function(){
	 
					var index=$(this).attr("data-index");
	 
					$(this).closest("div").remove();
	 
					blk=JSON.parse(localStorage.getItem("blk"));
	 
					blk.splice(index,1)
	 
					localStorage.setItem("blk", JSON.stringify(blk));
	 
					goto();
	 
				})
			})

			keyword.forEach(function(item,index){
	 
				var listItem='<br /><div class="blockItem"><p class="blk">item</p><button data-index="{index}" class="del">删除</button></div><br />'.replace(/item/g,item)
	 
				listItem=listItem.replace(/{index}/g,index)
	 
				list1.push(listItem)
	 
				$("#keyword").html(list1.join(""));
	 
				$(".del").on("click",function(){
	 
					var index=$(this).attr("data-index");
	 
					$(this).closest("div").remove();
	 
					keyword=JSON.parse(localStorage.getItem("keyword"));
	 
					keyword.splice(index,1)
	 
					localStorage.setItem("keyword", JSON.stringify(keyword));

					goto();
	 
				})
			})
		}
		console.log(apoc)

		if(apoc == 1){

			$("#applyOnComment").attr("checked", true);

		}
		else{

			$("#applyOnComment").attr("checked", false);

		}

		$("#addBlockItem").on("click",function(){
	 
			var blockItem=$("#blockItem").val();
	 
			if(!blockItem){
	 
				alert("不能提交空白内容")
	 
				return;
			}

			var re1 = new RegExp("https://www.douban.com/people/"+".*"+"/"+"$");

			var result4=re1.exec(blockItem);

			console.log(result4)

			if(!result4){
	 
				alert("主页地址必须为“https://www.douban.com/people/xxxxxxxxxx/”\n请删除例如“?_dtcc=1”的后缀!")
	 
				return;
			}
	 
			blk=JSON.parse(localStorage.getItem("blk"));
	 
			var listItem='<br /><div class="blockItem"><p class="blk">item</p><button data-index="{index}" class="del">删除</button></div><br />'.replace(/item/g,blockItem);
	 
			if($.isEmptyObject(blk)){
	 
				blk.push(blockItem)
	 
				localStorage.setItem("blk", JSON.stringify([blockItem]));
	 
				istItem=listItem.replace(/{index}/,"0")
	 
			}
	 
			else{
	 
				blk.push(blockItem)
	 
				localStorage.setItem("blk", JSON.stringify(blk));
	 
				listItem=listItem.replace(/{index}/,blk.length-1)
	         	 
			}
	 
			$("#blockItem").val("");
	 
			goto();
	 
		})

		$("#addkeyword").on("click",function(){

			var blockItem=$("#blkword").val();
	 
			if(!blockItem){
	 
				alert("不能提交空白内容")
	 
				return;
			}

			var re2 = new RegExp("https://www.douban.com/people/"+".*");

			var result5=re2.exec(blockItem);

			console.log(result5)

			if(result5){
	 
				alert("请勿输入主页地址!")
	 
				return;
			}
	 
			keyword=JSON.parse(localStorage.getItem("keyword"));
	 
			var listItem='<br /><div class="blockItem"><p class="blk">item</p><button data-index="{index}" class="del">删除</button></div><br />'.replace(/item/g,blockItem);
	 
			if($.isEmptyObject(keyword)){
	 
				keyword.push(blockItem)
	 
				localStorage.setItem("keyword", JSON.stringify([blockItem]));
	 
				istItem=listItem.replace(/{index}/,"0")
	 
			}
	 
			else{
	 
				keyword.push(blockItem)
	 
				localStorage.setItem("keyword", JSON.stringify(keyword));
	 
				listItem=listItem.replace(/{index}/,blk.length-1)
	         	 
			}
	 
			$("#blkword").val("");
	 
			goto();
		})

		$("#applyOnComment").on("click",function(){

			if ($("#applyOnComment")[0].checked) {

				apoc = 1;

			}

			else{

				apoc = 0;

			}

			localStorage.setItem("apoc", apoc);

			goto();

		})

		var windowUrl = window.location.href;
		var topicre = /https:\/\/www.douban.com\/group\/topic\/*/;
		var groupre = /https:\/\/www.douban.com\/group\/*/;
		if( windowUrl.match(groupre)){
			if( windowUrl.match(topicre)){
				blk=JSON.parse(localStorage.getItem("blk"));
				keyword=JSON.parse(localStorage.getItem("keyword"));
				$("a").each(function(index,item){
					var href=$(item).attr("href");
					var title=$(item).attr("title");
					blk.forEach(function(blockItem){
						var re = new RegExp(".*"+blockItem+".*");
						var result1=re.exec(href);
						if(result1){
							$(item).closest(".comment-item").hide();
						}
					})
					keyword.forEach(function(blockItem){
						var re = new RegExp(".*"+blockItem+".*");
						var fix = new RegExp("真的要删除"+".*"+blockItem+".*"+"的发言");
						var result2=re.exec(title);
						var result3=fix.exec(title);
						if(result2&&!result3){
							$(item).closest("li").hide();
						}
					})
				})
				if(apoc == 1){
					$("p.reply-content").each(function(index,item){
						var reply = $(this).text();
						keyword.forEach(function(blockItem){
							var re = new RegExp(".*"+blockItem+".*");
							var result2=re.exec(reply);
							if(result2){
								$(item).closest(".reply-item").hide();
							}
						})
					})
				}
			}
			else{
				blk=JSON.parse(localStorage.getItem("blk"));
				keyword=JSON.parse(localStorage.getItem("keyword"));
				$("a").each(function(index,item){
					var href=$(item).attr("href");
					var title=$(item).attr("title");
					blk.forEach(function(blockItem){
						var re = new RegExp(".*"+blockItem+".*");
						var result1=re.exec(href);
						if(result1){
							$(item).closest("tr").hide();
						}
					})
					keyword.forEach(function(blockItem){
						var re = new RegExp(".*"+blockItem+".*");
						var result2=re.exec(title);
						if(result2){
							$(item).closest("tr").hide();
						}
					})
				})
			}
		}
})();