Greasy Fork

Greasy Fork is available in English.

你看的时间太长了

You see it too long time.

当前为 2019-10-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name 你看的时间太长了
// @namespace You see it too long time.
// @version 0.0.9
// @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
// @grant GM_deleteValue
// @grant GM_listValues
// ==/UserScript==

// 每个网站最大观看时长,单位:分钟
const howMinutesCanYouWatchIt = 60
/* 以下无需修改 */
const oldLoadFun = window.onload
window.onload = async function(){
  oldLoadFun && oldLoadFun()
  // 获取当前所需时间对象
  const getNow = ()=>{
    const nowUTC = new Date()
    const now = Number((+nowUTC/1000).toFixed()) - nowUTC.getTimezoneOffset()*60
    const day = now - now%86400
    return { now, day }
  }
  // 储存当前数据
  const storeData = async (long)=>{
    GM_setValue(window.location.hostname, JSON.stringify({
      day: start.day,
      long: long
    }))
  }
  // 获取存储的数据
  const getData = async ()=>{
    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){
          return temp.long
        }
      } catch (error) {
        
      }
    }
    return 0
  }
  // 修改网页标题
  const changeTitle = title=>{
    document.title = title+document.title.replace(/^【[\d:-]+】/i, '')
  }
  // 两位数字
  const dbNum = num=>{
    return num<10 ? '0'+num : num
  }
  // 窗口是否最小化
  const isMin = ()=>{
    let isWinMin = false;
    if (window.outerWidth != undefined) {
        isWinMin = window.outerWidth <= 160 && window.outerHeight <= 27;
    } else {
        isWinMin = window.screenTop < -30000 && window.screenLeft < -30000;
    }
    return isWinMin;
  }
  // 时间更新
  const refreshTime = async ()=>{
    const n = getNow()  // 获取当前时间
    // 如果页面在后台或者窗口最小化,仅更新起始对象中的最后更新时间
    if(document.visibilityState !== 'visible' || isMin()){
      start.lastTime = n.now
      return
    }
    // 如果是当天
    start.historyLong = n.day === start.day
                        ? await getData() + n.now - start.lastTime
                        : n.now%86400
    start.day = n.day
    start.lastTime = n.now
    await storeData(start.historyLong)

    const lastTime = maxLong - start.historyLong
    if(start.historyLong >= maxLong){
      const alpha = 1 - 4*(start.historyLong-maxLong)/maxLong
      const opacity = ( alpha>1 ? 1 : ( alpha<0 ? 0 : alpha ) ).toFixed(2)
      document.querySelector('body').style.opacity = opacity
      if(document.fullscreenElement){// 如果全屏
        document.fullscreenElement.style.opacity = opacity
      }
      if(alpha<0){
        if(document.fullscreenElement){ document.exitFullscreen() } // 如果全屏则退出
        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)
      }
    }
    const lastTimeToShow = (lastTime>=0 ? '' : '-')
                          + dbNum( Math.floor( Math.abs(lastTime)/60) )
                          + ':'
                          + dbNum( Math.abs(lastTime)%60 )
    changeTitle('【'+lastTimeToShow+'】')
  }
  // 处理 hash 指令
  const hashCommand = async (hash, max)=>{
    if(/^#clear$/i.test(hash)){
      await storeData( 0 )
      window.location.hash = ''
      return
    }
    if(/^#clearAll$/i.test(hash)){
      const keys = await GM_listValues()
      for (let key of keys) {
        GM_deleteValue(key);
      }
      window.location.hash = ''
      return
    }
    if(/^#close$/i.test(hash)){
      await storeData( 1.25*max )
      window.location.hash = ''
      return
    }
    if(/^#add\d+$/i.test(hash)){
      let add = +hash.replace(/^#add(\d+)$/, '$1')
      add = isNaN(add) ? 0 : add*60
      let historyLong = await getData()
      historyLong = historyLong<add ? 0 : historyLong-add
      await storeData(historyLong)
      window.location.hash = ''
      return
    }
    if(/^#sub\d+$/i.test(hash)){
      let sub = +hash.replace(/^#sub(\d+)$/, '$1')
      sub = isNaN(sub) ? 0 : sub*60
      let historyLong = await getData()
      historyLong = historyLong+sub
      await storeData(historyLong)
      window.location.hash = ''
      return
    }
  }
  // 初始化 启动程序
  const start = getNow()
  start.lastTime = start.now
  start.historyLong = await getData()
  const maxLong = isNaN(howMinutesCanYouWatchIt) ? 3600 : howMinutesCanYouWatchIt*60
  await storeData( start.historyLong )

  await hashCommand(window.location.hash, maxLong)
  refreshTime()
  const timer = window.setInterval(refreshTime, 1e3)
}