MCPcopy Index your code
hub / github.com/XIU2/UserScript / collapsedNowAnswer

Function collapsedNowAnswer

Zhihu-Enhanced.user.js:295–416  ·  view source on GitHub ↗
(selectors)

Source from the content-addressed store, hash-verified

293
294// 收起当前回答、评论(监听点击事件,点击网页两侧空白处)
295function collapsedNowAnswer(selectors) {
296 backToTop(selectors) // 快捷回到顶部
297 if (!menu_value('menu_collapsedNowAnswer')) return
298 document.querySelector(selectors).onclick = function(event){
299 if (event.target == this) {
300 // 下面这段主要是 [收起回答],顺便 [收起评论](如果展开了的话)
301 let rightButton = document.querySelector('.ContentItem-actions.Sticky.RichContent-actions.is-fixed.is-bottom')
302 if (rightButton) { // 悬浮在底部的 [收起回答](此时正在浏览回答内容 [中间区域])
303 // 固定的 [收起评论](先看看是否展开评论)
304 let commentCollapseButton = rightButton.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
305 //console.log('111')
306 if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
307 // 再去收起回答
308 rightButton = rightButton.querySelector('.ContentItem-rightButton[data-zop-retract-question]')
309 //console.log('222')
310 if (rightButton) rightButton.click();
311
312 } else { // 固定在回答底部的 [收起回答](此时正在浏览回答内容 [尾部区域])
313
314 // 悬浮的 [收起评论](此时正在浏览评论内容 [中间区域])
315 //if (getXpath('//button[text()="收起评论"]',document.querySelector('.Comments-container'))) {getXpath('//button[text()="收起评论"]',document.querySelector('.Comments-container')).click();console.log('asfaf')}
316
317 let answerCollapseButton_ = false;
318 for (let el of document.querySelectorAll('.ContentItem-rightButton[data-zop-retract-question]')) { // 遍历所有回答底部的 [收起] 按钮
319 if (isElementInViewport(el)) { // 判断该 [收起] 按钮是否在可视区域内
320 // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
321 let commentCollapseButton = el.parentNode.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
322 // 如果展开了评论,就收起评论
323 //console.log('333')
324 //if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) commentCollapseButton.click();
325 if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) {
326 commentCollapseButton.click();
327 if (!isElementInViewport(commentCollapseButton)) scrollTo(0,el.offsetTop+50)
328 }
329 //console.log('444')
330 el.click() // 再去收起回答
331 answerCollapseButton_ = true; // 如果找到并点击收起了,就没必要执行下面的代码了(可视区域中没有 [收起回答] 时)
332 break
333 }
334 }
335 // 针对完全看不到 [收起回答] 按钮时(如 [头部区域],以及部分明明很长却不显示悬浮横条的回答)
336 if (!answerCollapseButton_) {
337 for (let el of document.querySelectorAll('.List-item, .Card.AnswerCard, .Card.TopstoryItem')) { // 遍历所有回答主体元素
338 if (isElementInViewport_(el)) { // 判断该回答是否在可视区域内
339 // 固定的 [收起评论](先看看是否展开评论,即存在 [收起评论] 按钮)
340 let commentCollapseButton = el.querySelector('button.Button.ContentItem-action.Button--plain.Button--withIcon.Button--withLabel:first-of-type')
341 // 如果展开了评论,就收起评论
342 //console.log('555',commentCollapseButton)
343 if (commentCollapseButton && commentCollapseButton.textContent.indexOf('收起评论') > -1) {
344 commentCollapseButton.click();
345 if (!isElementInViewport(commentCollapseButton)) scrollTo(0,el.offsetTop+50)
346 }
347 let answerCollapseButton__ = el.querySelector('.ContentItem-rightButton[data-zop-retract-question]');
348 //console.log('666')
349 if (answerCollapseButton__) answerCollapseButton__.click() // 再去收起回答
350 break
351 }
352 }

Callers 1

startFunction · 0.85

Calls 6

isElementInViewportFunction · 0.85
isElementInViewport_Function · 0.85
backToTopFunction · 0.70
menu_valueFunction · 0.70
getXpathFunction · 0.70
findParentElementFunction · 0.70

Tested by

no test coverage detected