113 lines
2.8 KiB
TypeScript
113 lines
2.8 KiB
TypeScript
import type { PlasmoCSConfig } from "plasmo";
|
|
import { postPageMessage, receivePageMessage } from "~utils/pageMessage";
|
|
|
|
export const config: PlasmoCSConfig = {
|
|
run_at: "document_start",
|
|
matches: ["https://www.bilibili.com/bangumi/media/*"],
|
|
world: "MAIN",
|
|
};
|
|
|
|
// 番剧 ID
|
|
let seasonId;
|
|
|
|
// 所有剧集(是个数组)
|
|
// {
|
|
// "aid": 280423481,
|
|
// "badge": "",
|
|
// "badge_info": {
|
|
// "bg_color": "#FB7299",
|
|
// "bg_color_night": "#BB5B76",
|
|
// "text": ""
|
|
// },
|
|
// "badge_type": 0,
|
|
// "cid": 1323548379,
|
|
// "cover": "http://i0.hdslb.com/bfs/archive/5356e34bfc9d1b39ad551a506960cec82c37c80f.png",
|
|
// "from": "bangumi",
|
|
// "id": 781703,
|
|
// "is_premiere": 0,
|
|
// "long_title": "任务26 跟踪父亲和母亲",
|
|
// "share_url": "https://www.bilibili.com/bangumi/play/ep781703",
|
|
// "status": 2,
|
|
// "title": "26",
|
|
// "vid": ""
|
|
// }
|
|
let sections;
|
|
|
|
// 进度对象
|
|
// {
|
|
// "last_ep_id": 810668,
|
|
// "last_ep_index": "35",
|
|
// "last_time": 245
|
|
// }
|
|
let progress;
|
|
|
|
// 进度(对应 section 接口里面所有剧集数组的索引)
|
|
let progressIndex;
|
|
|
|
// 给接口使用的结果
|
|
let result;
|
|
|
|
const open = XMLHttpRequest.prototype.open;
|
|
const send = XMLHttpRequest.prototype.send;
|
|
|
|
Object.defineProperty(XMLHttpRequest.prototype, "open", {
|
|
value: function (...params) {
|
|
this._paul_url = params[1];
|
|
|
|
return open.apply(this, params);
|
|
},
|
|
writable: true
|
|
})
|
|
|
|
Object.defineProperty(XMLHttpRequest.prototype, "send", {
|
|
value: function (...params) {
|
|
if (
|
|
this._paul_url.includes("season/section") ||
|
|
this._paul_url.includes("season/user/status")
|
|
) {
|
|
this.addEventListener("load", function () {
|
|
if (this._paul_url.includes("season/section")) {
|
|
sections = JSON.parse(this.response).result.main_section.episodes;
|
|
}
|
|
else if (this._paul_url.includes("season/user/status")) {
|
|
seasonId = new URL(this._paul_url).searchParams.get("season_id");
|
|
|
|
progress = JSON.parse(this.response).result.progress || { last_ep_id: -1 };
|
|
}
|
|
})
|
|
}
|
|
|
|
return send.apply(this, params);
|
|
},
|
|
writable: true,
|
|
});
|
|
|
|
const checkDataIsHandled = window.setInterval(() => {
|
|
if (sections && progress) {
|
|
if (progress.last_ep_id !== -1) {
|
|
progressIndex = sections.findIndex(
|
|
(item) => item.id === progress.last_ep_id
|
|
);
|
|
|
|
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");
|