Files
GrayMode/Plugin.php
Dylan Wu 77d1c59cd0 Add Plugin.php
Signed-off-by: Dylan Wu <i@dylanwu.space>
2025-08-04 04:34:57 -04:00

83 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 页面黑白显示插件(支持时间段、指定日期、每月/每年循环)
*
* @package GrayModePlugin
* @author Your
* @version 1.1.0
* @link https://your.site
*/
class GrayModePlugin implements Typecho_Plugin_Interface
{
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->header = [__CLASS__, 'changeheader'];
}
public static function deactivate() {}
public static function config(Typecho_Widget_Helper_Form $form)
{
$start = new Typecho_Widget_Helper_Form_Element_Text(
'startTime', NULL, '00:00', '开始时间', '格式如 08:00'
);
$end = new Typecho_Widget_Helper_Form_Element_Text(
'endTime', NULL, '23:59', '结束时间', '格式如 22:00'
);
$dates = new Typecho_Widget_Helper_Form_Element_Textarea(
'specialDates', NULL, '',
'灰白显示日期列表',
'支持多种格式:<br>
- 一次性2025-09-18<br>
- 每年重复04-04每年4月4日<br>
- 每月重复:-18每月18日<br>
多个值用英文逗号分隔2025-09-18,04-04,-18'
);
$form->addInput($start);
$form->addInput($end);
$form->addInput($dates);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
public static function changeheader()
{
$options = Typecho_Widget::widget('Widget_Options')->plugin('GrayModePlugin');
$start = $options->startTime;
$end = $options->endTime;
$dates = trim($options->specialDates);
$now = date('H:i');
// 判断是否在时间区间内
$inTimeRange = ($start <= $now && $now <= $end);
if (!$inTimeRange) return;
$today = date('Y-m-d');
$monthDay = date('m-d');
$dayOnly = date('d');
$dateList = array_filter(array_map('trim', explode(',', $dates)));
foreach ($dateList as $date) {
if (
$date === $today || // 一次性完整日期
$date === $monthDay || // 每年循环
$date === '-' . $dayOnly // 每月循环
) {
echo <<<EOT
<style>
html {
filter: grayscale(100%);
-webkit-filter: grayscale(100%);
}
</style>
EOT;
break;
}
}
}
}