-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathindex.mjs
More file actions
70 lines (64 loc) · 2.63 KB
/
index.mjs
File metadata and controls
70 lines (64 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { cropText, waitForElementToExistAndSelect } from '../../../utils'
import { config } from '../index.mjs'
export default {
init: async (hostname, userConfig, getInput, mountComponent) => {
if (location.pathname.includes('/bangumi')) return false
try {
// B站页面是SSR的,如果插入过早,页面 js 检测到实际 Dom 和期望 Dom 不一致,会导致重新渲染
await waitForElementToExistAndSelect('img.bili-avatar-img')
const getVideoPath = () =>
location.pathname + `?p=${new URLSearchParams(location.search).get('p') || 1}`
let oldPath = getVideoPath()
const checkPathChange = async () => {
const newPath = getVideoPath()
if (newPath !== oldPath) {
oldPath = newPath
mountComponent(config.bilibili)
}
}
window.setInterval(checkPathChange, 500)
} catch (e) {
/* empty */
}
return true
},
inputQuery: async () => {
try {
const bvid = location.pathname.replace('video', '').replaceAll('/', '')
const p = Number(new URLSearchParams(location.search).get('p') || 1) - 1
const pagelistResponse = await fetch(
`https://api.bilibili.com/x/player/pagelist?bvid=${bvid}`,
)
const pagelistData = await pagelistResponse.json()
const videoList = pagelistData.data
const cid = videoList[p].cid
const title = videoList[p].part
const infoResponse = await fetch(
`https://api.bilibili.com/x/player/v2?bvid=${bvid}&cid=${cid}`,
{
credentials: 'include',
},
)
const infoData = await infoResponse.json()
let subtitleUrl = infoData.data.subtitle.subtitles[0].subtitle_url
if (subtitleUrl.startsWith('//')) subtitleUrl = 'https:' + subtitleUrl
else if (!subtitleUrl.startsWith('http')) subtitleUrl = 'https://' + subtitleUrl
const subtitleResponse = await fetch(subtitleUrl)
const subtitleData = await subtitleResponse.json()
const subtitles = subtitleData.body
let subtitleContent = ''
for (let i = 0; i < subtitles.length; i++) {
if (i === subtitles.length - 1) subtitleContent += subtitles[i].content
else subtitleContent += subtitles[i].content + ','
}
return await cropText(
`You are an expert video summarizer. Create a comprehensive summary of the following Bilibili video in markdown format, ` +
`highlighting key takeaways, crucial information, and main topics. Include the video title.\n` +
`Video Title: "${title}"\n` +
`Subtitle content:\n${subtitleContent}`,
)
} catch (e) {
console.log(e)
}
},
}