Insert SyncButton on bilibili bangumi detail page to update progress

插入一个手动同步的按钮,以提交追番进度到服务器
This commit is contained in:
2024-04-21 23:34:23 +08:00
parent 11f1ed2875
commit ef26533127
7 changed files with 199 additions and 14 deletions
+30 -7
View File
@@ -1,4 +1,5 @@
import type { PlasmoCSConfig } from "plasmo";
import { postPageMessage, receivePageMessage } from "~utils/pageMessage";
export const config: PlasmoCSConfig = {
run_at: "document_start",
@@ -6,8 +7,8 @@ export const config: PlasmoCSConfig = {
world: "MAIN",
};
console.log("尝试获取当前追番进度");
// 番剧 ID
let seasonId;
// 所有剧集(是个数组)
// {
@@ -43,6 +44,9 @@ let progress;
// 进度(对应 section 接口里面所有剧集数组的索引)
let progressIndex;
// 给接口使用的结果
let result;
const open = XMLHttpRequest.prototype.open;
const send = XMLHttpRequest.prototype.send;
@@ -66,7 +70,9 @@ Object.defineProperty(XMLHttpRequest.prototype, "send", {
sections = JSON.parse(this.response).result.main_section.episodes;
}
else if (this._paul_url.includes("season/user/status")) {
progress = JSON.parse(this.response).result.progress;
seasonId = new URL(this._paul_url).searchParams.get("season_id");
progress = JSON.parse(this.response).result.progress || { last_ep_id: -1 };
}
})
}
@@ -78,12 +84,29 @@ Object.defineProperty(XMLHttpRequest.prototype, "send", {
const checkDataIsHandled = window.setInterval(() => {
if (sections && progress) {
progressIndex = sections.findIndex(
(item) => item.id === progress.last_ep_id
);
if (progress.last_ep_id !== -1) {
progressIndex = sections.findIndex(
(item) => item.id === progress.last_ep_id
);
console.log("播放进度获取完成", progressIndex);
console.log("Toolbox: 追番进度获取完成", progressIndex);
result = {
id: seasonId,
seem: progressIndex,
name: document.querySelector<HTMLSpanElement>(".media-info-title-t").innerText,
};
} else {
console.log("Toolbox: 追番进度获取失败,可能从未观看");
}
window.clearInterval(checkDataIsHandled);
}
}, 1000);
receivePageMessage(() => {
postPageMessage({
type: "toolbox::sendBiliProgress",
value: result,
});
}, "toolbox::getBiliProgress");