index.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta name="description" content="🎓🍅♨️ - 高效记录进度的番茄钟" />
  7. <meta name="keywords" content="进度跟踪,效率工具,时间管理" />
  8. <link
  9. rel="icon"
  10. type="image/svg+xml"
  11. href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E⌚%3C/text%3E%3C/svg%3E"
  12. />
  13. <title>🎓🍅♨️</title>
  14. <style>
  15. /* CSS 变量定义 - 浅色主题 */
  16. :root {
  17. /* 背景色 */
  18. --bg-primary: #f5f5f5;
  19. --bg-secondary: #fff;
  20. --bg-tertiary: #f0f0f0;
  21. /* 文字颜色 */
  22. --text-primary: #333;
  23. --text-secondary: #4caf50;
  24. --text-accent-1: #ff5722;
  25. --text-accent-2: #2196f3;
  26. /* 边框和进度条 */
  27. --border-primary: #e0e0e0;
  28. /* 阴影 */
  29. --shadow-primary: rgba(0, 0, 0, 0.1);
  30. /* 透明度 */
  31. --opacity-disabled: 0.7;
  32. --opacity-button-disabled: 0.5;
  33. /* 过渡效果 */
  34. --transition-base: all 0.3s ease;
  35. --transition-progress: width 1s linear, stroke-dashoffset 1s linear;
  36. }
  37. /* 深色主题变量 */
  38. @media (prefers-color-scheme: dark) {
  39. :root {
  40. /* 背景色 */
  41. --bg-primary: #1a1a1a;
  42. --bg-secondary: #2d2d2d;
  43. --bg-tertiary: #3a3a3a;
  44. /* 文字颜色 */
  45. --text-primary: #e0e0e0;
  46. --text-secondary: #66bb6a;
  47. --text-accent-1: #ff7043;
  48. --text-accent-2: #42a5f5;
  49. /* 边框和进度条 */
  50. --border-primary: #4a4a4a;
  51. /* 阴影 */
  52. --shadow-primary: rgba(0, 0, 0, 0.3);
  53. }
  54. }
  55. /* 重置样式 */
  56. * {
  57. margin: 0;
  58. padding: 0;
  59. box-sizing: border-box;
  60. }
  61. body {
  62. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  63. sans-serif;
  64. background-color: var(--bg-primary);
  65. color: var(--text-primary);
  66. height: 100vh;
  67. overflow: hidden;
  68. transition: var(--transition-base);
  69. }
  70. /* 主容器 */
  71. .container {
  72. width: 100%;
  73. height: 100vh;
  74. display: flex;
  75. flex-direction: column;
  76. padding: 20px;
  77. gap: 20px;
  78. overflow: scroll;
  79. }
  80. /* 标题 */
  81. .title {
  82. display: none;
  83. text-align: center;
  84. font-size: 2rem;
  85. margin-bottom: 10px;
  86. }
  87. /* 内容区域 */
  88. .content {
  89. flex: 1;
  90. display: flex;
  91. flex-direction: column;
  92. gap: 20px;
  93. }
  94. /* 总学习时长 */
  95. .study-time {
  96. background-color: var(--bg-secondary);
  97. border-radius: 8px;
  98. padding: 20px;
  99. text-align: center;
  100. box-shadow: 0 2px 4px var(--shadow-primary);
  101. transition: var(--transition-base);
  102. }
  103. .study-time-display {
  104. font-size: 3rem;
  105. font-weight: bold;
  106. color: var(--text-secondary);
  107. transition: var(--transition-base);
  108. }
  109. /* 番茄钟和空闲钟容器 */
  110. .timer-container {
  111. flex: 1;
  112. display: flex;
  113. flex-direction: column;
  114. gap: 20px;
  115. }
  116. /* 番茄钟 */
  117. .pomodoro {
  118. background-color: var(--bg-secondary);
  119. border-radius: 8px;
  120. padding: 20px;
  121. flex: 2;
  122. display: flex;
  123. flex-direction: column;
  124. align-items: center;
  125. justify-content: center;
  126. box-shadow: 0 2px 4px var(--shadow-primary);
  127. transition: var(--transition-base);
  128. }
  129. /* 空闲钟 */
  130. .idle {
  131. background-color: var(--bg-secondary);
  132. border-radius: 8px;
  133. padding: 20px;
  134. flex: 1;
  135. display: flex;
  136. flex-direction: column;
  137. align-items: center;
  138. justify-content: center;
  139. box-shadow: 0 2px 4px var(--shadow-primary);
  140. transition: var(--transition-base);
  141. }
  142. /* 圆形进度条容器 */
  143. .circular-progress {
  144. position: relative;
  145. width: 200px;
  146. height: 200px;
  147. margin: 20px 0;
  148. }
  149. .circular-progress svg {
  150. width: 100%;
  151. height: 100%;
  152. transform: rotate(-90deg);
  153. }
  154. .circular-progress .background {
  155. fill: none;
  156. stroke: var(--border-primary);
  157. stroke-width: 10;
  158. transition: var(--transition-base);
  159. }
  160. .circular-progress .progress {
  161. fill: none;
  162. stroke: var(--text-accent-1);
  163. stroke-width: 10;
  164. stroke-linecap: round;
  165. stroke-dasharray: 565.48;
  166. stroke-dashoffset: 565.48;
  167. transition: var(--transition-progress);
  168. }
  169. .circular-progress .time-display {
  170. position: absolute;
  171. top: 50%;
  172. left: 50%;
  173. transform: translate(-50%, -50%);
  174. font-size: 2rem;
  175. font-weight: bold;
  176. color: inherit;
  177. transition: var(--transition-base);
  178. }
  179. .idle .time-display {
  180. font-size: 1.5rem;
  181. font-weight: bold;
  182. color: inherit;
  183. transition: var(--transition-base);
  184. }
  185. /* 条形进度条 */
  186. .linear-progress {
  187. width: 100%;
  188. height: 20px;
  189. background-color: var(--border-primary);
  190. border-radius: 10px;
  191. margin: 20px 0;
  192. overflow: hidden;
  193. transition: var(--transition-base);
  194. }
  195. .linear-progress .progress {
  196. height: 100%;
  197. background-color: var(--text-accent-2);
  198. width: 0%;
  199. transition: var(--transition-progress);
  200. }
  201. /* 时间输入 */
  202. .time-input {
  203. font-size: 1.5rem;
  204. border: none;
  205. background: transparent;
  206. text-align: center;
  207. width: 120px;
  208. font-weight: bold;
  209. transition: var(--transition-base);
  210. color: inherit;
  211. }
  212. .time-input:focus {
  213. outline: none;
  214. background-color: var(--bg-tertiary);
  215. border-radius: 4px;
  216. transform: scale(1.05);
  217. }
  218. .time-input:disabled {
  219. cursor: not-allowed;
  220. opacity: var(--opacity-disabled);
  221. }
  222. /* 控制按钮 */
  223. .controls {
  224. display: flex;
  225. gap: 10px;
  226. margin-top: 20px;
  227. }
  228. .btn {
  229. background: none;
  230. border: none;
  231. font-size: 1.5rem;
  232. cursor: pointer;
  233. padding: 10px;
  234. border-radius: 50%;
  235. transition: var(--transition-base);
  236. color: inherit;
  237. }
  238. .btn:hover {
  239. background-color: var(--bg-tertiary);
  240. transform: scale(1.1);
  241. }
  242. .btn:active {
  243. transform: scale(0.95);
  244. }
  245. .btn:disabled {
  246. opacity: var(--opacity-button-disabled);
  247. cursor: not-allowed;
  248. transform: none;
  249. }
  250. /* 响应式布局 */
  251. @media (min-width: 768px) {
  252. .content {
  253. flex-direction: row;
  254. transition: flex-direction 0.3s ease;
  255. }
  256. .study-time {
  257. flex: 1;
  258. transition: flex 0.3s ease;
  259. }
  260. .timer-container {
  261. flex: 2;
  262. flex-direction: row;
  263. transition: flex 0.3s ease, flex-direction 0.3s ease;
  264. }
  265. }
  266. @media (min-width: 1200px) {
  267. .container {
  268. display: grid;
  269. grid-template-columns: 2fr 1fr;
  270. grid-template-rows: auto 1fr 1fr;
  271. gap: 20px;
  272. transition: var(--transition-base);
  273. }
  274. .title {
  275. grid-column: 1 / -1;
  276. }
  277. .pomodoro {
  278. grid-row: 2 / 4;
  279. grid-column: 1;
  280. transition: var(--transition-base);
  281. }
  282. .study-time {
  283. grid-row: 2;
  284. grid-column: 2;
  285. transition: var(--transition-base);
  286. }
  287. .idle {
  288. grid-row: 3;
  289. grid-column: 2;
  290. transition: var(--transition-base);
  291. }
  292. .content {
  293. display: contents;
  294. }
  295. .timer-container {
  296. display: contents;
  297. }
  298. }
  299. </style>
  300. </head>
  301. <body>
  302. <div class="container">
  303. <h1 class="title">🎓🍅♨️</h1>
  304. <div class="content">
  305. <div class="study-time">
  306. <h2>🎓</h2>
  307. <div class="study-time-display">0:00</div>
  308. </div>
  309. <div class="timer-container">
  310. <div class="pomodoro">
  311. <h2>🍅</h2>
  312. <div class="circular-progress">
  313. <svg>
  314. <circle class="background" cx="100" cy="100" r="90"></circle>
  315. <circle class="progress" cx="100" cy="100" r="90"></circle>
  316. </svg>
  317. <div class="time-display">
  318. <input type="text" class="time-input" value="25:00" />
  319. </div>
  320. </div>
  321. <div class="controls">
  322. <button class="btn start-btn">▶️</button>
  323. <button class="btn pause-btn" style="display: none">⏸️</button>
  324. <button class="btn stop-btn" style="display: none">⏹️</button>
  325. </div>
  326. </div>
  327. <div class="idle">
  328. <h2>♨️</h2>
  329. <div class="linear-progress">
  330. <div class="progress"></div>
  331. </div>
  332. <div class="time-display">
  333. <input type="text" class="time-input" value="5:00" />
  334. </div>
  335. <div class="controls">
  336. <button class="btn start-btn">▶️</button>
  337. <button class="btn pause-btn" style="display: none">⏸️</button>
  338. <button class="btn stop-btn" style="display: none">⏹️</button>
  339. </div>
  340. </div>
  341. </div>
  342. </div>
  343. </div>
  344. <script>
  345. // 数据管理
  346. const dataManager = {
  347. // 初始化数据
  348. init() {
  349. const data = localStorage.getItem("timerData");
  350. if (data) {
  351. return JSON.parse(data);
  352. } else {
  353. return {
  354. totalStudyTime: 0,
  355. pomodoroTime: 1500, // 25 分钟
  356. idleTime: 300, // 5 分钟
  357. };
  358. }
  359. },
  360. // 保存数据
  361. save(data) {
  362. localStorage.setItem("timerData", JSON.stringify(data));
  363. },
  364. // 格式化时间(秒转 HH:MM)
  365. formatTime(seconds) {
  366. const hours = Math.floor(seconds / 3600);
  367. const minutes = Math.floor((seconds % 3600) / 60);
  368. return `${hours}:${minutes.toString().padStart(2, "0")}`;
  369. },
  370. // 格式化倒计时(秒转 MM:SS)
  371. formatCountdown(seconds) {
  372. const minutes = Math.floor(seconds / 60);
  373. const secs = seconds % 60;
  374. return `${minutes}:${secs.toString().padStart(2, "0")}`;
  375. },
  376. };
  377. // 番茄钟控制器
  378. class PomodoroTimer {
  379. constructor(element) {
  380. this.element = element;
  381. this.timeInput = element.querySelector(".time-input");
  382. this.startBtn = element.querySelector(".start-btn");
  383. this.pauseBtn = element.querySelector(".pause-btn");
  384. this.stopBtn = element.querySelector(".stop-btn");
  385. this.progressCircle = element.querySelector(
  386. ".circular-progress .progress"
  387. );
  388. this.circumference = 2 * Math.PI * 90; // 圆的周长
  389. this.data = dataManager.init();
  390. this.totalTime = this.data.pomodoroTime;
  391. this.remainingTime = this.totalTime;
  392. this.isRunning = false;
  393. this.interval = null;
  394. this.init();
  395. }
  396. init() {
  397. this.timeInput.value = dataManager.formatCountdown(
  398. this.remainingTime
  399. );
  400. this.updateProgress();
  401. // 确保初始状态下输入框可编辑
  402. this.timeInput.readOnly = false;
  403. // 事件监听
  404. this.startBtn.addEventListener("click", () => this.start());
  405. this.pauseBtn.addEventListener("click", () => this.pause());
  406. this.stopBtn.addEventListener("click", () => this.stop());
  407. // 允许编辑时间
  408. this.timeInput.addEventListener("blur", () => {
  409. if (!this.isRunning) {
  410. const timeParts = this.timeInput.value.split(":");
  411. if (timeParts.length === 2) {
  412. const minutes = parseInt(timeParts[0], 10);
  413. const seconds = parseInt(timeParts[1], 10);
  414. // 验证输入
  415. if (
  416. !isNaN(minutes) &&
  417. !isNaN(seconds) &&
  418. minutes >= 0 &&
  419. minutes <= 1440 &&
  420. seconds >= 0 &&
  421. seconds <= 59
  422. ) {
  423. this.totalTime = minutes * 60 + seconds;
  424. this.remainingTime = this.totalTime;
  425. this.data.pomodoroTime = this.totalTime;
  426. dataManager.save(this.data);
  427. this.updateProgress();
  428. } else {
  429. // 输入无效,重置为之前的有效值
  430. this.timeInput.value = dataManager.formatCountdown(
  431. this.remainingTime
  432. );
  433. }
  434. } else {
  435. // 格式无效,重置为之前的有效值
  436. this.timeInput.value = dataManager.formatCountdown(
  437. this.remainingTime
  438. );
  439. }
  440. }
  441. });
  442. // 限制输入格式
  443. this.timeInput.addEventListener("input", (e) => {
  444. let value = e.target.value;
  445. // 只允许数字和冒号
  446. value = value.replace(/[^0-9:]/g, "");
  447. // 处理冒号位置
  448. const colonIndex = value.indexOf(":");
  449. if (colonIndex === -1) {
  450. // 没有冒号,自动在倒数第二位前添加冒号
  451. if (value.length >= 3) {
  452. value = value.slice(0, -2) + ":" + value.slice(-2);
  453. }
  454. } else {
  455. // 有冒号,确保冒号后只有两位数字
  456. const parts = value.split(":");
  457. if (parts.length === 2) {
  458. const minutes = parts[0];
  459. let seconds = parts[1];
  460. // 限制秒数为两位
  461. if (seconds.length > 2) {
  462. seconds = seconds.slice(0, 2);
  463. }
  464. value = minutes + ":" + seconds;
  465. }
  466. }
  467. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  468. if (value.length > 7) {
  469. value = value.slice(0, 7);
  470. }
  471. e.target.value = value;
  472. });
  473. }
  474. start() {
  475. if (!this.isRunning) {
  476. this.isRunning = true;
  477. this.timeInput.readOnly = true;
  478. this.startBtn.style.display = "none";
  479. this.pauseBtn.style.display = "inline-block";
  480. this.stopBtn.style.display = "inline-block";
  481. this.interval = setInterval(() => {
  482. this.remainingTime--;
  483. this.timeInput.value = dataManager.formatCountdown(
  484. this.remainingTime
  485. );
  486. this.updateProgress();
  487. if (this.remainingTime <= 0) {
  488. this.complete();
  489. }
  490. }, 1000);
  491. }
  492. }
  493. pause() {
  494. if (this.isRunning) {
  495. this.isRunning = false;
  496. clearInterval(this.interval);
  497. this.startBtn.style.display = "inline-block";
  498. this.pauseBtn.style.display = "none";
  499. }
  500. }
  501. stop() {
  502. this.isRunning = false;
  503. clearInterval(this.interval);
  504. // 将已进行的时间加入总学习时长
  505. const elapsed = this.totalTime - this.remainingTime;
  506. this.data.totalStudyTime += elapsed;
  507. dataManager.save(this.data);
  508. studyTimeDisplay.textContent = dataManager.formatTime(
  509. this.data.totalStudyTime
  510. );
  511. // 重置计时器
  512. this.remainingTime = this.totalTime;
  513. this.timeInput.value = dataManager.formatCountdown(
  514. this.remainingTime
  515. );
  516. this.timeInput.readOnly = false;
  517. this.updateProgress();
  518. this.startBtn.style.display = "inline-block";
  519. this.pauseBtn.style.display = "none";
  520. this.stopBtn.style.display = "none";
  521. }
  522. complete() {
  523. this.isRunning = false;
  524. clearInterval(this.interval);
  525. // 将全部时间加入总学习时长
  526. this.data.totalStudyTime += this.totalTime;
  527. dataManager.save(this.data);
  528. studyTimeDisplay.textContent = dataManager.formatTime(
  529. this.data.totalStudyTime
  530. );
  531. // 重置计时器
  532. this.remainingTime = this.totalTime;
  533. this.timeInput.value = dataManager.formatCountdown(
  534. this.remainingTime
  535. );
  536. this.timeInput.readOnly = false;
  537. this.updateProgress();
  538. this.startBtn.style.display = "inline-block";
  539. this.pauseBtn.style.display = "none";
  540. this.stopBtn.style.display = "none";
  541. // 发送通知
  542. this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
  543. }
  544. updateProgress() {
  545. const progress =
  546. (this.totalTime - this.remainingTime) / this.totalTime;
  547. const offset = this.circumference - progress * this.circumference;
  548. this.progressCircle.style.strokeDashoffset = offset;
  549. }
  550. sendNotification(title, message) {
  551. if ("Notification" in window) {
  552. if (Notification.permission === "granted") {
  553. new Notification(title, { body: message });
  554. } else if (Notification.permission !== "denied") {
  555. Notification.requestPermission().then((permission) => {
  556. if (permission === "granted") {
  557. new Notification(title, { body: message });
  558. }
  559. });
  560. }
  561. }
  562. // 播放声音
  563. const audio = new Audio(
  564. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  565. );
  566. audio.play();
  567. }
  568. }
  569. // 空闲钟控制器
  570. class IdleTimer {
  571. constructor(element) {
  572. this.element = element;
  573. this.timeInput = element.querySelector(".time-input");
  574. this.startBtn = element.querySelector(".start-btn");
  575. this.pauseBtn = element.querySelector(".pause-btn");
  576. this.stopBtn = element.querySelector(".stop-btn");
  577. this.progressBar = element.querySelector(
  578. ".linear-progress .progress"
  579. );
  580. this.data = dataManager.init();
  581. this.totalTime = this.data.idleTime;
  582. this.remainingTime = this.totalTime;
  583. this.isRunning = false;
  584. this.interval = null;
  585. this.init();
  586. }
  587. init() {
  588. this.timeInput.value = dataManager.formatCountdown(
  589. this.remainingTime
  590. );
  591. this.updateProgress();
  592. // 确保初始状态下输入框可编辑
  593. this.timeInput.readOnly = false;
  594. // 事件监听
  595. this.startBtn.addEventListener("click", () => this.start());
  596. this.pauseBtn.addEventListener("click", () => this.pause());
  597. this.stopBtn.addEventListener("click", () => this.stop());
  598. // 允许编辑时间
  599. this.timeInput.addEventListener("blur", () => {
  600. if (!this.isRunning) {
  601. const timeParts = this.timeInput.value.split(":");
  602. if (timeParts.length === 2) {
  603. const minutes = parseInt(timeParts[0], 10);
  604. const seconds = parseInt(timeParts[1], 10);
  605. // 验证输入
  606. if (
  607. !isNaN(minutes) &&
  608. !isNaN(seconds) &&
  609. minutes >= 0 &&
  610. minutes <= 1440 &&
  611. seconds >= 0 &&
  612. seconds <= 59
  613. ) {
  614. this.totalTime = minutes * 60 + seconds;
  615. this.remainingTime = this.totalTime;
  616. this.data.idleTime = this.totalTime;
  617. dataManager.save(this.data);
  618. this.updateProgress();
  619. } else {
  620. // 输入无效,重置为之前的有效值
  621. this.timeInput.value = dataManager.formatCountdown(
  622. this.remainingTime
  623. );
  624. }
  625. } else {
  626. // 格式无效,重置为之前的有效值
  627. this.timeInput.value = dataManager.formatCountdown(
  628. this.remainingTime
  629. );
  630. }
  631. }
  632. });
  633. // 限制输入格式
  634. this.timeInput.addEventListener("input", (e) => {
  635. let value = e.target.value;
  636. // 只允许数字和冒号
  637. value = value.replace(/[^0-9:]/g, "");
  638. // 处理冒号位置
  639. const colonIndex = value.indexOf(":");
  640. if (colonIndex === -1) {
  641. // 没有冒号,自动在倒数第二位前添加冒号
  642. if (value.length >= 3) {
  643. value = value.slice(0, -2) + ":" + value.slice(-2);
  644. }
  645. } else {
  646. // 有冒号,确保冒号后只有两位数字
  647. const parts = value.split(":");
  648. if (parts.length === 2) {
  649. const minutes = parts[0];
  650. let seconds = parts[1];
  651. // 限制秒数为两位
  652. if (seconds.length > 2) {
  653. seconds = seconds.slice(0, 2);
  654. }
  655. value = minutes + ":" + seconds;
  656. }
  657. }
  658. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  659. if (value.length > 7) {
  660. value = value.slice(0, 7);
  661. }
  662. e.target.value = value;
  663. });
  664. }
  665. start() {
  666. if (!this.isRunning) {
  667. this.isRunning = true;
  668. this.timeInput.readOnly = true;
  669. this.startBtn.style.display = "none";
  670. this.pauseBtn.style.display = "inline-block";
  671. this.stopBtn.style.display = "inline-block";
  672. this.interval = setInterval(() => {
  673. this.remainingTime--;
  674. this.timeInput.value = dataManager.formatCountdown(
  675. this.remainingTime
  676. );
  677. this.updateProgress();
  678. if (this.remainingTime <= 0) {
  679. this.complete();
  680. }
  681. }, 1000);
  682. }
  683. }
  684. pause() {
  685. if (this.isRunning) {
  686. this.isRunning = false;
  687. clearInterval(this.interval);
  688. this.startBtn.style.display = "inline-block";
  689. this.pauseBtn.style.display = "none";
  690. }
  691. }
  692. stop() {
  693. this.isRunning = false;
  694. clearInterval(this.interval);
  695. // 重置计时器
  696. this.remainingTime = this.totalTime;
  697. this.timeInput.value = dataManager.formatCountdown(
  698. this.remainingTime
  699. );
  700. this.timeInput.readOnly = false;
  701. this.updateProgress();
  702. this.startBtn.style.display = "inline-block";
  703. this.pauseBtn.style.display = "none";
  704. this.stopBtn.style.display = "none";
  705. }
  706. complete() {
  707. this.isRunning = false;
  708. clearInterval(this.interval);
  709. // 重置计时器
  710. this.remainingTime = this.totalTime;
  711. this.timeInput.value = dataManager.formatCountdown(
  712. this.remainingTime
  713. );
  714. this.timeInput.readOnly = false;
  715. this.updateProgress();
  716. this.startBtn.style.display = "inline-block";
  717. this.pauseBtn.style.display = "none";
  718. this.stopBtn.style.display = "none";
  719. // 发送通知
  720. this.sendNotification("空闲时间结束", "休息时间结束了!");
  721. }
  722. updateProgress() {
  723. const progress =
  724. (this.totalTime - this.remainingTime) / this.totalTime;
  725. this.progressBar.style.width = `${progress * 100}%`;
  726. }
  727. sendNotification(title, message) {
  728. if ("Notification" in window) {
  729. if (Notification.permission === "granted") {
  730. new Notification(title, { body: message });
  731. } else if (Notification.permission !== "denied") {
  732. Notification.requestPermission().then((permission) => {
  733. if (permission === "granted") {
  734. new Notification(title, { body: message });
  735. }
  736. });
  737. }
  738. }
  739. // 播放声音
  740. const audio = new Audio(
  741. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  742. );
  743. audio.play();
  744. }
  745. }
  746. // 初始化应用
  747. const data = dataManager.init();
  748. const studyTimeDisplay = document.querySelector(".study-time-display");
  749. studyTimeDisplay.textContent = dataManager.formatTime(
  750. data.totalStudyTime
  751. );
  752. // 初始化番茄钟
  753. const pomodoroElement = document.querySelector(".pomodoro");
  754. const pomodoroTimer = new PomodoroTimer(pomodoroElement);
  755. // 初始化空闲钟
  756. const idleElement = document.querySelector(".idle");
  757. const idleTimer = new IdleTimer(idleElement);
  758. // 请求通知权限
  759. if ("Notification" in window && Notification.permission === "default") {
  760. Notification.requestPermission();
  761. }
  762. // 处理页面可见性变化
  763. let lastVisibleTime = Date.now();
  764. document.addEventListener("visibilitychange", () => {
  765. if (document.visibilityState === "visible") {
  766. // 页面变为可见时,计算经过的时间并更新计时器
  767. const now = Date.now();
  768. const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
  769. if (pomodoroTimer.isRunning) {
  770. pomodoroTimer.remainingTime = Math.max(
  771. 0,
  772. pomodoroTimer.remainingTime - elapsedSeconds
  773. );
  774. pomodoroTimer.timeInput.value = dataManager.formatCountdown(
  775. pomodoroTimer.remainingTime
  776. );
  777. pomodoroTimer.updateProgress();
  778. if (pomodoroTimer.remainingTime <= 0) {
  779. pomodoroTimer.complete();
  780. }
  781. }
  782. if (idleTimer.isRunning) {
  783. idleTimer.remainingTime = Math.max(
  784. 0,
  785. idleTimer.remainingTime - elapsedSeconds
  786. );
  787. idleTimer.timeInput.value = dataManager.formatCountdown(
  788. idleTimer.remainingTime
  789. );
  790. idleTimer.updateProgress();
  791. if (idleTimer.remainingTime <= 0) {
  792. idleTimer.complete();
  793. }
  794. }
  795. }
  796. lastVisibleTime = Date.now();
  797. });
  798. // 页面关闭前提醒
  799. window.addEventListener("beforeunload", (e) => {
  800. if (pomodoroTimer.isRunning || idleTimer.isRunning) {
  801. e.preventDefault();
  802. e.returnValue = "您有正在运行的计时器,确定要离开吗?";
  803. return e.returnValue;
  804. }
  805. });
  806. </script>
  807. </body>
  808. </html>