From a0c56a107e0c06bcc6faf719de2c1a332a34c70a Mon Sep 17 00:00:00 2001 From: liangjiancang <42267675+liangjiancang@users.noreply.github.com> Date: Fri, 13 Aug 2021 19:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20[=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=94=B6=E8=B5=B7=E5=9B=9E=E7=AD=94]=20=E5=92=8C=20[=E4=B8=80?= =?UTF-8?q?=E9=94=AE=E6=94=B6=E8=B5=B7=E5=9B=9E=E7=AD=94]=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#70)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zhihu-Enhanced.user.js | 90 +++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 56668bd..03d8c47 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -87,24 +87,53 @@ function menu_value(menuName) { } +// 添加收器回答观察器 +function getCollapsedAnswerObserver() { + if (!window._collapsedAnswerObserver) { + const ob = new MutationObserver(mutations => { + for (const mutation of mutations) { + if (mutation.target.hasAttribute('script-collapsed')) return + if (!mutation.target.classList.contains('RichContent')) continue + for (const addedNode of mutation.addedNodes) { + if (addedNode.nodeType != Node.ELEMENT_NODE) continue + const button = addedNode.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]') + if (button) { + mutation.target.setAttribute('script-collapsed', '') + button.click() + return + } + } + } + }) + + ob.start = function() { + if (!this._active) { + this.observe(document, { childList: true, subtree: true }) + this._active = true + } + } + ob.end = function() { + if (this._active) { + this.disconnect() + } + } + + window.addEventListener('locationchange', function() { + ob[window.location.href.indexOf('answer') == -1 ? 'start' : 'end']() + }) + window._collapsedAnswerObserver = ob + } + return window._collapsedAnswerObserver +} + + // 默认收起回答 function defaultCollapsedAnswer() { if (!menu_value('menu_defaultCollapsedAnswer')) return - (new MutationObserver(mutations => { - for (const mutation of mutations) { - if (!mutation.target.classList.contains('RichContent')) continue - for (const addedNode of mutation.addedNodes) { - if (mutation.target._defaultCollapsed) return - if (addedNode.nodeType != Node.ELEMENT_NODE) continue - const button = addedNode.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]') - if (button) { - mutation.target._defaultCollapsed = true - button.click() - return - } - } - } - })).observe(document, { childList: true, subtree: true }) + const ob = getCollapsedAnswerObserver() + if (window.location.href.indexOf('answer') == -1) { + ob.start() + } } @@ -115,15 +144,30 @@ function collapsedAnswer() { document.head.appendChild(document.createElement('style')).textContent = '.CornerButton{margin-bottom:8px !important;}.CornerButtons{bottom:45px !important;}'; document.querySelector('.CornerAnimayedFlex').insertAdjacentHTML('afterBegin', ''); document.getElementById('collapsed-button').onclick = function () { - document.querySelectorAll('.RichContent').forEach(function (el) { - if (el.querySelector('.RichContent-inner').offsetHeight > 400) { - const button = el.querySelector('.ContentItem-rightButton[data-zop-retract-question]') - if (button) { - el._defaultCollapsed = true - button.click() - } + document.querySelectorAll('[script-collapsed]').forEach(function(scriptCollapsed) { + scriptCollapsed.querySelectorAll('.ContentItem-actions [data-zop-retract-question], .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) { + button.click() + }) + }) + document.querySelectorAll(':not([script-collapsed]) .ContentItem-actions.Sticky [data-zop-retract-question]').forEach(function(button) { + let el = button.parentElement + while (!el?.classList.contains('RichContent')) { + el = el.parentElement } - }); + if (el) { + el.setAttribute('script-collapsed', '') + } + button.click() + }) + const ob = getCollapsedAnswerObserver() + ob.start() + if (!menu_value('menu_defaultCollapsedAnswer') && !ob._disconnectListener) { + window.addEventListener('locationchange', function() { + ob.end() + window._collapsedAnswerObserver = null + }) + ob._disconnectListener = true + } } } }