Zhihu Easy Collapse

快捷地收起知乎首页的长答案

目前為 2015-09-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Zhihu Easy Collapse
// @description  快捷地收起知乎首页的长答案
// @author       wenLiangcan
// @version      0.2
// @namespace    https://github.com/wenLiangcan
// @homepage     https://github.com/wenLiangcan/Userscripts
// @license      GPL version 3
// @copyright    Copyright © 2015 wenLiangcan
// @updateURL
// @downloadURL
// @include      http://www.zhihu.com/
// @include      http://www.zhihu.com/explore
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    function getLastItem(arr) {
        return arr[arr.length - 1];
    }

    function toArray(obj) {
        return Array.prototype.slice.call(obj);
    }

    function addFloatingCollapseButtons(feeds) {
        feeds.forEach(function(feed) {
            var collapseButton = getLastItem(feed.getElementsByClassName('collapse'));
            if (collapseButton !== undefined) {
                var newButton = collapseButton.cloneNode(true);
                newButton.onclick = function() {
                    feed.scrollIntoView(true);
                    collapseButton.click();
                };
                newButton.style.position = 'relative';
                newButton.style.marginTop = '1px';
                newButton.style.marginLeft = '0px';
                try {
                    var votebar = getLastItem(feed.getElementsByClassName('zm-votebar'));
                    votebar.appendChild(newButton);
                } catch (e) {
                    console.log(e);
                }
            }
        });
    }

    function getSelectorsBasedOnUrl() {
        if (/^http:\/\/www\.zhihu\.com\/explore.*?$/.test(document.URL)) {
            return [
                '#js-explore-tab > div:nth-child(4) > div',
                '#js-explore-tab > div:nth-child(5) > div'
            ];
        } else {
            return ['#js-home-feed-list'];
        }
    }

    function observeFeedList(feedList) {
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                addFloatingCollapseButtons(toArray(mutation.addedNodes));
            });
        });
        var observerConfig = {
            childList: true
        };
        observer.observe(feedList, observerConfig);
    }

    getSelectorsBasedOnUrl().forEach(function(selector) {
        var feedList = document.querySelector(selector);
        observeFeedList(feedList);
    });

    addFloatingCollapseButtons(
        toArray(document.getElementsByClassName('feed-item')));
})();