Greasy Fork

florr.io | Playtime counter

Shows how no life you are

当前为 2023-03-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         florr.io | Playtime counter
// @namespace    https://github.com/Samer-Kizi
// @version      1.0.5
// @description  Shows how no life you are
// @author       Furaken
// @match        https://florr.io/*
// @grant        unsafeWindow
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js
// ==/UserScript==

var today = new Date().toString().substring(0,15)
var leech = [{"date":today,"value":0,map:[]}],
    cvs = document.querySelector('canvas')
var newDoc = document.createElement('div')
newDoc.style = `
    right: 6px;
    bottom: -3px;
    color: white;
    text-shadow: rgb(0, 0, 0) 2px 0px 0px, rgb(0, 0, 0) 1.75517px 0.958851px 0px, rgb(0, 0, 0) 1.0806px 1.68294px 0px, rgb(0, 0, 0) 0.141474px 1.99499px 0px, rgb(0, 0, 0) -0.832294px 1.81859px 0px, rgb(0, 0, 0) -1.60229px 1.19694px 0px, rgb(0, 0, 0) -1.97998px 0.28224px 0px, rgb(0, 0, 0) -1.87291px -0.701566px 0px, rgb(0, 0, 0) -1.30729px -1.5136px 0px, rgb(0, 0, 0) -0.421592px -1.95506px 0px, rgb(0, 0, 0) 0.567324px -1.91785px 0px, rgb(0, 0, 0) 1.41734px -1.41108px 0px, rgb(0, 0, 0) 1.92034px -0.558831px 0px;
    position: absolute;
    z-index: 1;
    font-family: Ubuntu;
    line-height: 0px;
    text-align: right;
    pointer-events: none;
`
document.querySelector('body').appendChild(newDoc)

if (localStorage.getItem("florrTime") === null) localStorage.setItem('florrTime', JSON.stringify(leech))
if (JSON.parse(localStorage.getItem("florrTime"))[0].date != today) {
    let x = JSON.parse(localStorage.getItem('florrTime'))
    x.unshift(leech)
    localStorage.setItem('florrTime', JSON.stringify(x));
}

var start = Date.now() - Number(JSON.parse(localStorage.getItem("florrTime"))[0].value) * 1000,
    time,
    allTime = 0,
    allTimeA,
    stop = false

function Time(seconds) {
    var hoursLeft = Math.floor(seconds / 3600);
    var min = Math.floor((seconds - hoursLeft * 3600) / 60);
    var secondsLeft = seconds - hoursLeft * 3600 - min * 60;
    secondsLeft = Math.round(secondsLeft * 100) / 100;
    var answer = hoursLeft< 10 ? "0" + hoursLeft : hoursLeft;
    answer += ":" + (min < 10 ? "0" + min : min);
    answer += ":" + (secondsLeft< 10 ? "0" + secondsLeft : secondsLeft);
    return answer
}

setInterval(function() {
    if (stop) {
        start = Date.now()
        stop = false
    } else {
        var delta = Date.now() - start
        let x = JSON.parse(localStorage.getItem('florrTime'))
        x[0].value = Math.floor(delta / 1000)
        time = Time(Math.floor(Number(x[0].value)))
        x.forEach(k => {allTime += Number(k.value)})
        allTimeA = Time(Math.floor(Number(allTime)))
        localStorage.setItem('florrTime', JSON.stringify(x));
        newDoc.innerHTML = `<p style="font-size:12px">Since ${JSON.parse(localStorage.getItem('florrTime')).at(-1).date}:  ${allTimeA}</p><p style="font-size:20px">${time}</p>`
        allTime = 0
    }
}, 1000)

var chartCon = document.createElement('div')
chartCon.style = `
    width: 100%;
    height: 100%;
    position: absolute;
    z-index: 100;
    font-family: Ubuntu;
    top: 0;
    margin: 0;
    opacity: 0;
    pointer-events: none;
    transition: 0.4s ease-in-out;
`
chartCon.innerHTML = `
    <canvas id="chartCtx" style="background: rgba(255, 255, 255, 0.95);padding: 50px;font-family: Ubuntu;opacity: 1;transition: 0.4s ease-in-out;"></canvas>
`
document.querySelector('body').appendChild(chartCon)

var data = [],
    labels = [],
    localStr = JSON.parse(localStorage.getItem("florrTime")),
    len = 30

function be(date) {
    return date.slice(8, 10) + "/" + (new Date(Date.parse(date.slice(4, 7) + " 1, 2012")).getMonth() + 1) + "/" + date.slice(-4)
}

if (localStr.length < 30) len = localStr.length
for (let i = 0; i < len; i++) {
    data.unshift(localStr[i].value / 3600)
    labels.unshift(be(localStr[i].date))
}

Chart.defaults.font.family = 'Ubuntu';
Chart.defaults.font.size = 16;
Chart.defaults.plugins.legend.display = false
var chart = new Chart(document.getElementById('chartCtx'), {
    type: 'line',
    data: {
        labels: labels,
        datasets: [
            {
                label: 'All time',
                data: data,
                fill: true,
                borderColor: 1,
                borderWidth: 4,
                tension: 0.3,
            }
        ]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            title: {
                text: 'Playtime (Month)',
                display: true
            }
        },
        scales: {
            x: {
                title: {
                    display: true,
                    text: 'Date'
                }
            },
            y: {
                title: {
                    display: true,
                    text: 'Hours'
                }
            }
        },
    },
});

function updateData(chart) {
    var data = [],
        labels = [],
        localStr = JSON.parse(localStorage.getItem("florrTime")),
        len = 30
    function be(date) {
        return date.slice(8, 10) + "/" + (new Date(Date.parse(date.slice(4, 7) + " 1, 2012")).getMonth() + 1) + "/" + date.slice(-4)
    }
    if (localStr.length < 30) len = localStr.length
    for (let i = 0; i < len; i++) {
        data.unshift(localStr[i].value / 3600)
        labels.unshift(be(localStr[i].date))
    }
    chart.data.datasets[0].data = data
    chart.update()
}
document.documentElement.addEventListener("keydown", function() {
    if (event.keyCode === 191 && event.ctrlKey && event.shiftKey) {
        if (confirm("Confirm reset all")) {
            stop = true;
            localStorage.setItem('florrTime', JSON.stringify(leech))
        }
    }
    else if (event.keyCode === 186) newDoc.style.display = newDoc.style.display === 'none' ? '' : 'none'
    else if (event.keyCode === 220) {
        updateData(chart)
        chartCon.style.opacity = chartCon.style.opacity == '0' ? '1' : '0'
        chartCon.style.pointerEvents = chartCon.style.opacity == '0' ? 'none' : 'all'
    }
});