fy / 统计阅读速度

// ==UserScript==
// @license MIT
// @name         统计阅读速度
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       You

// @match        https://www.reuters.com/*
// @match        https://www.cbc.ca/news/*


// @require      http://code.jquery.com/jquery-3.4.1.min.js


// @grant        none
// ==/UserScript==


$(() => {
    let domain = document.domain;
    let selectionDict = {
        "www.cbc.ca": ".storyWrapper>p",
        "www.reuters.com": ".StandardArticleBody_body>p"
    };


    let worldNumbers = $(selectionDict[domain]).text().split(" ").filter(i => {
        if (i !== "") return true
    }).length;
    if (worldNumbers > 80) {
        let minute = 0;
        let html = $(`<div>${worldNumbers}</div>`);
        let speedHtml = $(`<div></div>`);
        html.append(speedHtml);
        let css = " z-index:10;" +
            "width:4em;" +
            "height:3em;" +
            "position:fixed;" +
            "right:0;" +
            "bottom:30px;" +
            "background:#aaffaa;" +
            "font-size:1.5em;" +
            "border-radius:0.5em;text-align:center;";
        html.prop("style", css);


        function clock() {
            minute += 0.1;
            let speed = worldNumbers / minute;
            if (speed > 500) {
                speedHtml.html(minute.toFixed(1));
            } else {
                if (speed < 40) {
                    clearInterval(interval);
                    running=false
                }
                speedHtml.html(minute.toFixed(1) + "<br>" + speed.toPrecision(5));
            }
        }


        let running = true;

        $("body").append(html);

        interval = setInterval(clock, 6000);
        html.click(() => {
            if (running) {
                clearInterval(interval);
                html.css("background", "#888888");
                // html.append("P");
                running = false;

            } else {
                interval = setInterval(clock, 6000);
                html.css("background", "#aaffaa");

                running = true
            }

        })
    }


});