Add ScheduleTime class

This commit is contained in:
the1812 2018-09-28 15:18:42 +08:00
parent cf0052f2ae
commit 47fbbf01f7
2 changed files with 95 additions and 2 deletions

View File

@ -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,

View File

@ -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
};