提交
This commit is contained in:
78
public/js/cwd-comments.js
Normal file
78
public/js/cwd-comments.js
Normal file
@@ -0,0 +1,78 @@
|
||||
(function () {
|
||||
/**
|
||||
* cwd-widget(≤0.1.11)里 getLikeStatus 用 post_slug = 完整页面 URL(postUrl),
|
||||
* 而点赞写入 likePage 用的是 postSlug;后端排行榜按 slug 汇总,导致前台一直显示 0。
|
||||
* getPagePv 已写成 postUrl || postSlug,点赞查询未对齐。这里把 GET /api/like 的
|
||||
* post_slug 从绝对地址改回与评论区一致的 slug。
|
||||
*/
|
||||
function patchCwdLikeGetQuery(apiBaseNorm, postSlug) {
|
||||
if (window.__cwdLikeStatusQueryPatched) return;
|
||||
window.__cwdLikeStatusQueryPatched = true;
|
||||
|
||||
var base = apiBaseNorm;
|
||||
var orig = window.fetch;
|
||||
window.fetch = function (input, init) {
|
||||
var url =
|
||||
typeof input === "string"
|
||||
? input
|
||||
: input && typeof input.url === "string"
|
||||
? input.url
|
||||
: "";
|
||||
var method = "GET";
|
||||
if (init && init.method) method = String(init.method).toUpperCase();
|
||||
else if (typeof Request !== "undefined" && input instanceof Request) {
|
||||
method = String(input.method || "GET").toUpperCase();
|
||||
}
|
||||
if (
|
||||
url &&
|
||||
url.indexOf(base) === 0 &&
|
||||
url.indexOf("/api/like?") !== -1 &&
|
||||
method === "GET"
|
||||
) {
|
||||
try {
|
||||
var u = new URL(url);
|
||||
var ps = u.searchParams.get("post_slug") || "";
|
||||
if (ps.indexOf("://") !== -1) {
|
||||
u.searchParams.set("post_slug", postSlug);
|
||||
if (typeof input === "string") {
|
||||
input = u.toString();
|
||||
} else if (typeof Request !== "undefined" && input instanceof Request) {
|
||||
input = new Request(u.toString(), input);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return orig.call(this, input, init);
|
||||
};
|
||||
}
|
||||
|
||||
function mount() {
|
||||
var el = document.getElementById("comments");
|
||||
if (!el || typeof CWDComments === "undefined") return;
|
||||
|
||||
var ds = el.dataset;
|
||||
var apiBaseUrl = ds.apiBase;
|
||||
var postSlug = ds.postSlug;
|
||||
if (!apiBaseUrl || !postSlug) return;
|
||||
|
||||
patchCwdLikeGetQuery(apiBaseUrl.replace(/\/$/, ""), postSlug);
|
||||
|
||||
var opts = {
|
||||
el: "#comments",
|
||||
apiBaseUrl: apiBaseUrl,
|
||||
postSlug: postSlug,
|
||||
lang: ds.lang || "zh-CN"
|
||||
};
|
||||
if (ds.siteId) opts.siteId = ds.siteId;
|
||||
|
||||
new CWDComments(opts).mount();
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", mount);
|
||||
} else {
|
||||
mount();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user