Skip to content

Commit

Permalink
fix: use jsonp
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Apr 24, 2022
1 parent ba058cf commit a5e789f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
41 changes: 27 additions & 14 deletions dist/busuanzi.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
let xhr = new XMLHttpRequest();
xhr.open('GET', '//busuanzi.9420.ltd/api', true);
xhr.send(null);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
if (data.success === true) {
document.getElementById("busuanzi_page_pv").innerText = data.data['page_pv'];
document.getElementById("busuanzi_page_uv").innerText = data.data['page_uv'];
document.getElementById("busuanzi_site_pv").innerText = data.data['site_pv'];
document.getElementById("busuanzi_site_uv").innerText = data.data['site_uv'];
var scriptTag;
var url = "https://busuanzi.9420.ltd/api?callback=BusuanziCallback";
var tags = ["site_pv", "site_uv", "page_pv", "page_uv"];
var fetchUrl = function (url, callback) {
var str = "BusuanziCallback_" + Math.floor(1099511627776 * Math.random()).toString();
window[str] = function (callback) {
return function (a) {
try {
callback(a);
scriptTag.parentElement.removeChild(scriptTag);
}
}
}
catch (c) { }
};
}(callback);
scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.defer = true;
scriptTag.src = url.replace("BusuanziCallback", str);
scriptTag.referrerPolicy = "no-referrer-when-downgrade";
document.getElementsByTagName("head")[0].appendChild(scriptTag);
};
fetchUrl(url, function (a) {
tags.map(function (tag) {
var ele = document.getElementById("busuanzi_".concat(tag));
if (ele) {
ele.innerHTML = a[tag];
}
});
});
1 change: 0 additions & 1 deletion dist/busuanzi.min.js

This file was deleted.

31 changes: 31 additions & 0 deletions dist/busuanzi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let scriptTag: HTMLScriptElement;

const url = "https://busuanzi.9420.ltd/api?callback=BusuanziCallback";
const tags:Array<any> = ["site_pv","site_uv", "page_pv", "page_uv"];

const fetchUrl = (url: string, callback: Function) => {
let str: string = "BusuanziCallback_" + Math.floor(1099511627776 * Math.random()).toString();
window[str] = function(callback: Function){
return function(a) {
try {
callback(a);
scriptTag.parentElement.removeChild(scriptTag)
} catch (c) {}
}
}(callback)
scriptTag = document.createElement("script");
scriptTag.type = "text/javascript"
scriptTag.defer = true;
scriptTag.src = url.replace("BusuanziCallback",str);
scriptTag.referrerPolicy = "no-referrer-when-downgrade"
document.getElementsByTagName("head")[0].appendChild(scriptTag)
}

fetchUrl(url, function(a) {
tags.map(tag => {
let ele = document.getElementById(`busuanzi_${tag}`);
if (ele) {
ele.innerHTML = a[tag];
}
})
})
1 change: 0 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<title>不蒜子 - powered by xcsoft</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="基于golang的简易页面访问统计">
<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js" type="application/javascript"></script>
<link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap/5.1.3/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap/5.1.3/js/bootstrap.bundle.min.js" type="application/javascript"></script>
<!-- 不蒜子 -->
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
r.Use(web.AccessControl())

r.LoadHTMLFiles("dist/index.html")
r.StaticFile("/js", "dist/busuanzi.min.js")
r.StaticFile("/js", "dist/busuanzi.js")
// router
r.GET("/", web.Index)
r.GET("/api", web.ApiHandler)
Expand Down
13 changes: 13 additions & 0 deletions web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ func ApiHandler(c *gin.Context) { // test redisHelper
// count
sitePv, siteUv, pagePv, pageUv := core.Count(host, path, ip)

// return jsonp
var query = c.Request.URL.Query()
if query.Get("callback") != "" {
// return jsonp
c.JSONP(200, gin.H{
"site_pv": sitePv,
"site_uv": siteUv,
"page_pv": pagePv,
"page_uv": pageUv,
})
return
}

c.JSON(200, gin.H{
"success": true,
"message": "ok",
Expand Down

0 comments on commit a5e789f

Please sign in to comment.