Skip to content

Commit cdbc7eb

Browse files
committed
refactor: migrate from jQuery/XHR to native fetch API and DOM methods
1 parent 2c77b38 commit cdbc7eb

File tree

1 file changed

+78
-71
lines changed

1 file changed

+78
-71
lines changed

liaune/bangumi_related_subject_enhance.user.js

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,19 @@ height: 100%;
297297
if (flag) {
298298
collectStatus[ID] = "collect";
299299
avatarNeue.classList.add("relate_collect");
300-
$.post(`/subject/${ID}/interest/update?gh=${securitycode}`, {
301-
status: "collect",
302-
privacy: privacy,
300+
fetch(`/subject/${ID}/interest/update?gh=${securitycode}`, {
301+
method: "POST",
302+
body: new URLSearchParams({
303+
status: "collect",
304+
privacy: privacy,
305+
}),
303306
});
304307
} else {
305308
delete collectStatus[ID];
306309
avatarNeue.classList.remove("relate_collect");
307-
$.post(`/subject/${ID}/remove?gh=${securitycode}`);
310+
fetch(`/subject/${ID}/remove?gh=${securitycode}`, {
311+
method: "POST",
312+
});
308313
}
309314

310315
i++;
@@ -321,11 +326,10 @@ height: 100%;
321326
mangaControlPanel.appendChild(privateLabel);
322327
mangaControlPanel.appendChild(allCollect);
323328

324-
$(mangaControlPanel).insertBefore(
325-
document.querySelectorAll(
326-
"#columnSubjectHomeB .subject_section .clearit"
327-
)[0]
329+
const clearitElement = document.querySelector(
330+
"#columnSubjectHomeB .subject_section .clearit"
328331
);
332+
clearitElement.parentNode.insertBefore(mangaControlPanel, clearitElement);
329333
}
330334

331335
function createElement(type, className, href, textContent) {
@@ -342,23 +346,28 @@ height: 100%;
342346
}
343347

344348
let checkIn = createElement("a", "subCheckIn", "javascript:;");
345-
let flag = (collectStatus[ID] === "collect") ? 1 : 0;
349+
let flag = collectStatus[ID] === "collect" ? 1 : 0;
346350
let avatarNeue = elem.querySelector("span.avatarNeue");
347351
checkIn.addEventListener("click", function () {
348352
flag = flag == 1 ? 0 : 1;
349353
if (flag) {
350354
checkIn.style.backgroundPosition = "bottom left";
351355
collectStatus[ID] = "collect";
352356
avatarNeue.classList.add("relate_collect");
353-
$.post(`/subject/${ID}/interest/update?gh=${securitycode}`, {
354-
status: "collect",
355-
privacy: privacy,
357+
fetch(`/subject/${ID}/interest/update?gh=${securitycode}`, {
358+
method: "POST",
359+
body: new URLSearchParams({
360+
status: "collect",
361+
privacy: privacy,
362+
}),
356363
});
357364
} else {
358365
checkIn.style.backgroundPosition = "top left";
359366
delete collectStatus[ID];
360367
avatarNeue.classList.remove("relate_collect");
361-
$.post(`/subject/${ID}/remove?gh=${securitycode}`);
368+
fetch(`/subject/${ID}/remove?gh=${securitycode}`, {
369+
method: "POST",
370+
});
362371
}
363372
localStorage.setItem(
364373
"bangumi_subject_collectStatus",
@@ -437,7 +446,7 @@ height: 100%;
437446
}
438447

439448
let thisItem = window.location.href.replace(/subject/, "update");
440-
fetch(thisItem, { credentials: "include" })
449+
fetch(thisItem)
441450
.then((data) => {
442451
return new Promise(function (resovle, reject) {
443452
let targetStr = data.text();
@@ -463,7 +472,7 @@ height: 100%;
463472
}
464473

465474
function showCollect(href, elem) {
466-
fetch(href, { credentials: "include" })
475+
fetch(href)
467476
.then((data) => {
468477
return new Promise(function (resovle, reject) {
469478
let targetStr = data.text();
@@ -506,63 +515,61 @@ height: 100%;
506515
}
507516

508517
function showRank(href, elem) {
509-
let xhr = new XMLHttpRequest();
510-
xhr.open("GET", href);
511-
xhr.withCredentials = true;
512-
xhr.responseType = "document";
513-
xhr.send();
514-
xhr.onload = function () {
515-
let d = xhr.responseXML;
516-
let nameinfo = d.querySelector("#infobox li");
517-
let name_cn = nameinfo.innerText.match(/: (\.*)/)
518-
? nameinfo.innerText.match(/: (\.*)/)[1]
519-
: null;
520-
//获取排名
521-
let ranksp = d.querySelector(
522-
"#panelInterestWrapper .global_score small.alarm"
523-
);
524-
let rank = ranksp ? ranksp.innerText.match(/\d+/)[0] : null;
525-
//获取站内评分和评分人数
526-
let score = d.querySelector(
527-
"#panelInterestWrapper .global_score span.number"
528-
).innerText;
529-
let votes = d.querySelector("#ChartWarpper small.grey span").innerText;
530-
//获取好友评分和评分人数
531-
let frdScore = d.querySelector("#panelInterestWrapper .frdScore");
532-
let score_f = frdScore
533-
? frdScore.querySelector("span.num").innerText
534-
: null;
535-
let votes_f = frdScore
536-
? frdScore.querySelector("a.l").innerText.match(/\d+/)[0]
537-
: null;
538-
let score_u = 0;
539-
let info = {
540-
name_cn: name_cn,
541-
rank: rank,
542-
score: score,
543-
votes: votes,
544-
score_f: score_f,
545-
votes_f: votes_f,
546-
score_u: score_u,
547-
};
548-
let ID = href.split("/subject/")[1];
549-
setCache(ID, info);
550-
if (!update) {
551-
displayRank(rank, elem);
552-
} else {
553-
count += 1;
554-
updateBtn.textContent = `更新中... (${count}/${itemsList.length})`;
555-
if (count == itemsList.length) {
556-
updateBtn.textContent = "更新完毕!";
557-
isUpdating = false;
558-
updateBtn.style.opacity = "1";
559-
updateBtn.style.pointerEvents = "auto";
560-
setTimeout(() => {
561-
updateBtn.textContent = "更新排名";
562-
}, 1000);
518+
fetch(href)
519+
.then((response) => response.text())
520+
.then((html) => {
521+
const parser = new DOMParser();
522+
const d = parser.parseFromString(html, "text/html");
523+
let nameinfo = d.querySelector("#infobox li");
524+
let name_cn = nameinfo.innerText.match(/: (\.*)/)
525+
? nameinfo.innerText.match(/: (\.*)/)[1]
526+
: null;
527+
//获取排名
528+
let ranksp = d.querySelector(
529+
"#panelInterestWrapper .global_score small.alarm"
530+
);
531+
let rank = ranksp ? ranksp.innerText.match(/\d+/)[0] : null;
532+
//获取站内评分和评分人数
533+
let score = d.querySelector(
534+
"#panelInterestWrapper .global_score span.number"
535+
).innerText;
536+
let votes = d.querySelector("#ChartWarpper small.grey span").innerText;
537+
//获取好友评分和评分人数
538+
let frdScore = d.querySelector("#panelInterestWrapper .frdScore");
539+
let score_f = frdScore
540+
? frdScore.querySelector("span.num").innerText
541+
: null;
542+
let votes_f = frdScore
543+
? frdScore.querySelector("a.l").innerText.match(/\d+/)[0]
544+
: null;
545+
let score_u = 0;
546+
let info = {
547+
name_cn: name_cn,
548+
rank: rank,
549+
score: score,
550+
votes: votes,
551+
score_f: score_f,
552+
votes_f: votes_f,
553+
score_u: score_u,
554+
};
555+
let ID = href.split("/subject/")[1];
556+
setCache(ID, info);
557+
if (!update) {
558+
displayRank(rank, elem);
559+
} else {
560+
count += 1;
561+
updateBtn.textContent = `更新中... (${count}/${itemsList.length})`;
562+
if (count == itemsList.length) {
563+
updateBtn.textContent = "更新完毕!";
564+
isUpdating = false;
565+
updateBtn.style.opacity = "1";
566+
updateBtn.style.pointerEvents = "auto";
567+
setTimeout(() => {
568+
updateBtn.textContent = "更新排名";
569+
}, 1000);
570+
}
563571
}
564-
}
565-
};
572+
});
566573
}
567574

568575
function displayRank(rank, elem) {

0 commit comments

Comments
 (0)