Skip to content

Commit

Permalink
refactor(web): busuanzi
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Sep 10, 2024
1 parent b68403c commit 6206017
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/core/router.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
39 changes: 14 additions & 25 deletions src/core/utils/busuanzi.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
16 changes: 10 additions & 6 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ <h2 id="page-title">&nbsp;</h2>
</main>
<footer>
<div>
PV: <span id="busuanzi_value_site_pv">N/A</span>
<span>&emsp;&dot;&emsp;</span>
<span>Powered by </span>
<a href="https://github.com/auioc" target="_blank">AUIOC</a>
<span>&emsp;&dot;&emsp;</span>
<span>SitePV:</span>
<span id="busuanzi_value_site_pv">N/A</span>
<span>, SiteUV:</span>
<span id="busuanzi_value_site_uv">N/A</span>
<span>, PagePV:</span>
<span id="busuanzi_value_page_pv">N/A</span>
<span>&emsp;-&emsp;</span>
<a href="https://www.auioc.org" target="_blank">AUIOC.ORG</a>
<span>&emsp;-&emsp;</span>
<a
href="https://github.com/auioc/banciyuan-archive"
target="_blank"
Expand All @@ -43,7 +47,7 @@ <h2 id="page-title">&nbsp;</h2>
<script>
window.REPO = 'auioc/banciyuan-archive';
window.DATA_URL =
window.location.hostname === '127.0.0.1'
window.location.hostname === '127.0.0.2'
? '//127.0.0.1:8080/banciyuan-archive-data'
: 'https://hi.auioc.org/banciyuan-archive-data';
</script>
Expand Down

0 comments on commit 6206017

Please sign in to comment.