天数倒计时
// ==UserScript==
// @name 天数倒计时
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.8
// @description 每天提醒你距离目标日期的天数
// @author Exisi
// @crontab * 7-23 once * *
// @grant GM_notification
// @grant GM_getValue
// @homepage https://scriptcat.org/script-show-page/372
// @supportURL https://exi.ink
// ==/UserScript==
/* ==UserConfig==
设置:
Notice:
title: 标题
description: 请输入通知标题
Timeout:
title: 通知时间
description: 请输入每次通知保持的时长,默认 3 秒
Year:
title: 年
description: 请输入年份,默认当前年份
Month:
title: 月
description: 请输入年份,默认当前月份
Day:
title: 日
description: 请输入天数,默认无
==/UserConfig== */
function getTotalDay(year, month) {
let date = new Date(year, month, 0);
return date.getDate();
}
function getDiffDay(date, aim_date){
let diff = (aim_date - date) / 1000 / 3600 / 24;
return diff;
}
function error(target){
GM_notification({
title : target.notice,
text : '日期错误,请重新设置时间',
timeout : GM_getValue('设置.Timeout') !='' ? GM_getValue('设置.Timeout') : 3000,
});
}
return new Promise((resolve, reject) => {
let date = new Date();
let aim_date = new Date();
let current = {
year : date.getFullYear(),
month : date.getMonth() + 1,
day : date.getDate(),
}
let target = {
notice : GM_getValue('设置.Notice') != '' ? GM_getValue('设置.Notice') : '天数倒计时',
year : GM_getValue('设置.Year') != '' && GM_getValue('设置.Year') != null ? GM_getValue('设置.Year') : current.year,
month : GM_getValue('设置.Month') != '' && GM_getValue('设置.Month') != null ? GM_getValue('设置.Month') : current.month,
day : GM_getValue('设置.Day'),
}
let total_day = getTotalDay(target.year, target.month);
let real = {
month : 0 < target.month && target.month <= 12,
day : 0 < target.day && target.day <= total_day,
}
if(!(real.month && real.day)){
resolve('日期错误,请重新设置时间');
return error(target);
}
let over = {
all : target.year > current.year,
year : target.year == current.year,
month : target.month == current.month,
fmonth : target.month > current.month,
day : target.day >= current.day,
}
if ( over.all || (over.year && over.month && over.day) || (over.year && over.fmonth)) {
aim_date.setFullYear(target.year, target.month - 1 , target.day);
let diff = getDiffDay(date, aim_date);
GM_notification({
title : target.notice,
text : '剩余天数:'+ diff,
timeout : GM_getValue('设置.Timeout') !='' ? GM_getValue('设置.Timeout') : 3000,
});
resolve();
} else {
resolve('倒计时完成,自动停止');
}
});