Skip to content

Commit 5516e5f

Browse files
committed
Using counterapi.dev for view count
1 parent c45d388 commit 5516e5f

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

app/main.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ function logThis (report) {
3838
logs.prepend(row);
3939
}
4040

41-
function updateViewCount (type, increment=1) {
41+
async function updateViewCount (type, publicKey) {
4242
const id = `${type}Views`;
43-
let viewCount = parseInt(cache.getItem(id) ?? 0);
44-
viewCount += parseInt(increment);
45-
document.getElementById(id).innerText = viewCount;
46-
cache.setItem(id, viewCount);
43+
document.getElementById(id).innerText = '...';
44+
const formID = `${type}@formonit`;
45+
const url = `https://api.counterapi.dev/v1/${publicKey}/${encodeURIComponent(formID)}/`;
46+
const viewCount = await fetch(url)
47+
.then((response) => response.json())
48+
.then((obj) => obj.count)
49+
.catch((err) => {});
50+
if (viewCount) document.getElementById(id).innerText = viewCount;
4751
}
4852

4953
async function inbox (dataArray, fresh=true) {
@@ -64,11 +68,6 @@ async function inbox (dataArray, fresh=true) {
6468
const origin = data.FormID ?? 'Undefined';
6569
delete data.FormID;
6670

67-
if (origin.startsWith('_view_')) {
68-
updateViewCount(origin.substring('_view_'.length));
69-
continue;
70-
}
71-
7271
const chatID = data.ChatID;
7372
delete data.ChatID;
7473

@@ -291,7 +290,7 @@ function renderForms () {
291290
linkElement.href = url;
292291
qrElement.href = "https://api.qrserver.com/v1/create-qr-code/?size=350x350&data=" + url;
293292
const type = shareableLinks[i].getAttribute('name');
294-
updateViewCount(type, 0);
293+
updateViewCount(type, formActionURL.split('/').pop());
295294
}
296295
};
297296

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ <h3>Forms</h3>
187187
</details>
188188
<details class="my-4" open>
189189
<summary><strong>Shareable links</strong></summary>
190-
<p>Following lists your custom webpages made with &#x2764;&#xfe0f; and hosted by Formonit. Numbers on the right represent page views since your last sign-in.</p>
190+
<p>Following lists your custom webpages made with &#x2764;&#xfe0f; and hosted by Formonit. Numbers on the right represent page views.</p>
191191
<!-- ToDo: https://getbootstrap.com/docs/5.0/components/modal/ and https://stackoverflow.com/a/68077623 -->
192192
<ol class="list-group list-group-numbered">
193193
<li name="anon" class="shareable-link list-group-item d-flex justify-content-between align-items-start">

0 commit comments

Comments
 (0)