-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path中国大学MOOC-讨论区-停止复读.user.js
99 lines (84 loc) · 2.88 KB
/
中国大学MOOC-讨论区-停止复读.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
98
99
// ==UserScript==
// @name 中国大学MOOC-讨论区-停止复读
// @namespace http://tampermonkey.net/
// @version 0.3.2
// @description 隐藏复读内容(有评论的回复始终显示)
// @license GPL-3.0-or-later
// @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
// @author Y.D.X.
// @require https://gitee.com/YDX-2147483647/BIT-enhanced/raw/main/TamperMonkey/lib/mooc.js
// @match https://www.icourse163.org/learn*
// @match https://www.icourse163.org/spoc/learn*
// @grant none
// ==/UserScript==
(function () {
'use strict'
/* global Mooc */
// interval's unit: ms.
function wait_until_presence (selector, interval) {
return new Promise((resolve, reject) => {
const check = setInterval(function () {
if (document.querySelector(selector)) {
clearInterval(check)
resolve(document.querySelector(selector))
}
}, interval)
})
}
let already_add_style_sheet = false
function add_style_sheet () {
if (already_add_style_sheet) {
return
}
const sheet = document.createElement('style')
sheet.innerHTML = `
.copycat {
display: none;
}
`
document.head.appendChild(sheet)
already_add_style_sheet = true
}
function get_main_content (reply_div) {
return reply_div.querySelector('.f-richEditorText').textContent
}
function has_comment (reply_div) {
return reply_div.querySelector('.m-commentWrapper') !== null
}
function check_all_copycat () {
const replies = document.querySelectorAll('.j-reply-all > div > .j-list > .j-data-list > div')
for (let i = replies.length - 1; i >= 0; i--) {
const current_reply = replies[i]
let is_copycat = false
if (!has_comment(current_reply)) {
for (let j = replies.length - 1; j > i; j--) {
const r = replies[j]
if (r.classList.contains('copycat')) {
continue
} else if (get_main_content(r) === get_main_content(current_reply)) {
is_copycat = true
break
}
}
}
if (is_copycat) {
current_reply.classList.add('copycat')
} else if (current_reply.classList.contains('copycat')) {
current_reply.classList.remove('copycat')
}
}
}
async function main () {
if (/#\/learn\/forumdetail\?pid=\d+/.test(window.location.hash)) {
add_style_sheet()
await wait_until_presence('#courseLearn-inner-box .j-detailBox .rinfobox > h4', 1000)
check_all_copycat()
const page_selector = document.querySelector('.j-reply-all > div > .j-list > .j-data-list + .u-pager')
page_selector.addEventListener('click', function () {
setTimeout(() => check_all_copycat(), 500)
})
}
}
Mooc.on_every_loaded(main)
window.addEventListener('hashchange', () => Mooc.on_every_loaded(main))
})()