-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathindex.mjs
More file actions
66 lines (57 loc) · 2.21 KB
/
index.mjs
File metadata and controls
66 lines (57 loc) · 2.21 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
import { cropText } from '../../../utils'
import { config } from '../index.mjs'
// This function was written by ChatGPT and modified by iamsirsammy
function replaceHtmlEntities(htmlString) {
const doc = new DOMParser().parseFromString(htmlString.replaceAll('&', '&'), 'text/html')
return doc.documentElement.innerText
}
export default {
init: async (hostname, userConfig, getInput, mountComponent) => {
try {
let oldUrl = location.href
const checkUrlChange = async () => {
if (location.href !== oldUrl) {
oldUrl = location.href
mountComponent(config.youtube)
}
}
window.setInterval(checkUrlChange, 500)
} catch (e) {
/* empty */
}
return true
},
inputQuery: async () => {
try {
const docText = await (
await fetch(location.href, {
credentials: 'include',
})
).text()
const subtitleUrlStartAt = docText.indexOf('https://www.youtube.com/api/timedtext')
if (subtitleUrlStartAt === -1) return
let subtitleUrl = docText.substring(subtitleUrlStartAt)
subtitleUrl = subtitleUrl.substring(0, subtitleUrl.indexOf('"'))
subtitleUrl = subtitleUrl.replaceAll('\\u0026', '&')
let title = docText.substring(docText.indexOf('"title":"') + '"title":"'.length)
title = title.substring(0, title.indexOf('","'))
const subtitleResponse = await fetch(subtitleUrl)
if (!subtitleResponse.ok) return
let subtitleData = await subtitleResponse.text()
let subtitleContent = ''
while (subtitleData.indexOf('">') !== -1) {
subtitleData = subtitleData.substring(subtitleData.indexOf('">') + 2)
subtitleContent += subtitleData.substring(0, subtitleData.indexOf('<')) + ','
}
subtitleContent = replaceHtmlEntities(subtitleContent)
return await cropText(
`You are an expert video summarizer. Create a comprehensive summary of the following YouTube 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)
}
},
}