diff --git a/dist/busuanzi.js b/dist/busuanzi.js index b08afd6..a13289f 100644 --- a/dist/busuanzi.js +++ b/dist/busuanzi.js @@ -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]; + } + }); +}); diff --git a/dist/busuanzi.min.js b/dist/busuanzi.min.js deleted file mode 100644 index 810f564..0000000 --- a/dist/busuanzi.min.js +++ /dev/null @@ -1 +0,0 @@ -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']}}}};; \ No newline at end of file diff --git a/dist/busuanzi.ts b/dist/busuanzi.ts new file mode 100644 index 0000000..1f2e9d0 --- /dev/null +++ b/dist/busuanzi.ts @@ -0,0 +1,31 @@ +let scriptTag: HTMLScriptElement; + +const url = "https://busuanzi.9420.ltd/api?callback=BusuanziCallback"; +const tags:Array = ["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]; + } + }) +}) diff --git a/dist/index.html b/dist/index.html index 01945cf..2757b09 100644 --- a/dist/index.html +++ b/dist/index.html @@ -5,7 +5,6 @@ 不蒜子 - powered by xcsoft - diff --git a/main.go b/main.go index 73022b0..e09d7e2 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/web/api.go b/web/api.go index 9fae7bb..53fa336 100644 --- a/web/api.go +++ b/web/api.go @@ -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",