rate shower

nowcoder.com通过率与通过人数切换

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         rate shower
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  nowcoder.com通过率与通过人数切换
// @author       Ygy
// @match        https://ac.nowcoder.com/acm/contest/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nowcoder.com
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    $(window).on('load',function () {
    let $tableHeadTrs = $(".table-hover>thead>tr>th")
    let $tableBodyTrs = $(".table-hover>tbody>tr")
    let $fixedMenu = $(".fixed-menu>ul")

    // $tableHead.append($('<th width="10%">通过率</th>'))

    let $a1 = $('<a id="a1" class="qq"></a>')
    $a1.css('background','url("")')
    // let $a1 = $('<a id="a1" class="qq" style="background: url("")"></a>')
    let $li1 = $('<li></li>').append($a1)
    $fixedMenu.append($li1)

    let flag = 1
    let columnIndex = 2
    let oriList = []
    let rateList = []


    //获取通过率列号
    columnIndex = getColumnIndex()

    console.log(columnIndex)


    //生成列表
    rateListGen(oriList, rateList)

    console.log(oriList)
    console.log(rateList)

    //绑定点击事件
    $("#a1").on('click', change)

    function change() {
        console.log("I'm clicked....")
        $tableBodyTrs.each(function (index, element) {
            let $tableBodyTds = $(element).children('td')
            // console.log($tableBodyTds[2].textContent)
            if (flag < 0) {
                $tableBodyTds[columnIndex].innerHTML = oriList[index]
            } else {
                $tableBodyTds[columnIndex].innerHTML = rateList[index]
            }
        })
        flag *= -1
    }


    //生成两列表,原始与计算后
    function rateListGen(li1, li2) {
        $tableBodyTrs.each(function (index, element) {
            let $tableBodyTds = $(element).children('td')
            // console.log($tableBodyTds[2].textContent)
            let str = $tableBodyTds[columnIndex].textContent
            li1.push(str)
            // console.log(toPercent(rateCal(str)))
            li2.push(toPercent(rateCal(str)))
        })
    }

    //计算比率
    function rateCal(str) {
        let nums = str.split('/');
        if (nums.length !== 2) return 'error!'
        let num1 = parseInt(nums[0])
        let num2 = parseInt(nums[1])
        if (num2 === 0) return 0
        else return num1 / num2
    }

    //转成百分数
    function toPercent(point) {
        let str = Number(point * 100).toFixed(2);
        str += "%";
        return str;
    }

    //获取通过率所在列
    function getColumnIndex() {
        let idx = 2
        $tableHeadTrs.each(function (index, element) {
            let str = element.innerText
            if ("通过率"===str){
                idx = index
                return false
            }
        })
        return idx
    }
});
    // Your code here...
})();