youtube-po-token-generator/lib/task.js
2024-08-06 10:03:42 +08:00

40 lines
1.4 KiB
JavaScript

const fs = require('fs/promises')
const { JSDOM, VirtualConsole } = require('jsdom')
const { url, userAgent } = require('./consts')
const createTask = async (visitorData) => {
const domContent = await fs.readFile('vendor/index.html', 'utf-8')
const baseContent = await fs.readFile(`vendor/base.js`, 'utf-8')
const baseAppendContent = await fs.readFile(`lib/inject.js`, 'utf-8')
let destroy = undefined
return {
stop: () => destroy?.(),
start: async () => {
while (true) {
const { poToken } = await new Promise(async (res, rej) => {
const { window } = new JSDOM(domContent, {
url,
pretendToBeVisual: true,
runScripts: 'dangerously',
virtualConsole: new VirtualConsole(),
})
Object.defineProperty(window.navigator, 'userAgent', { value: userAgent, writable: false })
window.visitorData = visitorData
window.onPoToken = (poToken) => {
res({ poToken })
}
window.eval(baseContent.replace(/}\s*\)\(_yt_player\);\s*$/, (matched) => `;${baseAppendContent};${matched}`))
destroy = () => {
window.close()
rej(new Error('Window is closed'))
}
}).finally(() => destroy())
if (poToken.length === 160) {
return { poToken }
}
}
},
}
}
module.exports = { createTask }