mirror of
https://github.com/YouHaveTrouble/youhavetrouble.github.io.git
synced 2026-05-11 22:06:56 +00:00
add classic styled view counter
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
<div class="view-counter">
|
||||||
|
<div data-viewcount>
|
||||||
|
<span>?</span>
|
||||||
|
</div>
|
||||||
|
<div>unique visitors</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style lang="scss" is:global>
|
||||||
|
.view-counter {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
padding-block: 1rem;
|
||||||
|
width: max-content;
|
||||||
|
text-align: center;
|
||||||
|
|
||||||
|
[data-viewcount] {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
line-height: 1;
|
||||||
|
gap: 0.2rem;
|
||||||
|
padding: 0.25rem;
|
||||||
|
font-size: 1.25rem;
|
||||||
|
span {
|
||||||
|
display: inline-block;
|
||||||
|
background: black;
|
||||||
|
color: white;
|
||||||
|
padding: 0.5ch;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
updateViewCount();
|
||||||
|
|
||||||
|
async function updateViewCount() {
|
||||||
|
const viewCounter = document.querySelector("[data-viewcount]");
|
||||||
|
if (!viewCounter) return;
|
||||||
|
const result = await fetch("https://youhavetrouble-view_counter.web.val.run").catch(() => null);
|
||||||
|
if (result === null) return;
|
||||||
|
const json = await result.json();
|
||||||
|
const rawCount = json.count;
|
||||||
|
const chars = rawCount.toString();
|
||||||
|
viewCounter.innerHTML = "";
|
||||||
|
for (let i = 0; i < chars.length; i++) {
|
||||||
|
const span = document.createElement("span");
|
||||||
|
span.textContent = chars[i];
|
||||||
|
viewCounter.appendChild(span);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../layouts/BaseLayout.astro';
|
import BaseLayout from '../layouts/BaseLayout.astro';
|
||||||
|
import ViewCounter from "../components/ViewCounter.astro";
|
||||||
|
|
||||||
const title = 'Home';
|
const title = 'Home';
|
||||||
const description = 'My little corner of the internet.';
|
const description = 'My little corner of the internet.';
|
||||||
@@ -16,6 +17,9 @@ const permalink = Astro?.site?.href ?? '/';
|
|||||||
<span>My online status:</span>
|
<span>My online status:</span>
|
||||||
<span id="online-status">Unknown</span><span id="online-game"></span>
|
<span id="online-status">Unknown</span><span id="online-game"></span>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="wrap">
|
||||||
|
<ViewCounter />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="hero-socials">
|
<div class="hero-socials">
|
||||||
@@ -66,9 +70,7 @@ const permalink = Astro?.site?.href ?? '/';
|
|||||||
<script>
|
<script>
|
||||||
let task = -1;
|
let task = -1;
|
||||||
|
|
||||||
document.addEventListener('astro:page-load', () => {
|
updateOnlineStatus();
|
||||||
updateOnlineStatus();
|
|
||||||
});
|
|
||||||
|
|
||||||
async function updateOnlineStatus() {
|
async function updateOnlineStatus() {
|
||||||
clearTimeout(task);
|
clearTimeout(task);
|
||||||
@@ -161,10 +163,11 @@ const permalink = Astro?.site?.href ?? '/';
|
|||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
border-width: 1px;
|
border-width: 1px;
|
||||||
border-style: solid;
|
border-style: solid;
|
||||||
border-color: transparent;
|
border-color: rgba(255, 255, 255, 0);
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
gap: 0.25rem;
|
gap: 0.25rem;
|
||||||
border-radius: 0.25rem;
|
border-radius: 0.25rem;
|
||||||
|
transition: border-color 0.2s, color 0.2s;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
color: var(--text-main);
|
color: var(--text-main);
|
||||||
|
|||||||
Reference in New Issue
Block a user