-
Notifications
You must be signed in to change notification settings - Fork 4
/
ckylin-script-ytb-comments-blocker.user.js
97 lines (92 loc) · 3.45 KB
/
ckylin-script-ytb-comments-blocker.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ==UserScript==
// @name Youtube 评论区屏蔽工具
// @name:en Youtube comments blocker
// @namespace ckylin-script-ytb-comments-blocker
// @version 0.1
// @description 屏蔽指定上传者的视频下方评论
// @description:en Block comments from specified uploader's videos.
// @author CKylinMC
// @match https://www.youtube.com/watch?v=*
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_info
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @license GPL-3.0-only
// ==/UserScript==
(function() {
'use strict';
const wait = t => new Promise(r => setTimeout(r,t));
const waitFor = async q => {
let trialTimes = 50;
while(--trialTimes>=0){
let dom = document.querySelector(q);
if(dom) return dom;
await wait(200);
}
return null;
}
const hasBlocked = async link => (await GM_getValue(link))=="Blocked";
const addBlock = async link => {
if(!(await hasBlocked(link))){
await GM_setValue(link,"Blocked");
}
}
const unBlock = async link => {
if((await hasBlocked(link))){
await GM_setValue(link,"0");
}
}
const rmStyle = () => {
const doms = document.querySelectorAll("style.CK-ytb-blocker");
if(doms) [...doms].forEach(e=>e.remove());
}
const addStyle = s =>{
const style = document.createElement("style");
style.classList.add("CK-ytb-blocker");
style.innerHTML = s;
document.head.appendChild(style);
}
const menuId = {
lastMenu: null
}
function addBlockMenu(link){
if(menuId.lastMenu) GM_unregisterMenuCommand(menuId.lastMenu);
menuId.lastMenu = GM_registerMenuCommand("屏蔽此Uploader / Block this Uploader", async () => {
await addBlock(link);
tryDetect();
});
}
function addUnBlockMenu(link,ytd_comments){
if(menuId.lastMenu) GM_unregisterMenuCommand(menuId.lastMenu);
menuId.lastMenu = GM_registerMenuCommand("取消屏蔽此Uploader / Unblock this Uploader", async () => {
await unBlock(link);
const tip = document.querySelector("#CK-ytb-blocktip");
if(tip) tip.remove();
rmStyle();
ytd_comments.style.display = "block";
addBlockMenu(link);
});
}
async function tryDetect(){
const ytd_ch_link_q = "#text-container.ytd-channel-name a";
const ytd_ch_link_dom = await waitFor(ytd_ch_link_q);
if(!ytd_ch_link_dom) return console.log("[CommentsBlocker] No channel link detected.");
const ytd_ch_link_href = ytd_ch_link_dom.href;
if((await hasBlocked(ytd_ch_link_href))){
//addStyle(`ytd-comments,#comments{display:none!important;}`);
const ytd_comments = document.querySelector("#comments");
const blocktip = document.createElement("center");
addUnBlockMenu(ytd_ch_link_href,ytd_comments);
blocktip.id = "CK-ytb-blocktip";
blocktip.innerHTML = "Comments has been blocked.";
blocktip.style.color = getComputedStyle(ytd_ch_link_dom).color;
ytd_comments.parentNode.insertBefore(blocktip,ytd_comments);
ytd_comments.style.display = "none";
}else{
addBlockMenu(ytd_ch_link_href);
}
}
tryDetect();
})();