From 11f1ed2875b50bbbd26f9190e1042c2904718c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Thu, 18 Apr 2024 01:44:48 +0800 Subject: [PATCH] Inject bilibili bangumi detail page to get progress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 获取追番进度逻辑 --- contents/injectBili.ts | 89 ++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 contents/injectBili.ts diff --git a/contents/injectBili.ts b/contents/injectBili.ts new file mode 100644 index 0000000..0918425 --- /dev/null +++ b/contents/injectBili.ts @@ -0,0 +1,89 @@ +import type { PlasmoCSConfig } from "plasmo"; + +export const config: PlasmoCSConfig = { + run_at: "document_start", + matches: ["https://www.bilibili.com/bangumi/media/*"], + world: "MAIN", +}; + +console.log("尝试获取当前追番进度"); + + +// 所有剧集(是个数组) +// { +// "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; + +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")) { + progress = JSON.parse(this.response).result.progress; + } + }) + } + + return send.apply(this, params); + }, + writable: true, +}); + +const checkDataIsHandled = window.setInterval(() => { + if (sections && progress) { + progressIndex = sections.findIndex( + (item) => item.id === progress.last_ep_id + ); + + console.log("播放进度获取完成", progressIndex); + + window.clearInterval(checkDataIsHandled); + } +}, 1000); diff --git a/package.json b/package.json index dc5a7e0..d9cea45 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "manifest": { "permissions": [ - "tabs", "storage" + "tabs", "storage", "scripting" ], "host_permissions": [ "https://*/*"