Greasy Fork

Greasy Fork is available in English.

你看的时间太长了

You see it too long time.

目前为 2019-10-25 提交的版本。查看 最新版本

// ==UserScript==
// @name 你看的时间太长了
// @namespace You see it too long time.
// @version 0.0.5
// @author 稻米鼠
// @description You see it too long time.
// @run-at document-idle
// @homepage https://meta.appinn.net/t/11501
// @supportURL https://meta.appinn.com/t/11501
// @match *://*/*
// @noframes
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==

// 每个网站最大观看时长,单位:分钟
const howMinutesCanYouWatchIt = 60
/* 以下无需修改 */
const oldLoadFun = window.onload
window.onload=function(){
  oldLoadFun && oldLoadFun()
  // 获取当前所需时间对象
  const getNow = ()=>{
    const nowUTC = new Date()
    const now = +nowUTC - nowUTC.getTimezoneOffset()*6e4
    const day = now - now%864e5
    return { now, day }
  }
  // 初始化
  const start = getNow()
  start.lastTime = start.now
  start.historyLong = 0
  const maxLong = isNaN(howMinutesCanYouWatchIt) ? 36e5 : howMinutesCanYouWatchIt*6e4
  // 储存当前数据
  const storeData = (long)=>{
    const todayYouSeeItHowLong = {
      day: start.day,
      long: long
    }
    GM_setValue(window.location.hostname, JSON.stringify(todayYouSeeItHowLong))
  }
  // 修改网页标题
  const changeTitle = title=>{
    document.title = title+document.title.replace(/^【[OX]+】/i, '')
  }
  // 如果存在数据
  if(GM_getValue(window.location.hostname)){
    try {
      const temp = JSON.parse(GM_getValue(window.location.hostname))
      // 如果有合理的数据,并且是当天数据
      if(temp.day && temp.day === start.day && temp.long){
        start.historyLong = temp.long
      }else{
        storeData(0)
      }
    } catch (error) {
      storeData(0)
    }
  } else {
    storeData(0)
  }
  const timer = window.setInterval(()=>{
    const n = getNow()
    if(document.visibilityState !== 'visible'){
      start.lastTime = n.now
      return
    }
    // console.log(start, n)
    if(n.day === start.day){
      start.historyLong += n.now - start.lastTime
      start.lastTime = n.now
    }else{
      start.day = n.day
      start.historyLong = n.now%864e5
      start.lastTime = n.now
    }
    storeData(start.historyLong)

    const lastTime = +(100*(maxLong - start.historyLong)/maxLong).toFixed(2)
    if(start.historyLong >= maxLong){
      const alpha = 1 - 4*(start.historyLong-maxLong)/maxLong
      document.querySelector('body').style.opacity = ( alpha>1 ? 1 : ( alpha<0 ? 0 : alpha ) ).toFixed(2)
      if(alpha<0){
        document.querySelector('html').style.backgroundImage = 'url("https://i.v2ex.co/WJ15n12m.png")'
        document.querySelector('html').style.backgroundPosition = 'center top'
        document.querySelector('html').style.backgroundRepeat = 'no-repeat'
        document.querySelector('html').style.backgroundAttachment = 'fixed'
        window.clearInterval(timer)
      }
    }
    if(lastTime>=87.5){ changeTitle('【OOOO】'); return }
    if(lastTime>=62.5){ changeTitle('【OOOX】'); return }
    if(lastTime>=37.5){ changeTitle('【OOXX】'); return }
    if(lastTime>=12.5){ changeTitle('【OXXX】'); return }
    if(lastTime>=0){ changeTitle('【XXXX】'); return }
    changeTitle(Math.floor(n.now/1000)%2 ? '【X】' : '【O】')
  }, 1e3)
}