知乎回答排序插件

为知乎问题页面多添加两种排序方式

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         知乎回答排序插件
// @namespace    https://github.com/discountry/zhihumarkdown
// @require      https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
// @version      0.2
// @description  为知乎问题页面多添加两种排序方式
// @author       Discountry
// @match        https://www.zhihu.com/question/*
// @exclude      https://www.zhihu.com/question/*/answer/*
// @copyright    2017+, @余博伦
// ==/UserScript==

(function() {
    'use strict';
    var wrapper = $('#QuestionAnswers-answers');
    var toolbar = $('.QuestionButtonGroup');
    toolbar.width('36rem');
    toolbar.append($('<button id="orderByVote" class="Button Button--primary Button--green" type="button">按赞数排序</button>'));
    toolbar.append($('<button id="orderByComments" class="Button Button--primary Button--red" type="button">按评论数排序</button>'));
    // Your code here...
    var orderByVote = function(){
      wrapper.find('.List').html(wrapper.find('.List-item').sort(function(a, b) {
      return b.querySelector('.VoteButton--up').innerText - a.querySelector('.VoteButton--up').innerText;
      }));
    };

    var orderByComments = function () {
       wrapper.find('.List').html(wrapper.find('.List-item').sort(function(a, b) {
          return b.querySelector('.ContentItem-actions .Button--plain').innerText.replace(/ 条评论/g, '') - a.querySelector('.ContentItem-actions .Button--plain').innerText.replace(/ 条评论/g, '');
      }));
    };
    //event listeners
    $("#orderByVote").click(function(){
        orderByVote();
    });
    $("#orderByComments").click(function(){
        orderByComments();
    });
})();