From 47fbbf01f7e24b68cce68513f55a4bfc70a11396 Mon Sep 17 00:00:00 2001 From: the1812 Date: Fri, 28 Sep 2018 15:18:42 +0800 Subject: [PATCH] Add ScheduleTime class --- bilibili-evolved.preview.user.js | 5 +- style/dark/dark-schedule.js | 92 +++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/bilibili-evolved.preview.user.js b/bilibili-evolved.preview.user.js index d744e680c..09d42b5d1 100644 --- a/bilibili-evolved.preview.user.js +++ b/bilibili-evolved.preview.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name Bilibili Evolved (Preview) -// @version 1.4.2 +// @version 1.4.3 // @description 增强哔哩哔哩Web端体验. (预览版分支) // @author Grant Howard, Coulomb-G // @match *://*.bilibili.com/* @@ -21,6 +21,9 @@ { const $ = unsafeWindow.$ || self$; const settings = { + darkScheduleStart: "18:00", + darkScheduleEnd: "06:00", + darkSchedule: false, blurSettingsPanel: false, blurVideoControl: false, toast: true, diff --git a/style/dark/dark-schedule.js b/style/dark/dark-schedule.js index 4125124c7..6ea29960e 100644 --- a/style/dark/dark-schedule.js +++ b/style/dark/dark-schedule.js @@ -2,7 +2,97 @@ { return () => { - + class ScheduleTime + { + constructor(text) + { + if (arguments.length === 0) + { + const now = new Date(); + this.hour = now.getHours(); + this.minute = now.getMinutes(); + } + else if (arguments.length === 1) + { + [this.hour, this.minute] = text + .split(":") + .slice(0, 2) + .map(it => this.validatePart(it)); + this.normalize(); + } + else if (arguments.length === 2) + { + [this.hour, this.minute] = arguments; + } + } + validatePart(text) + { + const number = parseInt(text); + if (!isNaN(number) && 0 <= number && number <= 59) + { + return number; + } + else + { + return null; + } + } + normalize() + { + while (this.minute < 0) + { + this.minute += 60; + this.hour -= 1; + } + while (this.minute >= 60) + { + this.minute -= 60; + this.hour += 1; + } + while (this.hour < 0) + { + this.hour += 24; + } + while (this.hour >= 24) + { + this.hour -= 24; + } + } + lessThan(other) + { + if (this.hour < other.hour || + this.hour === other.hour && this.minute < other.minute) + { + return true; + } + return false; + } + greaterThan(other) + { + if (this.hour > other.hour || + this.hour === other.hour && this.minute > other.minute) + { + return true; + } + return false; + } + equals(other) + { + return this.hour === other.hour && this.minute === other.minute; + } + isInRange(start, end) + { + let inRange = this.greaterThan(start) && this.lessThan(end); + if (start.greaterThan(end)) + { + inRange = this.greaterThan(start) || this.lessThan(end); + } + const result = inRange || + this.equals(start) || + this.equals(end); + return result; + } + } return { ajaxReload: false };