Greasy Fork

Greasy Fork is available in English.

B站显示点赞率、投币率、收藏率

显示b站 | bilibili | 哔哩哔哩 点赞率、投币率、收藏率

目前为 2023-12-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         B站显示点赞率、投币率、收藏率
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  显示b站 | bilibili | 哔哩哔哩 点赞率、投币率、收藏率
// @author       魂hp
// @license      MIT
// @match        *.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        GM.addStyle
// @grant        unsafeWindow
// ==/UserScript==

(function () {
  "use strict";
  if (!(unsafeWindow?.__INITIAL_STATE__?.videoData?.stat?.view)) {
    return;
  }
  // 修改样式
  GM.addStyle(`
        .video-toolbar-left-item{
            width:auto !important
        }
        .toolbar-left-item-wrap{
            margin-rigth:auto !important;
            display:flex !important
        }
    `);
  class videoData {
    videoStat = {
      view: 0,
      like: 0,
      coin: 0,
      favorite: 0,
    };
    constructor() {
      this.initVideoStat();
    }
    initVideoStat() {
      for (let key in this.videoStat) {
        this.videoStat[key] = unsafeWindow.__INITIAL_STATE__.videoData.stat[key];
      }
    }
    // 计算点赞率、投币率、收藏率,并获取对应的颜色
    getRateAndColor(nameStr) {
      let res = { rate: 0, color: "#222" };
      if (!(nameStr in this.videoStat)) {
        return res;
      }
      let num = this.videoStat.view / this.videoStat[nameStr];
      if (num == Infinity) {
        return res;
      }
      if (num <= 10) {
        res.rate = num.toFixed(2);
        res.color = "DarkOrange";
      } else if (num <= 25) {
        res.rate = num.toFixed(1);
        res.color = "#db03fc";
      } else {
        res.rate = num.toFixed(0);
      }
      return res;
    }
  }
  const vData = new videoData();
  //添加元素
  const div = { like: {}, coin: {}, favorite: {} };
  for (let e in div) {
    div[e] = document.createElement("div");
    div[e].innerHTML = `
        <span style="margin-left: 3px;margin-right: 3px;">≈</span>
        <math xmlns="http://www.w3.org/1998/Math/MathML" style="font-size: 23px;">
            <mfrac>
                <mrow>
                    <mn>1</mn>
                    </mrow><mrow>
                    <mi></mi>
                </mrow>
            </mfrac>
        </math>
        <svg class="view-icon" style="width:20px;height:20px;vertical-align:text-bottom;xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20" width="20" height="20">
        <path d="M10 4.040041666666666C7.897383333333334 4.040041666666666 6.061606666666667 4.147 4.765636666666667 4.252088333333334C3.806826666666667 4.32984 3.061106666666667 5.0637316666666665 2.9755000000000003 6.015921666666667C2.8803183333333333 7.074671666666667 2.791666666666667 8.471183333333332 2.791666666666667 9.998333333333333C2.791666666666667 11.525566666666668 2.8803183333333333 12.922083333333333 2.9755000000000003 13.9808C3.061106666666667 14.932983333333334 3.806826666666667 15.666916666666667 4.765636666666667 15.744683333333336C6.061611666666668 15.849716666666666 7.897383333333334 15.956666666666667 10 15.956666666666667C12.10285 15.956666666666667 13.93871666666667 15.849716666666666 15.234766666666667 15.74461666666667C16.193416666666668 15.66685 16.939000000000004 14.933216666666667 17.024583333333336 13.981216666666668C17.11975 12.922916666666667 17.208333333333332 11.526666666666666 17.208333333333332 9.998333333333333C17.208333333333332 8.470083333333333 17.11975 7.073818333333334 17.024583333333336 6.015513333333334C16.939000000000004 5.063538333333333 16.193416666666668 4.329865000000001 15.234766666666667 4.252118333333334C13.93871666666667 4.147016666666667 12.10285 4.040041666666666 10 4.040041666666666zM4.684808333333334 3.255365C6.001155 3.14862 7.864583333333334 3.0400416666666668 10 3.0400416666666668C12.13565 3.0400416666666668 13.999199999999998 3.148636666666667 15.315566666666667 3.2553900000000002C16.753416666666666 3.3720016666666672 17.890833333333333 4.483195 18.020583333333335 5.925965000000001C18.11766666666667 7.005906666666667 18.208333333333336 8.433 18.208333333333336 9.998333333333333C18.208333333333336 11.56375 18.11766666666667 12.990833333333335 18.020583333333335 14.0708C17.890833333333333 15.513533333333331 16.753416666666666 16.624733333333335 15.315566666666667 16.74138333333333C13.999199999999998 16.848116666666666 12.13565 16.95666666666667 10 16.95666666666667C7.864583333333334 16.95666666666667 6.001155 16.848116666666666 4.684808333333334 16.7414C3.2467266666666665 16.624750000000002 2.1092383333333338 15.513266666666667 1.9795200000000002 14.070383333333334C1.8823900000000002 12.990000000000002 1.7916666666666667 11.562683333333334 1.7916666666666667 9.998333333333333C1.7916666666666667 8.434066666666666 1.8823900000000002 7.00672 1.9795200000000002 5.926381666666667C2.1092383333333338 4.483463333333334 3.2467266666666665 3.371976666666667 4.684808333333334 3.255365z" fill="currentColor"></path><path d="M12.23275 9.1962C12.851516666666667 9.553483333333332 12.851516666666667 10.44665 12.232683333333332 10.803866666666666L9.57975 12.335600000000001C8.960983333333335 12.692816666666667 8.1875 12.246250000000002 8.187503333333334 11.531733333333333L8.187503333333334 8.4684C8.187503333333334 7.753871666666667 8.960983333333335 7.307296666666667 9.57975 7.66456L12.23275 9.1962z" fill="currentColor">
        </path>
        </svg>
    `;
  }
  // 更新数据
  function updateRate() {
    for (let e in div) {
      let mi = div[e].querySelector("mi");
      let rateAndColor = vData.getRateAndColor(e);
      mi.style.color = rateAndColor.color;
      mi.textContent = rateAndColor.rate;
    }
  }
  updateRate();
  new MutationObserver(function (mutationsList) {
    for (let mutation of mutationsList) {
      if (mutation.type === "attributes") {
        document
          .querySelector(".video-like")
          .parentNode.appendChild(div.like);
        document
          .querySelector(".video-coin")
          .parentNode.appendChild(div.coin);
        document
          .querySelector(".video-fav")
          .parentNode.appendChild(div.favorite);
      }
    }
  }).observe(document.querySelector(".toolbar-left-item-wrap"), {
    attributes: true,
  });

  new MutationObserver(function (mutationsList) {
    for (let mutation of mutationsList) {
      if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
        vData.initVideoStat();
        updateRate();
      }
    }
  }).observe(document.querySelector(".video-fav-info"), {
    childList: true,
  });
})();