gyakkun / Show VIB Grading and Delta Alpha

// ==UserScript==
// @name          Show VIB Grading and Delta Alpha
// @description   You are a Very Important Bangumier!
// @copyright     gyakkun
// @license       MIT
// @version       0.0.6
// @match        *://bangumi.tv/*
// @match        *://bgm.tv/*
// @match        *://chii.in/*
// @updateURL https://openuserjs.org/meta/gyakkun/Show_VIB_Grading_and_Delta_Alpha.meta.js
// @downloadURL https://openuserjs.org/install/gyakkun/Show_VIB_Grading_and_Delta_Alpha.user.js
// @grant none
// ==/UserScript==
(function () {
  if (document.location.pathname.match(/\/subject\/\d+$/) == null) return

  // Calculate global avg
  let globalRatingSum = 0, globalVoterCount = 0
  const LOG_PREFIX = "[SVGaDA] "
  const GLOBAL_AVG_SELECTOR = ".global_score > span[property='v:average']"
  const FRIEND_SCORE_SELECTOR = "div.frdScore"
  const SCORE_PANEL_SELECTOR = "div[rel='v:rating']"
  $(".horizontalChart > li > a").each((idx, item) => {
    let text = item.attributes["data-original-title"].value
    let count = text.substring(text.indexOf("(") + 1, text.lastIndexOf(")") - 1);
    count = parseInt(count)
    let rating = 10 - idx
    console.debug(`${LOG_PREFIX}Global vote: rating - ${rating}, count - ${count}`)
    globalRatingSum += count * rating
    globalVoterCount += count
  })

  let globalAvg = globalRatingSum / globalVoterCount
  console.log(`${LOG_PREFIX}Global AVG:  ${globalAvg}`)

  // Fetch and calculate VIB avg
  fetch(document.URL + "/stats").then(d => {
    return d.text()
  }).then(html => {
    let lines = html.split(/\r?\n/);
    let chartSetsKeyword = "CHART_SETS"
    console.debug(`${LOG_PREFIX}Num of lines in html: ${lines.length}`)
    for (let line of lines) {
      let idxOfKeyword = line.indexOf(chartSetsKeyword)
      if (idxOfKeyword < 0) continue;
      let objLine = line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)
      console.debug(`${LOG_PREFIX}The CHART_SETS obj line ${objLine}`)
      let chartSetsObj = JSON.parse(objLine)
      console.debug(`${LOG_PREFIX}The CHART_SETS obj after parsing: ${objLine}`)
      if (!!!chartSetsObj['vib']) {
        console.log(`${LOG_PREFIX}No VIB rating found.`)
        break
      }
      let vibRatingSum = 0, vibVoterCount = 0;
      for (let group of chartSetsObj.vib.data) {
        let rating = group.title
        let count = !!group.vib ? group.vib : 0
        vibRatingSum += rating * count
        vibVoterCount += count
        console.debug(`${LOG_PREFIX}VIB vote: rating - ${rating}, count - ${count}`)
      }
      let vibAvg = (vibRatingSum + 0.0) / (vibVoterCount + 0.0)
      console.log(`${LOG_PREFIX}VIB AVG: ${vibAvg}`)
      // $(GLOBAL_AVG_SELECTOR).after("<div>" + avg + "</div>")
      let delta = vibAvg - globalAvg
      console.log(`${LOG_PREFIX}Delta AVG: ${delta}`)

      // $(GLOBAL_AVG_SELECTOR).after("<div> Delta: " + (avgAvg - avg) + "" + "</div>")
      let scorePanelDiv = $(SCORE_PANEL_SELECTOR)
      let friendScorePanel = $(FRIEND_SCORE_SELECTOR)
      let thePanel = friendScorePanel.length > 0 ? friendScorePanel : scorePanelDiv
      thePanel.first().after(`
        <div class="frdScore">
          全站评价:<span class="num">${globalAvg.toFixed(2) + ""}</span>
        </div>
        <div class="frdScore">
          VIB评价 :<span class="num">${vibAvg.toFixed(2) + ""}</span> <span class="desc">${delta > 0 ? "▲" + delta.toFixed(2) : "▼" + delta.toFixed(2)}</span> <a href="${document.URL + "/stats"}" class="l">透视</a>
        </div>
    `)
      break;
    }
  })
}());