topPost

置顶v2ex高赞回复

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         topPost
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  置顶v2ex高赞回复
// @author       yuyinws
// @match        *://v2ex.com/t/*
// @match        *://*.v2ex.com/t/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @icon         https://www.v2ex.com/static/favicon.ico
// @run-at       document-end
// @license      MIT
// ==/UserScript==
(() => {
  // star限制值
  let starLimit = GM_getValue("starLimit") || 5;
  // 菜单注册
  GM_registerMenuCommand(`star限制值:${starLimit}(点击修改)`, () => {
    let starLimit = prompt("请输入");
    GM_setValue("starLimit", starLimit);
  });

  let postMap = new Map();

  let topEl = document.createElement("div");
  topEl.className = "box";

  let refEl = document.querySelector("#Main").childNodes[5];

  let sepEl = document.createElement("div");
  sepEl.className = "sep20";

  let cellEl = document.createElement("div");
  cellEl.className = "cell";
  cellEl.innerText = "高赞回复";

  // 获取所有有star的回复
  document.querySelectorAll("div[id^=r_]").forEach((item) => {
    let clonedItem = item.cloneNode(true);
    if (clonedItem.querySelector(".fade")) {
      let star = Number(clonedItem.querySelector(".fade").innerText);
      if (star >= starLimit) {
        postMap.set(clonedItem, Number(item.querySelector(".fade").innerText));
      }
    }
  });

  // 排序
  const sortMap = new Map([...postMap].sort((a, b) => b[1] - a[1]));
  if (sortMap.size > 0) {
    topEl.appendChild(cellEl);
    for (let [key] of sortMap) {
      topEl.appendChild(key);
    }
    refEl.parentNode.insertBefore(sepEl, refEl);
    refEl.parentNode.insertBefore(topEl, refEl);
  }
})();