Skip to content

Commit

Permalink
🐛 solve confict
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Nov 29, 2024
2 parents d7bf204 + 4ee2e39 commit de1a684
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 26 deletions.
42 changes: 41 additions & 1 deletion app/controller/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package controller

import (
"encoding/json"
"net/url"

"github.com/gin-gonic/gin"
"github.com/soxft/busuanzi/core"
"net/url"
"github.com/soxft/busuanzi/library/tool"
)

var defaultData = gin.H{
Expand Down Expand Up @@ -132,3 +135,40 @@ func GetHandler(c *gin.Context) {
},
})
}

func JsonpHandler(c *gin.Context) {
c.Header("Content-Type", "application/javascript")

callback := c.Query("callback")
if callback == "" {
c.String(200, "try{console.error(%s)}catch{}", "missing callback parameter")
return
}

u, err := url.Parse(c.GetHeader("Referer"))
if err != nil {
c.String(200, "try{console.error(%s)}catch{}", "unable to parse referer")
return
} else if u.Host == "" {
c.String(200, "try{console.error(%s)}catch{}", "invalid referer")
return
}

var host = u.Host
var path = u.Path
counts := core.Count(c, host, path, tool.Md5(c.ClientIP()+c.Request.UserAgent()))

data := gin.H{
"site_pv": counts.SitePv,
"site_uv": counts.SiteUv,
"page_pv": counts.PagePv,
"page_uv": counts.PageUv,
}
jsonData, err := json.Marshal(data)
if err != nil {
c.String(200, "try{console.error(%s)}catch{}", "gen json failed")
return
}

c.String(200, "try{%s(%s);}catch(e){}", callback, string(jsonData))
}
1 change: 1 addition & 0 deletions dist/busuanzi.jsonp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions dist/busuanzi.jsonp.ts
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);
}
})();
1 change: 1 addition & 0 deletions dist/busuanzi.lite.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions dist/busuanzi.lite.ts
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();
})()
1 change: 1 addition & 0 deletions dist/busuanzi.pjax.lite.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions dist/busuanzi.pjax.lite.ts
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);
})()
1 change: 1 addition & 0 deletions dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "busuanzi",
"description": "",
"dependencies": {
"glob": "^11.0.0",
"ts-loader": "^9.3.0",
"typescript": "^4.6.4",
"webpack": "5.95.0",
Expand Down
Loading

0 comments on commit de1a684

Please sign in to comment.