forked from natee/tampermonkey-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure-douyu.js
212 lines (183 loc) · 5.39 KB
/
pure-douyu.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// ==UserScript==
// @name 屏蔽斗鱼多余内容
// @version 0.2
// @description 弹幕、右侧评论、下方推荐、火箭、抽奖、导航栏,统统屏蔽!
// @author [email protected]
// @include *://www.douyu.com
// @include *://www.douyu.com/*
// @grant none
// ==/UserScript==
/**
* 完全可以remove页面元素,但是为了防止它的js代码报错导致异常,还是直接隐藏即可
*/
(function () {
'use strict';
const noneClassName = 'hidden';//'(⊙\.⊙)';
const noneClassName1 = 'hidden1';
const noneClassName2 = 'hidden2';
const playerId = '#__h5player'; // 播放器id,需隐藏它的所有兄弟节点
const hideElems = [
'.UPlayerLotteryEnter', // 左下角漂浮的房间开奖
'.Title-anchorPic',
'#dysign-30008',
'#js-bottom',
'#js-chat-cont',
'#js-chat-speak',
'#js-live-room-normal-right',
'#js-player-toolbar',
'#js-recommand',
'#js-room-activity',
'#js-stats-and-actions',
'#right_col_peck',
'.PlayerCase-Sub',
'.PlayerCaseSub',
'.PlayerCaseSub-Main',
'.PlayerSub',
'.anchor-dynamic',
'.announce',
'.c-list',
'.chat-cont',
'.chat-cont-wrap',
'.chat-speak',
'.embed-msg',
'.live-room-normal-equal-right-item',
'.live-room-normal-left',
'.live-room-normal-right',
'.pendant-wrap',
'.recommand',
'.room-ad-top',
'.stats-and-actions',
'.tab',
'.text-cont',
'.valentine1807',
'.layout-Player-aside',
'.layout-Aside',
'.Title-roomOtherBottom',
'.layout-Player-guessgame',
'.guessGameContainer',
'.YubaGroup',
'#ad1'
];
const promiseDelay = ms => {
let timeout
return {
promise: () => {
return new Promise(resolve => {
timeout = setTimeout(resolve, ms)
})
},
clearTimeout: () => clearTimeout(timeout)
}
}
const addStyle = () => {
const css = `
${hideElems.join(',')},
.hidden.hidden1.hidden2,
.LotteryContainer-svgaWrap,
.LotteryContainer {
display:none!important;
}`;
const head = document.head || document.getElementsByTagName('head')[0];
let style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
const hide = (element) => {
if (element) {
element.classList.add(noneClassName, noneClassName1, noneClassName2)
}
}
/**
* 直接屏蔽页面元素,比如右侧的评论框,左侧的导航栏,下方的推荐、广告各种垃圾
*/
const hideBySelector = () => {
const timeout = promiseDelay(300);
timeout.promise().then(() => {
hideElems.forEach(seletor => {
hide(document.querySelector(seletor))
});
timeout.clearTimeout()
})
}
const checkPlayerComponents = () => {
const timeout2 = promiseDelay(2000);
timeout2.promise().then(() => {
const playerElem = document.querySelector(playerId);
let playserElemChildren = undefined;
if (playerElem) {
playserElemChildren = playerElem.children;
}
// 表示各种乱七八糟的未加载完毕
if (!playserElemChildren || playserElemChildren.length < 10) {
checkPlayerComponents();
} else {
timeout2.clearTimeout();
hidePlayerAds(playserElemChildren);
hideSlowElem(playserElemChildren);
}
});
}
// 播放器区域内部元素不一定2s内可以显示
const dynamicClass = [
'broadcastDiv', // 火箭
'comment',
'luckDraw',
'focusModel', // 主播求关注的漂浮物
];
const hideSlowElem = (playserElemChildren) => {
const timeout1 = promiseDelay(5000);
timeout1.promise().then(() => {
// TODO 这里遍历了多余的元素,hidePlayerAds处理过的元素无需再处理
for (let i = 0; i < playserElemChildren.length; i++) {
const v = playserElemChildren[i];
const classArr = v.classList.toString();
for (let j = 0; j < dynamicClass.length; j++) {
const element = dynamicClass[j];
if (classArr.indexOf(element) > -1) {
hide(v)
}
}
}
timeout1.clearTimeout();
});
}
/**
* 屏蔽播放器区域的弹幕、飞机、各种漂浮物等
* 这个需要等到播放器加载后才能渲染
* @param { DOMTokenList } playserElemChildren
*/
const hidePlayerAds = (playserElemChildren) => {
for (let i = 0; i < playserElemChildren.length; i++) {
const v = playserElemChildren[i];
const classArr = v.classList.toString();
const hasClass = classArr != '';
let isControl = false;
// 视频控制器得显示出来,通过判断子元素是否有controlbar类
if (!hasClass) {
const emptyDivChildren = v.children;
for (let j = 0; j < emptyDivChildren.length; j++) {
const element = emptyDivChildren[j];
if (element.classList.toString().indexOf('controlbar') > -1) {
// 表示是播放器
isControl = true;
break;
}
}
}
if (!isControl && classArr.indexOf('video-container') < 0) {
hide(v)
}
}
}
function setup() {
hideBySelector();
checkPlayerComponents();
addStyle();
}
setup();
})();