-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
472 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
(function () { | ||
let url: string = "http://127.0.0.1:8080/jsonp", | ||
tags: string[] = ["site_pv", "site_uv", "page_pv", "page_uv"], | ||
current: HTMLOrSVGScriptElement = document.currentScript, | ||
pjax: boolean = current.hasAttribute("pjax"), | ||
api: string = current.getAttribute("data-api") || url, | ||
prefix: string = current.getAttribute("data-prefix") || "busuanzi", | ||
style: string = current.getAttribute("data-style") || "default"; | ||
|
||
let format = (num: number, style: string = 'default'): string => { | ||
if (style === "comma") return num.toLocaleString(); | ||
if (style === "short") { | ||
const units = ["", "K", "M", "B", "T"]; | ||
let index = Math.floor(Math.log10(num) / 3); | ||
num /= Math.pow(1000, index); | ||
return `${Math.round(num * 100) / 100}${units[index]}`; | ||
} | ||
return num.toString(); | ||
}; | ||
|
||
let bsz_send = () => { | ||
let callbackName = `BszCallback_${Math.round(100000 * Math.random())}_${Date.now().toString().slice(-5)}`; | ||
let script = document.createElement('script'); | ||
script.src = `${api}?callback=${callbackName}`; | ||
script.type = "text/javascript"; | ||
script.defer = !0; | ||
script.referrerPolicy = "no-referrer-when-downgrade"; | ||
|
||
// @ts-ignore | ||
window[callbackName] = (res: { | ||
site_pv: number, | ||
site_uv: number, | ||
page_pv: number, | ||
page_uv: number, | ||
token: string | null, | ||
}) => { | ||
try { | ||
tags.map((tag: string) => { | ||
let element = document.getElementById(`${prefix}_${tag}`); | ||
if (element != null) { // @ts-ignore | ||
element.innerHTML = format(res[tag], style); | ||
} | ||
|
||
let container = document.getElementById(`${prefix}_container_${tag}`); | ||
if (container != null) container.style.display = "inline"; | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} finally { | ||
document.body.removeChild(script); | ||
// @ts-ignore | ||
delete window[callbackName]; | ||
} | ||
}; | ||
|
||
document.body.appendChild(script); | ||
}; | ||
bsz_send(); | ||
|
||
if (!!pjax) { | ||
let history_pushState: Function = window.history.pushState; | ||
window.history.pushState = function () { | ||
history_pushState.apply(this, arguments); | ||
bsz_send(); | ||
}; | ||
|
||
window.addEventListener("popstate", function (_e: PopStateEvent) { | ||
bsz_send(); | ||
}, false); | ||
} | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(function (){ | ||
// 在此处设置您的后端地址 如 https://example.com/api | ||
let url: string = "http://127.0.0.1:8080/api", | ||
tags: string[] = ["site_pv", "site_uv", "page_pv", "page_uv"], | ||
prefix: string = "busuanzi", // 自定义标签ID前缀 | ||
storageName: string = "bsz-id"; // 本地存储名称 | ||
|
||
let bsz_send = () => { | ||
let xhr: XMLHttpRequest = new XMLHttpRequest(); | ||
xhr.open("POST", url, true); | ||
|
||
// set user identity | ||
let token: string | null = localStorage.getItem(storageName); | ||
if (token != null) xhr.setRequestHeader("Authorization", "Bearer " + token); | ||
xhr.setRequestHeader("x-bsz-referer", window.location.href); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 200) { | ||
let res: any = JSON.parse(xhr.responseText); | ||
if (res.success === true) { | ||
tags.map((tag: string) => { | ||
let element = document.getElementById(`${prefix}_${tag}`); | ||
if (element != null) element.innerHTML = res['data'][tag]; | ||
|
||
let container = document.getElementById(`${prefix}_container_${tag}`); | ||
if (container != null) container.style.display = "inline"; | ||
}) | ||
|
||
let setIdentity = xhr.getResponseHeader("Set-Bsz-Identity") | ||
if (setIdentity != null && setIdentity != "") localStorage.setItem(storageName, setIdentity); | ||
} | ||
} | ||
} | ||
} | ||
xhr.send(); | ||
}; | ||
|
||
bsz_send(); | ||
})() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(function () { | ||
// 在此处设置您的后端地址 如 https://example.com/api | ||
let url: string = "http://127.0.0.1:8080/api", | ||
tags: string[] = ["site_pv", "site_uv", "page_pv", "page_uv"], | ||
prefix: string = "busuanzi", // 自定义标签ID前缀 | ||
storageName: string = "bsz-id"; // 本地存储名称 | ||
|
||
let bsz_send = () => { | ||
let xhr: XMLHttpRequest = new XMLHttpRequest(); | ||
xhr.open("POST", url, true); | ||
|
||
// set user identity | ||
let token: string | null = localStorage.getItem(storageName); | ||
if (token != null) xhr.setRequestHeader("Authorization", "Bearer " + token); | ||
xhr.setRequestHeader("x-bsz-referer", window.location.href); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 200) { | ||
let res: any = JSON.parse(xhr.responseText); | ||
if (res.success === true) { | ||
tags.map((tag: string) => { | ||
let element = document.getElementById(`${prefix}_${tag}`); | ||
if (element != null) element.innerHTML = res['data'][tag]; | ||
|
||
let container = document.getElementById(`${prefix}_container_${tag}`); | ||
if (container != null) container.style.display = "inline"; | ||
}) | ||
|
||
let setIdentity = xhr.getResponseHeader("Set-Bsz-Identity") | ||
if (setIdentity != null && setIdentity != "") localStorage.setItem(storageName, setIdentity); | ||
} | ||
} | ||
} | ||
} | ||
xhr.send(); | ||
}; | ||
|
||
bsz_send(); | ||
|
||
let history_pushState: Function = window.history.pushState; | ||
window.history.pushState = function () { | ||
history_pushState.apply(this, arguments); | ||
bsz_send(); | ||
}; | ||
|
||
window.addEventListener("popstate", function (_e: PopStateEvent) { | ||
bsz_send(); | ||
}, false); | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.