-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLinuxDo-AccessFixes.user.js
More file actions
230 lines (191 loc) · 6.55 KB
/
Copy pathLinuxDo-AccessFixes.user.js
File metadata and controls
230 lines (191 loc) · 6.55 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// ==UserScript==
// @name !.Linux.do Access Fixes
// @description 自动跳转与话题中键打开补 track-view 计数。
// @version 0.1.0
// @author ank
// @namespace https://010314.xyz/
// @license AGPL-3.0-or-later
// @match https://linux.do/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=linux.do
// @grant none
// @run-at document-end
// @updateURL https://raw.githubusercontent.com/AnkRoot/Tampermonkey/main/Discourse/LinuxDo-AccessFixes.user.js
// @downloadURL https://raw.githubusercontent.com/AnkRoot/Tampermonkey/main/Discourse/LinuxDo-AccessFixes.user.js
// ==/UserScript==
;(function () {
'use strict'
const GLOBAL_KEY = '__linuxDoAccessFixes__'
const globalState = (window[GLOBAL_KEY] ||= {
challengeStarted: false,
topicTrackViewStarted: false
})
const CONFIG = {
challengePath: '/challenge',
challengeDebounceMs: 200,
jumpCooldownMs: 3000,
lastJumpKey: 'ld_cf_last_jump',
legacyTopicTrackedKeyPrefix: 'mfix_topic_',
topicTrackedKeyPrefix: 'ld_topic_tracked_',
trackViewCarrierPath: '/session/current.json',
trackViewReadyPollMs: 200,
trackViewReadyTimeoutMs: 6000
}
const ERROR_KEYWORDS = [
'403 error',
'该回应是很久以前创建的',
'reaction was created too long ago',
'我们无法加载该话题',
'we cannot load this topic'
].map(text => text.toLowerCase())
const SELECTORS = {
dialog: '.dialog-body',
content: '.topic-post, .topic-body, .timeline-container',
closeButton: '.dialog-footer button.btn'
}
const query = selector => document.querySelector(selector)
const meta = name => document.querySelector(`meta[name="${name}"]`)?.content || ''
const lowerText = el => (el?.textContent || '').toLowerCase()
const createLogger = prefix => ({
info: (...args) => console.info(`[${prefix}]`, ...args),
warn: (...args) => console.warn(`[${prefix}]`, ...args)
})
const cfLog = createLogger('CF Jump')
const trackLog = createLogger('MiddleclickFix')
const storageGet = key => {
try {
return sessionStorage.getItem(key)
} catch {
return null
}
}
const storageGetNumber = (key, fallback = 0) => {
const value = Number(storageGet(key))
return Number.isFinite(value) ? value : fallback
}
const storageSet = (key, value) => {
try {
sessionStorage.setItem(key, String(value))
return true
} catch {
return false
}
}
const debounce = (fn, ms) => {
let timer = null
return () => {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
timer = null
fn()
}, ms)
}
}
function startChallengeJump() {
let memoryLastJump = 0
let observer = null
const isChallengePage = () => location.pathname.startsWith(CONFIG.challengePath)
const hasContent = () => query(SELECTORS.content) !== null
const dismissDialog = () => query(SELECTORS.closeButton)?.click()
const shouldJump = () => {
if (isChallengePage()) return false
const dialog = query(SELECTORS.dialog)
if (!dialog) return false
const text = lowerText(dialog)
if (!ERROR_KEYWORDS.some(keyword => text.includes(keyword))) return false
if (hasContent()) {
dismissDialog()
return false
}
return true
}
const readLastJump = () => {
const stored = storageGetNumber(CONFIG.lastJumpKey, NaN)
return Number.isFinite(stored) ? stored : memoryLastJump
}
const writeLastJump = timestamp => {
memoryLastJump = timestamp
storageSet(CONFIG.lastJumpKey, timestamp)
}
const jump = () => {
if (Date.now() - readLastJump() <= CONFIG.jumpCooldownMs) {
cfLog.warn('跳转过于频繁,暂停执行以防止死循环。')
return
}
cfLog.info('检测到 Cloudflare 阻断,正在跳转验证...')
observer?.disconnect()
writeLastJump(Date.now())
location.href = `${CONFIG.challengePath}?redirect=${encodeURIComponent(location.href)}`
}
const check = () => shouldJump() && jump()
check()
if (!document.body) return
observer = new MutationObserver(debounce(check, CONFIG.challengeDebounceMs))
observer.observe(document.body, {
childList: true,
subtree: true,
characterData: true
})
window.addEventListener('pagehide', () => observer?.disconnect(), { once: true })
}
function startTopicTrackViewFix() {
const topicId = location.pathname.match(/^\/t\/[^/]+\/(\d+)(?:\/|$)/)?.[1]
if (!topicId) return
const flagKeys = [
`${CONFIG.legacyTopicTrackedKeyPrefix}${topicId}`,
`${CONFIG.topicTrackedKeyPrefix}${topicId}`
]
const isTracked = () => flagKeys.some(key => storageGet(key))
const markTracked = () => flagKeys.forEach(key => storageSet(key, '1'))
if (isTracked()) return
const sendTrackView = () => {
if (!meta('csrf-token')) return
const headers = {
'X-Requested-With': 'XMLHttpRequest',
'Discourse-Track-View': 'true',
'Discourse-Track-View-Topic-Id': topicId,
'Discourse-Track-View-Url': location.href,
'Discourse-Track-View-Referrer': document.referrer || '',
'Discourse-Present': 'true'
}
const sessionId = meta('discourse-track-view-session-id')
if (sessionId) headers['Discourse-Track-View-Session-Id'] = sessionId
fetch(CONFIG.trackViewCarrierPath, {
method: 'GET',
credentials: 'include',
headers
})
.then(response => {
if (!response.ok) {
trackLog.warn(`请求失败 status=${response.status}`)
return
}
markTracked()
trackLog.info(`✓ 已为话题 ${topicId} 补 track-view`)
})
.catch(error => trackLog.warn('网络错误:', error))
}
const isReady = () => Boolean(meta('discourse-track-view-session-id'))
if (isReady()) {
sendTrackView()
return
}
let tries = 0
const maxTries = Math.ceil(CONFIG.trackViewReadyTimeoutMs / CONFIG.trackViewReadyPollMs)
const timer = setInterval(() => {
tries += 1
if (isReady() || tries > maxTries) {
clearInterval(timer)
sendTrackView()
}
}, CONFIG.trackViewReadyPollMs)
window.addEventListener('pagehide', () => clearInterval(timer), { once: true })
}
if (!globalState.challengeStarted) {
globalState.challengeStarted = true
startChallengeJump()
}
if (!globalState.topicTrackViewStarted) {
globalState.topicTrackViewStarted = true
startTopicTrackViewFix()
}
})()