From dd2a962264ef76d75bd790593c9aaea50eee47bb Mon Sep 17 00:00:00 2001 From: sup39 Date: Tue, 2 Apr 2024 13:09:16 +0900 Subject: [PATCH] feat: add title and end params for countdown --- LICENSE | 21 +++++++++++++++++++++ README.md | 5 +++++ index.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ index.js | 43 +++++++++++++++++++++++++++++++++++++++++++ jsconfig.json | 7 +++++++ package.json | 8 ++++++++ 6 files changed, 128 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.html create mode 100644 index.js create mode 100644 jsconfig.json create mode 100644 package.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..abb6c4b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2024 sup39 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4899b7f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Countdown +Countdown web page for sup39 + +## Example +https://app.sup39.dev/countdown/?title=TITLE&end=2024-12-31%2012:07 diff --git a/index.html b/index.html new file mode 100644 index 0000000..977c900 --- /dev/null +++ b/index.html @@ -0,0 +1,44 @@ + + + + Countdown | sup39 + + + + + + + + +
+
+
+ + 0 + d + 00 + h + 00 + m + 00 + s +
+
+ + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..36c16c2 --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2024 sup39 + +document.addEventListener('DOMContentLoaded', () => { + const params = new URLSearchParams(window.location.search); + const end = +new Date(params.get('end') ?? +new Date()); + console.log(params.get('end')); + + /** @param {string} selector */ + const queryKnownSelector = selector => + /**@type{HTMLElement}*/(document.querySelector(selector)); + + const title = params.get('title'); + if (title != null) { + queryKnownSelector('#title').textContent = title; + } + + const units = [ + {max: 60, digit: 2, element: queryKnownSelector('#ct-s')}, + {max: 60, digit: 2, element: queryKnownSelector('#ct-m')}, + {max: 24, digit: 2, element: queryKnownSelector('#ct-h')}, + {max: Infinity, digit: 0, element: queryKnownSelector('#ct-d')}, + ]; + const elmSign = queryKnownSelector('#ct-sign'); + let t0 = 0; + function render() { + const t = (end - +new Date()) / 1000 | 0; + if (t !== t0) { + let val = t; + const isMinus = val < 0; + if (isMinus) val = -val; + elmSign.textContent = isMinus ? '-' : ''; + for (const {max, digit, element} of units) { + element.textContent = String(val % max).padStart(digit, '0'); + val = val / max | 0; + } + // next + t0 = t; + } + requestAnimationFrame(render); + } + render(); +}); diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..2f72189 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "lib": ["dom", "es2017"], + "checkJs": true, + "strict": true + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3c22884 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "@sup39/countdown", + "version": "0.1.0", + "description": "Countdown web page for sup39", + "author": "sup39 ", + "license": "MIT", + "private": true +}