Created
September 11, 2024 04:10
-
-
Save yige233/09803324a5bac731d7de71d984406952 to your computer and use it in GitHub Desktop.
手动实现的b站评论区评论带图。需要在浏览器控制台中执行。可以上传任何B站允许上传的图片,并将其附加到评论(一般用户也可以评论带动图)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getCrsfFromCookie() { | |
| return document.cookie.match(/(?<=bili_jct=).*?(?=;)/)?.[0] ?? undefined; | |
| } | |
| function buildFd(data = {}) { | |
| return Object.entries(data).reduce((fd, [key, value]) => { | |
| fd.append(key, value); | |
| return fd; | |
| }, new FormData()); | |
| } | |
| async function selectImageFile() { | |
| const input = document.createElement("input"); | |
| input.type = "file"; | |
| input.style = "display:none;"; | |
| document.body.append(input); | |
| input.click(); | |
| return new Promise((resolve) => input.addEventListener("input", () => resolve(input.files[0]))); | |
| } | |
| async function uploadImage(file, csrf) { | |
| const fd = buildFd({ csrf, file_up: file, category: "daily", biz: "new_dyn" }); | |
| const pic = await fetch("https://api.bilibili.com/x/dynamic/feed/draw/upload_bfs", { | |
| body: fd, | |
| method: "POST", | |
| credentials: "include", | |
| }).then((res) => res.json()); | |
| return pic.data; | |
| } | |
| /** | |
| * 带图评论 | |
| * @param {string} csrf csrf token | |
| * @param {*} uploadResult 图片上传结果 | |
| * @returns | |
| */ | |
| function commentWithPic(csrf, uploadResult) { | |
| const { image_url: img_src, image_width: img_width, image_height: img_height, img_size } = uploadResult; | |
| const pictures = JSON.stringify([{ img_src, img_width, img_height, img_size }]); | |
| /** | |
| * @param {number} commentAreaType 评论区类型。1是视频评论区;11是动态评论区 | |
| * @param {number} commentAreaId 评论区id | |
| * @param {string} commentTextContent 评论内容 | |
| */ | |
| return function (commentAreaType, commentAreaId, commentTextContent) { | |
| const fd = buildFd({ | |
| oid: commentAreaId, | |
| type: commentAreaType, | |
| message: commentTextContent, | |
| plat: 1, | |
| at_name_to_mid: "{}", | |
| pictures, | |
| has_vote_option: true, | |
| gaia_source: "main_web", | |
| }); | |
| fd.append("type", 1); //视频评论区,type是1,动态评论区,type是11 | |
| return fetch(`https://api.bilibili.com/x/v2/reply/add?csrf=${csrf}`, { body: fd, method: "POST", credentials: "include" }); | |
| }; | |
| } | |
| async function main() { | |
| const csrf = getCrsfFromCookie(); | |
| if (!csrf) { | |
| alert("未能从Cookie中获取到csrf"); | |
| return; | |
| } | |
| const image = await selectImageFile(); | |
| const uploadResult = await uploadImage(image, csrf); | |
| const commentWithPicFn = commentWithPic(csrf, uploadResult); | |
| const commentResult = await commentWithPicFn(11, 287130401, "你好"); | |
| console.log(commentResult); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment