知乎回答按赞同数排序

four score and seven years ago...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         知乎回答按赞同数排序
// @namespace    zhihu.com
// @version      0.2
// @description  four score and seven years ago...
// @author       何从
// @match        https://www.zhihu.com/question/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // bindMyEvent()
  // window.addEventListener('popstate', bindMyEvent)

  var binded = false
  var loop = setInterval(bindMyEvent, 1000)

  function bindMyEvent() {
    if (binded) {
      clearInterval(loop)
      return
    }
    var e = document.querySelectorAll('div.List-headerOptions div.Popover')[0]
    if (e === undefined) { return }
    binded = true
    e.addEventListener('click', function () {
      setTimeout(function () {
        var sorts = document.querySelector('div.Select-list.Answers-select')
        var butt = document.createElement('button')
        butt.className = 'Select-option'
        butt.innerText = '按赞同排序'
        butt.addEventListener('click', sortByLikes)
        sorts.appendChild(butt)

        function sortByLikes() {
          var answers = document.querySelectorAll('div.List-item')
          if (answers.length === 0) {
            return
          }
          var parent = answers[0].parentElement
          var likes = []
          for (var i = 0; i < answers.length; i++) {
            var temp = answers[i].getElementsByClassName('Voters')[0]
            var n = temp ? temp.innerText : '0'
            n = parseInt(n.split(' ')[0])
            likes.push([n, i])
          }
          likes = likes.sort(descendByFirst)
          for (i = 0; i < likes.length; i++) {
            parent.appendChild(answers[likes[i][1]])
          }
        }

        function descendByFirst(x, y) {
          return y[0] - x[0]
        }
      }, 0)
    })
  }
})()