| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221 |
- <!DOCTYPE html>
- <html lang="zh-Hans-CN">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <meta name="description" content="🎓🍅♨️ - 高效记录进度的番茄钟" />
- <meta name="keywords" content="进度跟踪,效率工具,时间管理" />
- <!-- PWA 相关元标签 -->
- <meta name="theme-color" content="#4caf50" />
- <meta name="apple-mobile-web-app-capable" content="yes" />
- <meta name="apple-mobile-web-app-status-bar-style" content="default" />
- <meta name="apple-mobile-web-app-title" content="番茄钟" />
- <meta name="application-name" content="番茄钟" />
- <meta name="msapplication-TileColor" content="#4caf50" />
- <meta name="msapplication-config" content="browserconfig.xml" />
- <!-- PWA Manifest 和图标 -->
- <link rel="manifest" href="manifest.json" />
- <link
- rel="icon"
- type="image/svg+xml"
- href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%234caf50'/%3E%3Ctext x='50' y='65' font-size='50' text-anchor='middle' fill='white'%3E🍅%3C/text%3E%3C/svg%3E"
- />
- <link
- rel="apple-touch-icon"
- href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 180 180'%3E%3Crect width='180' height='180' fill='%234caf50' rx='24'/%3E%3Ctext x='90' y='110' font-size='90' text-anchor='middle' fill='white'%3E🍅%3C/text%3E%3C/svg%3E"
- />
- <title>🎓🍅♨️</title>
- <style>
- /* CSS 变量定义 - 浅色主题 */
- :root {
- /* 背景色 */
- --bg-primary: #f5f5f5;
- --bg-secondary: #fff;
- --bg-tertiary: #f0f0f0;
- /* 文字颜色 */
- --text-primary: #333;
- --text-secondary: #4caf50;
- --text-accent-1: #ff5722;
- --text-accent-2: #2196f3;
- /* 边框和进度条 */
- --border-primary: #e0e0e0;
- /* 阴影 */
- --shadow-primary: rgba(0, 0, 0, 0.1);
- /* 透明度 */
- --opacity-disabled: 0.7;
- --opacity-button-disabled: 0.5;
- /* 过渡效果 */
- --transition-base: all 0.3s ease;
- --transition-progress: width 1s linear, stroke-dashoffset 1s linear;
- }
- /* 深色主题变量 */
- @media (prefers-color-scheme: dark) {
- :root {
- /* 背景色 */
- --bg-primary: #1a1a1a;
- --bg-secondary: #2d2d2d;
- --bg-tertiary: #3a3a3a;
- /* 文字颜色 */
- --text-primary: #e0e0e0;
- --text-secondary: #66bb6a;
- --text-accent-1: #ff7043;
- --text-accent-2: #42a5f5;
- /* 边框和进度条 */
- --border-primary: #4a4a4a;
- /* 阴影 */
- --shadow-primary: rgba(0, 0, 0, 0.3);
- }
- }
- /* 重置样式 */
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
- sans-serif;
- background-color: var(--bg-primary);
- color: var(--text-primary);
- height: 100vh;
- overflow: hidden;
- transition: var(--transition-base);
- }
- /* 主容器 */
- .container {
- width: 100%;
- height: 100vh;
- display: flex;
- flex-direction: column;
- padding: 20px;
- gap: 20px;
- overflow: scroll;
- }
- /* 标题 */
- .title {
- display: none;
- text-align: center;
- font-size: 2rem;
- margin-bottom: 10px;
- }
- /* 内容区域 */
- .content {
- flex: 1;
- display: flex;
- flex-direction: column;
- gap: 20px;
- }
- /* 总学习时长 */
- .study-time {
- background-color: var(--bg-secondary);
- border-radius: 8px;
- padding: 20px;
- text-align: center;
- box-shadow: 0 2px 4px var(--shadow-primary);
- transition: var(--transition-base);
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .study-time-display {
- font-size: 3rem;
- font-weight: bold;
- color: var(--text-secondary);
- transition: var(--transition-base);
- }
- /* 番茄钟和空闲钟容器 */
- .timer-container {
- flex: 1;
- display: flex;
- flex-direction: column;
- gap: 20px;
- }
- /* 番茄钟 */
- .pomodoro {
- background-color: var(--bg-secondary);
- border-radius: 8px;
- padding: 20px;
- flex: 2;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- box-shadow: 0 2px 4px var(--shadow-primary);
- transition: var(--transition-base);
- }
- /* 空闲钟 */
- .idle {
- background-color: var(--bg-secondary);
- border-radius: 8px;
- padding: 20px;
- flex: 1;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- box-shadow: 0 2px 4px var(--shadow-primary);
- transition: var(--transition-base);
- }
- /* 圆形进度条容器 */
- .circular-progress {
- position: relative;
- width: 200px;
- height: 200px;
- margin: 20px 0;
- }
- .circular-progress svg {
- width: 100%;
- height: 100%;
- transform: rotate(-90deg);
- }
- .circular-progress .background {
- fill: none;
- stroke: var(--border-primary);
- stroke-width: 10;
- transition: var(--transition-base);
- }
- .circular-progress .progress {
- fill: none;
- stroke: var(--text-accent-1);
- stroke-width: 10;
- stroke-linecap: round;
- stroke-dasharray: 565.48;
- stroke-dashoffset: 565.48;
- transition: var(--transition-progress);
- }
- .circular-progress .time-display {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- font-size: 2rem;
- font-weight: bold;
- color: inherit;
- transition: var(--transition-base);
- }
- .idle .time-display {
- font-size: 1.5rem;
- font-weight: bold;
- color: inherit;
- transition: var(--transition-base);
- }
- /* 条形进度条 */
- .linear-progress {
- width: 100%;
- height: 20px;
- background-color: var(--border-primary);
- border-radius: 10px;
- margin: 20px 0;
- overflow: hidden;
- transition: var(--transition-base);
- }
- .linear-progress .progress {
- height: 100%;
- background-color: var(--text-accent-2);
- width: 0%;
- transition: var(--transition-progress);
- }
- /* 时间输入 */
- .time-input {
- font-size: 1.5rem;
- border: none;
- background: transparent;
- text-align: center;
- width: 120px;
- font-weight: bold;
- transition: var(--transition-base);
- color: inherit;
- }
- .time-input:focus {
- outline: none;
- background-color: var(--bg-tertiary);
- border-radius: 4px;
- transform: scale(1.05);
- }
- .time-input:disabled {
- cursor: not-allowed;
- opacity: var(--opacity-disabled);
- }
- /* 控制按钮 */
- .controls {
- display: flex;
- gap: 10px;
- margin-top: 20px;
- }
- .btn {
- background: none;
- border: none;
- font-size: 1.5rem;
- cursor: pointer;
- padding: 10px;
- border-radius: 50%;
- transition: var(--transition-base);
- color: inherit;
- }
- .btn:hover {
- background-color: var(--bg-tertiary);
- transform: scale(1.1);
- }
- .btn:active {
- transform: scale(0.95);
- }
- .btn:disabled {
- opacity: var(--opacity-button-disabled);
- cursor: not-allowed;
- transform: none;
- }
- /* 响应式布局 */
- @media (min-width: 768px) {
- .content {
- flex-direction: row;
- transition: flex-direction 0.3s ease;
- }
- .study-time {
- flex: 1;
- transition: flex 0.3s ease;
- }
- .timer-container {
- flex: 2;
- flex-direction: row;
- transition: flex 0.3s ease, flex-direction 0.3s ease;
- }
- }
- @media (min-width: 1200px) {
- .container {
- display: grid;
- grid-template-columns: 2fr 1fr;
- grid-template-rows: auto 1fr 1fr;
- gap: 20px;
- transition: var(--transition-base);
- }
- .title {
- grid-column: 1 / -1;
- }
- .pomodoro {
- grid-row: 2 / 4;
- grid-column: 1;
- transition: var(--transition-base);
- }
- .study-time {
- grid-row: 2;
- grid-column: 2;
- transition: var(--transition-base);
- }
- .idle {
- grid-row: 3;
- grid-column: 2;
- transition: var(--transition-base);
- }
- .content {
- display: contents;
- }
- .timer-container {
- display: contents;
- }
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1 class="title">🎓🍅♨️</h1>
- <div class="content">
- <div class="study-time">
- <h2>🎓</h2>
- <div class="study-time-display">0:00</div>
- <div class="controls">
- <button class="btn clear-btn">🧹</button>
- <button class="btn confirm-btn" style="display: none">☑️</button>
- <button class="btn cancel-btn" style="display: none">❎</button>
- </div>
- <!-- <div class="controls"> -->
- <!-- TODO: 实现“📄(导出数据文件)、⚙️(应用数据文件)和 🏠([源码](https://github.com/PJ-568/time-of-studying))”数据管理按钮和相关功能 -->
- <!-- </div> -->
- </div>
- <div class="timer-container">
- <div class="pomodoro">
- <h2>🍅</h2>
- <div class="circular-progress">
- <svg>
- <circle class="background" cx="100" cy="100" r="90"></circle>
- <circle class="progress" cx="100" cy="100" r="90"></circle>
- </svg>
- <div class="time-display">
- <input type="text" class="time-input" value="25:00" />
- </div>
- </div>
- <div class="controls">
- <button class="btn start-btn">▶️</button>
- <button class="btn pause-btn" style="display: none">⏸️</button>
- <button class="btn stop-btn" style="display: none">⏹️</button>
- </div>
- </div>
- <div class="idle">
- <h2>♨️</h2>
- <div class="linear-progress">
- <div class="progress"></div>
- </div>
- <div class="time-display">
- <input type="text" class="time-input" value="5:00" />
- </div>
- <div class="controls">
- <button class="btn start-btn">▶️</button>
- <button class="btn pause-btn" style="display: none">⏸️</button>
- <button class="btn stop-btn" style="display: none">⏹️</button>
- </div>
- </div>
- </div>
- </div>
- </div>
- <script>
- // 数据管理
- const dataManager = {
- // 初始化数据
- init() {
- const data = localStorage.getItem("timerData");
- if (data) {
- return JSON.parse(data);
- } else {
- return {
- totalStudyTime: 0,
- pomodoroTime: 1500, // 25 分钟
- idleTime: 300, // 5 分钟
- };
- }
- },
- // 保存数据
- save(data) {
- localStorage.setItem("timerData", JSON.stringify(data));
- },
- // 格式化时间(秒转 HH:MM)
- formatTime(seconds) {
- const hours = Math.floor(seconds / 3600);
- const minutes = Math.floor((seconds % 3600) / 60);
- return `${hours}:${minutes.toString().padStart(2, "0")}`;
- },
- // 格式化倒计时(秒转 MM:SS)
- formatCountdown(seconds) {
- const minutes = Math.floor(seconds / 60);
- const secs = seconds % 60;
- return `${minutes}:${secs.toString().padStart(2, "0")}`;
- },
- };
- // 番茄钟控制器
- class PomodoroTimer {
- constructor(element) {
- this.element = element;
- this.timeInput = element.querySelector(".time-input");
- this.startBtn = element.querySelector(".start-btn");
- this.pauseBtn = element.querySelector(".pause-btn");
- this.stopBtn = element.querySelector(".stop-btn");
- this.progressCircle = element.querySelector(
- ".circular-progress .progress"
- );
- this.circumference = 2 * Math.PI * 90; // 圆的周长
- this.data = dataManager.init();
- this.totalTime = this.data.pomodoroTime;
- this.remainingTime = this.totalTime;
- this.isRunning = false;
- this.interval = null;
- this.init();
- }
- init() {
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.updateProgress();
- // 确保初始状态下输入框可编辑
- this.timeInput.readOnly = false;
- // 事件监听
- this.startBtn.addEventListener("click", () => this.start());
- this.pauseBtn.addEventListener("click", () => this.pause());
- this.stopBtn.addEventListener("click", () => this.stop());
- // 允许编辑时间
- this.timeInput.addEventListener("blur", () => {
- if (!this.isRunning) {
- const timeParts = this.timeInput.value.split(":");
- if (timeParts.length === 2) {
- const minutes = parseInt(timeParts[0], 10);
- const seconds = parseInt(timeParts[1], 10);
- // 验证输入
- if (
- !isNaN(minutes) &&
- !isNaN(seconds) &&
- minutes >= 0 &&
- minutes <= 1440 &&
- seconds >= 0 &&
- seconds <= 59
- ) {
- this.totalTime = minutes * 60 + seconds;
- this.remainingTime = this.totalTime;
- this.data.pomodoroTime = this.totalTime;
- dataManager.save(this.data);
- this.updateProgress();
- } else {
- // 输入无效,重置为之前的有效值
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- }
- } else {
- // 格式无效,重置为之前的有效值
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- }
- }
- });
- // 限制输入格式
- this.timeInput.addEventListener("input", (e) => {
- let value = e.target.value;
- // 只允许数字和冒号
- value = value.replace(/[^0-9:]/g, "");
- // 处理冒号位置
- const colonIndex = value.indexOf(":");
- if (colonIndex === -1) {
- // 没有冒号,自动在倒数第二位前添加冒号
- if (value.length >= 3) {
- value = value.slice(0, -2) + ":" + value.slice(-2);
- }
- } else {
- // 有冒号,确保冒号后只有两位数字
- const parts = value.split(":");
- if (parts.length === 2) {
- const minutes = parts[0];
- let seconds = parts[1];
- // 限制秒数为两位
- if (seconds.length > 2) {
- seconds = seconds.slice(0, 2);
- }
- value = minutes + ":" + seconds;
- }
- }
- // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
- if (value.length > 7) {
- value = value.slice(0, 7);
- }
- e.target.value = value;
- });
- }
- start() {
- if (!this.isRunning) {
- this.isRunning = true;
- this.timeInput.readOnly = true;
- this.startBtn.style.display = "none";
- this.pauseBtn.style.display = "inline-block";
- this.stopBtn.style.display = "inline-block";
- this.interval = setInterval(() => {
- this.remainingTime--;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.updateProgress();
- if (this.remainingTime <= 0) {
- this.complete();
- }
- }, 1000);
- }
- }
- pause() {
- if (this.isRunning) {
- this.isRunning = false;
- clearInterval(this.interval);
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- }
- }
- stop() {
- this.isRunning = false;
- clearInterval(this.interval);
- // 将已进行的时间加入总学习时长
- const elapsed = this.totalTime - this.remainingTime;
- this.data.totalStudyTime += elapsed;
- dataManager.save(this.data);
- studyTimeManager.display.textContent = dataManager.formatTime(
- this.data.totalStudyTime
- );
- // 重置计时器
- this.remainingTime = this.totalTime;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.timeInput.readOnly = false;
- this.updateProgress();
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- this.stopBtn.style.display = "none";
- }
- complete() {
- this.isRunning = false;
- clearInterval(this.interval);
- // 将全部时间加入总学习时长
- this.data.totalStudyTime += this.totalTime;
- dataManager.save(this.data);
- studyTimeManager.display.textContent = dataManager.formatTime(
- this.data.totalStudyTime
- );
- // 重置计时器
- this.remainingTime = this.totalTime;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.timeInput.readOnly = false;
- this.updateProgress();
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- this.stopBtn.style.display = "none";
- // 发送通知
- this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
- }
- updateProgress() {
- const progress =
- (this.totalTime - this.remainingTime) / this.totalTime;
- const offset = this.circumference - progress * this.circumference;
- this.progressCircle.style.strokeDashoffset = offset;
- }
- sendNotification(title, message) {
- if ("Notification" in window) {
- if (Notification.permission === "granted") {
- new Notification(title, { body: message });
- } else if (Notification.permission !== "denied") {
- Notification.requestPermission().then((permission) => {
- if (permission === "granted") {
- new Notification(title, { body: message });
- }
- });
- }
- }
- // 播放声音
- const audio = new Audio(
- "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
- );
- audio.play();
- }
- }
- // 空闲钟控制器
- class IdleTimer {
- constructor(element) {
- this.element = element;
- this.timeInput = element.querySelector(".time-input");
- this.startBtn = element.querySelector(".start-btn");
- this.pauseBtn = element.querySelector(".pause-btn");
- this.stopBtn = element.querySelector(".stop-btn");
- this.progressBar = element.querySelector(
- ".linear-progress .progress"
- );
- this.data = dataManager.init();
- this.totalTime = this.data.idleTime;
- this.remainingTime = this.totalTime;
- this.isRunning = false;
- this.interval = null;
- this.init();
- }
- init() {
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.updateProgress();
- // 确保初始状态下输入框可编辑
- this.timeInput.readOnly = false;
- // 事件监听
- this.startBtn.addEventListener("click", () => this.start());
- this.pauseBtn.addEventListener("click", () => this.pause());
- this.stopBtn.addEventListener("click", () => this.stop());
- // 允许编辑时间
- this.timeInput.addEventListener("blur", () => {
- if (!this.isRunning) {
- const timeParts = this.timeInput.value.split(":");
- if (timeParts.length === 2) {
- const minutes = parseInt(timeParts[0], 10);
- const seconds = parseInt(timeParts[1], 10);
- // 验证输入
- if (
- !isNaN(minutes) &&
- !isNaN(seconds) &&
- minutes >= 0 &&
- minutes <= 1440 &&
- seconds >= 0 &&
- seconds <= 59
- ) {
- this.totalTime = minutes * 60 + seconds;
- this.remainingTime = this.totalTime;
- this.data.idleTime = this.totalTime;
- dataManager.save(this.data);
- this.updateProgress();
- } else {
- // 输入无效,重置为之前的有效值
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- }
- } else {
- // 格式无效,重置为之前的有效值
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- }
- }
- });
- // 限制输入格式
- this.timeInput.addEventListener("input", (e) => {
- let value = e.target.value;
- // 只允许数字和冒号
- value = value.replace(/[^0-9:]/g, "");
- // 处理冒号位置
- const colonIndex = value.indexOf(":");
- if (colonIndex === -1) {
- // 没有冒号,自动在倒数第二位前添加冒号
- if (value.length >= 3) {
- value = value.slice(0, -2) + ":" + value.slice(-2);
- }
- } else {
- // 有冒号,确保冒号后只有两位数字
- const parts = value.split(":");
- if (parts.length === 2) {
- const minutes = parts[0];
- let seconds = parts[1];
- // 限制秒数为两位
- if (seconds.length > 2) {
- seconds = seconds.slice(0, 2);
- }
- value = minutes + ":" + seconds;
- }
- }
- // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
- if (value.length > 7) {
- value = value.slice(0, 7);
- }
- e.target.value = value;
- });
- }
- start() {
- if (!this.isRunning) {
- this.isRunning = true;
- this.timeInput.readOnly = true;
- this.startBtn.style.display = "none";
- this.pauseBtn.style.display = "inline-block";
- this.stopBtn.style.display = "inline-block";
- this.interval = setInterval(() => {
- this.remainingTime--;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.updateProgress();
- if (this.remainingTime <= 0) {
- this.complete();
- }
- }, 1000);
- }
- }
- pause() {
- if (this.isRunning) {
- this.isRunning = false;
- clearInterval(this.interval);
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- }
- }
- stop() {
- this.isRunning = false;
- clearInterval(this.interval);
- // 重置计时器
- this.remainingTime = this.totalTime;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.timeInput.readOnly = false;
- this.updateProgress();
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- this.stopBtn.style.display = "none";
- }
- complete() {
- this.isRunning = false;
- clearInterval(this.interval);
- // 重置计时器
- this.remainingTime = this.totalTime;
- this.timeInput.value = dataManager.formatCountdown(
- this.remainingTime
- );
- this.timeInput.readOnly = false;
- this.updateProgress();
- this.startBtn.style.display = "inline-block";
- this.pauseBtn.style.display = "none";
- this.stopBtn.style.display = "none";
- // 发送通知
- this.sendNotification("空闲时间结束", "休息时间结束了!");
- }
- updateProgress() {
- const progress =
- (this.totalTime - this.remainingTime) / this.totalTime;
- this.progressBar.style.width = `${progress * 100}%`;
- }
- sendNotification(title, message) {
- if ("Notification" in window) {
- if (Notification.permission === "granted") {
- new Notification(title, { body: message });
- } else if (Notification.permission !== "denied") {
- Notification.requestPermission().then((permission) => {
- if (permission === "granted") {
- new Notification(title, { body: message });
- }
- });
- }
- }
- // 播放声音
- const audio = new Audio(
- "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
- );
- audio.play();
- }
- }
- // 总学习时长控制器
- class StudyTimeManager {
- constructor() {
- this.display = document.querySelector(".study-time-display");
- this.clearBtn = document.querySelector(".clear-btn");
- this.confirmBtn = document.querySelector(".confirm-btn");
- this.cancelBtn = document.querySelector(".cancel-btn");
- this.data = dataManager.init();
- this.init();
- }
- init() {
- // 初始化显示
- this.display.textContent = dataManager.formatTime(
- this.data.totalStudyTime
- );
- // 事件监听
- this.clearBtn.addEventListener("click", () =>
- this.showConfirmDialog()
- );
- this.confirmBtn.addEventListener("click", () =>
- this.clearStudyTime()
- );
- this.cancelBtn.addEventListener("click", () =>
- this.hideConfirmDialog()
- );
- }
- showConfirmDialog() {
- // 显示确认和取消按钮,隐藏清空按钮
- this.clearBtn.style.display = "none";
- this.confirmBtn.style.display = "inline-block";
- this.cancelBtn.style.display = "inline-block";
- }
- hideConfirmDialog() {
- // 隐藏确认和取消按钮,显示清空按钮
- this.clearBtn.style.display = "inline-block";
- this.confirmBtn.style.display = "none";
- this.cancelBtn.style.display = "none";
- }
- clearStudyTime() {
- // 清空学习时长
- this.data.totalStudyTime = 0;
- dataManager.save(this.data);
- this.display.textContent = dataManager.formatTime(0);
- // 隐藏确认和取消按钮,显示清空按钮
- this.hideConfirmDialog();
- }
- }
- // 初始化应用
- const data = dataManager.init();
- const studyTimeManager = new StudyTimeManager();
- // 初始化番茄钟
- const pomodoroElement = document.querySelector(".pomodoro");
- const pomodoroTimer = new PomodoroTimer(pomodoroElement);
- // 初始化空闲钟
- const idleElement = document.querySelector(".idle");
- const idleTimer = new IdleTimer(idleElement);
- // PWA 相关功能
- // 注册 Service Worker
- if ("serviceWorker" in navigator) {
- window.addEventListener("load", () => {
- navigator.serviceWorker
- .register("./sw.js")
- .then((registration) => {
- console.log("Service Worker 注册成功:", registration.scope);
- // 检查是否有更新
- registration.addEventListener("updatefound", () => {
- const newWorker = registration.installing;
- newWorker.addEventListener("statechange", () => {
- if (
- newWorker.state === "installed" &&
- navigator.serviceWorker.controller
- ) {
- // 有新版本可用
- console.log("发现新版本的应用");
- // 这里可以添加更新提示
- }
- });
- });
- })
- .catch((error) => {
- console.error("Service Worker 注册失败:", error);
- });
- });
- }
- // PWA 安装提示 - 使用图标而非文字
- let deferredPrompt;
- const installPromptElement = document.createElement("div");
- installPromptElement.id = "install-prompt";
- installPromptElement.style.cssText = `
- position: fixed;
- bottom: 20px;
- left: 50%;
- transform: translateX(-50%);
- background-color: var(--bg-secondary);
- color: var(--text-primary);
- padding: 16px;
- border-radius: 8px;
- box-shadow: 0 4px 12px var(--shadow-primary);
- display: none;
- align-items: center;
- gap: 16px;
- z-index: 1000;
- animation: slideUp 0.3s ease-out;
- `;
- // 使用图标表示安装应用
- const installIcon = document.createElement("span");
- installIcon.textContent = "📱";
- installIcon.style.cssText = "font-size: 24px;";
- installPromptElement.appendChild(installIcon);
- const installButton = document.createElement("button");
- installButton.textContent = "⬇️";
- installButton.className = "btn install-btn";
- installButton.title = "安装应用";
- installButton.style.cssText = `
- background-color: var(--text-secondary);
- color: white;
- border: none;
- padding: 8px 16px;
- border-radius: 50%;
- cursor: pointer;
- font-size: 20px;
- width: 44px;
- height: 44px;
- display: flex;
- align-items: center;
- justify-content: center;
- `;
- installPromptElement.appendChild(installButton);
- const dismissButton = document.createElement("button");
- dismissButton.textContent = "❌";
- dismissButton.className = "btn dismiss-btn";
- dismissButton.title = "忽略";
- dismissButton.style.cssText = `
- background: none;
- border: none;
- font-size: 18px;
- cursor: pointer;
- padding: 4px;
- color: var(--text-primary);
- `;
- installPromptElement.appendChild(dismissButton);
- // 添加动画样式
- const style = document.createElement("style");
- style.textContent = `
- @keyframes slideUp {
- from { transform: translateX(-50%) translateY(100px); opacity: 0; }
- to { transform: translateX(-50%) translateY(0); opacity: 1; }
- }
-
- @keyframes slideDown {
- from { transform: translateX(-50%) translateY(0); opacity: 1; }
- to { transform: translateX(-50%) translateY(100px); opacity: 0; }
- }
-
- .install-prompt-dismiss {
- animation: slideDown 0.3s ease-out forwards;
- }
- `;
- document.head.appendChild(style);
- document.body.appendChild(installPromptElement);
- // 监听安装提示事件
- window.addEventListener("beforeinstallprompt", (e) => {
- // 阻止默认的安装提示
- e.preventDefault();
- // 存储事件以便后续使用
- deferredPrompt = e;
- // 检查用户是否已经安装了应用
- if (!localStorage.getItem("pwa-install-dismissed")) {
- // 显示安装提示
- installPromptElement.style.display = "flex";
- }
- });
- // 安装按钮点击事件
- installButton.addEventListener("click", async () => {
- if (deferredPrompt) {
- // 显示安装提示
- deferredPrompt.prompt();
- // 等待用户响应
- const { outcome } = await deferredPrompt.userChoice;
- console.log(`用户${outcome === "accepted" ? "接受" : "拒绝"}了安装`);
- // 重置 deferredPrompt
- deferredPrompt = null;
- // 隐藏安装提示
- hideInstallPrompt();
- }
- });
- // 忽略按钮点击事件
- dismissButton.addEventListener("click", () => {
- hideInstallPrompt();
- // 记录用户已经忽略过安装提示
- localStorage.setItem("pwa-install-dismissed", "true");
- });
- // 隐藏安装提示的函数
- function hideInstallPrompt() {
- installPromptElement.classList.add("install-prompt-dismiss");
- setTimeout(() => {
- installPromptElement.style.display = "none";
- installPromptElement.classList.remove("install-prompt-dismiss");
- }, 300);
- }
- // 检查应用是否已经安装
- window.addEventListener("appinstalled", () => {
- console.log("应用已成功安装");
- hideInstallPrompt();
- localStorage.setItem("pwa-installed", "true");
- });
- // 处理应用启动时的 URL 参数
- function handleAppAction() {
- const urlParams = new URLSearchParams(window.location.search);
- const action = urlParams.get("action");
- if (action === "pomodoro") {
- // 自动开始番茄钟
- setTimeout(() => {
- if (!pomodoroTimer.isRunning) {
- pomodoroTimer.start();
- }
- }, 1000);
- } else if (action === "idle") {
- // 自动开始空闲钟
- setTimeout(() => {
- if (!idleTimer.isRunning) {
- idleTimer.start();
- }
- }, 1000);
- } else if (action === "stats") {
- // 高亮显示学习时长区域
- const studyTimeElement = document.querySelector(".study-time");
- studyTimeElement.style.animation = "pulse 2s ease-in-out 3";
- const pulseStyle = document.createElement("style");
- pulseStyle.textContent = `
- @keyframes pulse {
- 0%, 100% { transform: scale(1); }
- 50% { transform: scale(1.05); }
- }
- `;
- document.head.appendChild(pulseStyle);
- setTimeout(() => {
- studyTimeElement.style.animation = "";
- }, 6000);
- }
- }
- // 处理应用启动
- handleAppAction();
- // 请求通知权限
- if ("Notification" in window && Notification.permission === "default") {
- Notification.requestPermission();
- }
- // 处理页面可见性变化
- let lastVisibleTime = Date.now();
- document.addEventListener("visibilitychange", () => {
- if (document.visibilityState === "visible") {
- // 页面变为可见时,计算经过的时间并更新计时器
- const now = Date.now();
- const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
- if (pomodoroTimer.isRunning) {
- pomodoroTimer.remainingTime = Math.max(
- 0,
- pomodoroTimer.remainingTime - elapsedSeconds
- );
- pomodoroTimer.timeInput.value = dataManager.formatCountdown(
- pomodoroTimer.remainingTime
- );
- pomodoroTimer.updateProgress();
- if (pomodoroTimer.remainingTime <= 0) {
- pomodoroTimer.complete();
- }
- }
- if (idleTimer.isRunning) {
- idleTimer.remainingTime = Math.max(
- 0,
- idleTimer.remainingTime - elapsedSeconds
- );
- idleTimer.timeInput.value = dataManager.formatCountdown(
- idleTimer.remainingTime
- );
- idleTimer.updateProgress();
- if (idleTimer.remainingTime <= 0) {
- idleTimer.complete();
- }
- }
- }
- lastVisibleTime = Date.now();
- });
- // 页面关闭前提醒
- window.addEventListener("beforeunload", (e) => {
- if (pomodoroTimer.isRunning || idleTimer.isRunning) {
- e.preventDefault();
- e.returnValue = "您有正在运行的计时器,确定要离开吗?";
- return e.returnValue;
- }
- });
- </script>
- </body>
- </html>
|