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

Function getCollapsedAnswerObserver

Zhihu-Enhanced.user.js:181–232  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

179
180// 添加收起回答观察器
181function getCollapsedAnswerObserver() {
182 if (!window._collapsedAnswerObserver) {
183 const observer = new MutationObserver(mutations => {
184 for (const mutation of mutations) {
185 if (mutation.target.hasAttribute('script-collapsed')) return
186 // 短的回答
187 if (mutation.target.classList.contains('RichContent')) {
188 for (const addedNode of mutation.addedNodes) {
189 if (addedNode.nodeType != Node.ELEMENT_NODE) continue
190 if (addedNode.className != 'RichContent-inner') continue
191 if (addedNode.offsetHeight < 400) break
192 //console.log('111',addedNode, addedNode.classList, addedNode.classList.contains('RichContent-inner'), addedNode.offsetHeight, addedNode.textContent.length)
193 const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
194 if (button) {
195 mutation.target.setAttribute('script-collapsed', '');
196 button.click();
197 return
198 }
199 }
200 // 长的回答
201 } else if (mutation.target.tagName === 'DIV' && !mutation.target.style.cssText && !mutation.target.className) {
202 if (mutation.target.parentElement.hasAttribute('script-collapsed')) return
203 //console.log('222',mutation.target, mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]'))
204 const button = mutation.target.querySelector('.ContentItem-actions.Sticky [data-zop-retract-question]');
205 if (button) {
206 mutation.target.parentElement.setAttribute('script-collapsed', '');
207 button.click();
208 return
209 }
210 }
211 }
212 })
213
214 observer.start = function() {
215 if (!this._active) {
216 this.observe(document, { childList: true, subtree: true });
217 this._active = true;
218 }
219 }
220 observer.end = function() {
221 if (this._active) {
222 this.disconnect();
223 }
224 }
225
226 window.addEventListener('urlchange', function() {
227 observer[location.href.indexOf('/answer/') === -1 ? 'start' : 'end']();
228 })
229 window._collapsedAnswerObserver = observer;
230 }
231 return window._collapsedAnswerObserver
232}
233
234
235// 默认收起回答

Callers 2

defaultCollapsedAnswerFunction · 0.85
collapsedAnswerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected