From 620601750074ba2a113fbfc70e5861e5797efafc Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Wed, 11 Sep 2024 00:12:56 +0800 Subject: [PATCH] refactor(web): busuanzi --- src/core/router.ts | 2 +- src/core/utils/busuanzi.ts | 39 ++++++++++++++------------------------ src/index.html | 16 ++++++++++------ 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/core/router.ts b/src/core/router.ts index 2e06d1a..6390394 100644 --- a/src/core/router.ts +++ b/src/core/router.ts @@ -1,7 +1,7 @@ +import busuanzi from 'utils/busuanzi'; import { getDetail, getIndex, getReadme } from './data/data'; import { EVENT_TARGET, LoadDetailEvent, LoadIndexEvent } from './event/events'; import { TYPE } from './types'; -import { busuanzi } from './utils/busuanzi'; import { abortFetch, onFetchError } from './utils/fetch'; import { hashpath, loadPage } from './utils/utils'; diff --git a/src/core/utils/busuanzi.ts b/src/core/utils/busuanzi.ts index a239a5b..f4e9405 100644 --- a/src/core/utils/busuanzi.ts +++ b/src/core/utils/busuanzi.ts @@ -1,34 +1,23 @@ -const jsonp = '//busuanzi.ibruce.info/busuanzi?jsonpCallback='; +const busuanzi_url = 'https://events.vercount.one/log'; + const prefix = 'busuanzi_value_'; const keys = ['site_pv', 'page_pv', 'site_uv'] as const; -type BusuanziData = { +type Data = { readonly [k in (typeof keys)[number]]: number; -} & { readonly version: number }; +}; -export function busuanzi() { - if (window.location.hostname === '127.0.0.1') { - return; - } - const callbackId = Math.floor(1099511627776 * Math.random()); - const callbackName = 'BusuanziCallback_' + callbackId; - const script = document.createElement('script'); - script.id = callbackName; - script.defer = true; - script.src = jsonp + callbackName; - script.referrerPolicy = 'no-referrer-when-downgrade'; - document.getElementsByTagName('head')[0].appendChild(script); - //@ts-expect-error - window[callbackName] = async (data: BusuanziData) => { - callback(data); - document.getElementById(callbackName).remove(); - //@ts-expect-error - delete window[callbackName]; - }; -} +export default async function busuanzi() { + const page = window.location.href.replace('#', 'H').replace('?', '/'); + const data: Data = await fetch(busuanzi_url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ url: page }), + }).then((res) => res.json()); -async function callback(data: BusuanziData) { - console.debug(data); + console.debug('busuanzi', data); for (const key of keys) { const element = document.getElementById(prefix + key); if (element) { diff --git a/src/index.html b/src/index.html index 638ca73..e6bf03c 100644 --- a/src/index.html +++ b/src/index.html @@ -28,11 +28,15 @@