diff --git a/registry/lib/components/utils/import-series/index.ts b/registry/lib/components/utils/import-series/index.ts index 9c08291f3..bb0307668 100644 --- a/registry/lib/components/utils/import-series/index.ts +++ b/registry/lib/components/utils/import-series/index.ts @@ -10,7 +10,6 @@ export const component = defineComponentMetadata({ displayName: '批量导入合集', tags: [componentsTags.utils], entry: realEntry, - enabledByDefault: true, description: { 'zh-CN': '在合集页面制作一个批量导入按钮,可以新增所有合集内视频至同名收藏夹', }, diff --git a/registry/lib/components/utils/import-series/logic.ts b/registry/lib/components/utils/import-series/logic.ts index 94fb4b846..cbf9c6456 100644 --- a/registry/lib/components/utils/import-series/logic.ts +++ b/registry/lib/components/utils/import-series/logic.ts @@ -1,46 +1,29 @@ -import progress from './progress.vue' -import { mountVueComponent } from '@/core/utils' +import Progress from './Progress.vue' +import { mountVueComponent, getCsrf, delay } from '@/core/utils' +import { sq } from '@/core/spin-query' export const realEntry = async () => { - // 模仿python中的time.sleep - const sleep = timeout => - new Promise(resolve => { - setTimeout(resolve, timeout) - }) - const url = new URL(window.location.href) // 合集id const sid = url.searchParams.get('sid') // 合集作者 const uid = url.pathname.split('/')[1] // 合集名 - let series_name = '' - while (true) { - series_name = document.getElementsByClassName('item cur')[0].innerHTML - if (series_name !== '') { - break - } else { - await sleep(500) - } - } + let seriesName = '' + seriesName = await sq( + () => document.getElementsByClassName('item cur')[0].innerHTML, + t => t !== '', + { + queryInterval: 500, + }, + ) - const get_cookie = name => { - const cookies = document.cookie.split(';') - for (let i = 0; i < cookies.length; i++) { - const cookie = cookies[i].split('=') - cookie[0] = cookie[0].trim() - if (cookie[0] === name) { - return cookie[1] - } - } - return '' - } // 当前登录用户 我 的验证信息 - const csrf = get_cookie('bili_jct') + const csrf = getCsrf() // add a button - const page_head = document.getElementsByClassName('page-head')[0] - const rel = page_head.children[1] + const pageHead = document.getElementsByClassName('page-head')[0] + const rel = pageHead.children[1] const button = document.createElement('a') // 什么离谱class名,play打错了可还行 button.className = 'paly-all-btn' @@ -51,17 +34,17 @@ export const realEntry = async () => { text-align: center; width: 65px; ` - page_head.insertBefore(button, rel) + pageHead.insertBefore(button, rel) button.onclick = async () => { // 获取合集所有视频 - const series_videos = ( + const seriesVideos = ( await fetch( `https://api.bilibili.com/x/series/archives?mid=${uid}&series_id=${sid}&only_normal=true&sort=desc&pn=1&ps=99999`, ).then(r => r.json()) ).data.archives - let fav_id = 0 + let favId = 0 // 创建收藏夹,获取新收藏夹id while (true) { const response = await fetch('https://api.bilibili.com/x/v3/fav/folder/add', { @@ -70,40 +53,40 @@ export const realEntry = async () => { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, - body: `title=${series_name}&csrf=${csrf}&privacy=0`, + body: `title=${seriesName}&csrf=${csrf}&privacy=0`, }).then(r => r.json()) if (response.code === 0) { - fav_id = response.data.id + favId = response.data.id break } else { - await sleep(2000) + await delay(2000) } } // 挂载弹出框 - const mount_point = document.createElement('div') - document.getElementsByTagName('body')[0].appendChild(mount_point) - type progressSetting = Vue & { + const mountPoint = document.createElement('div') + document.getElementsByTagName('body')[0].appendChild(mountPoint) + type ProgressSetting = Vue & { isDisabled: boolean all: number handled: number } - const p: progressSetting = mountVueComponent(progress, mount_point) - p.all = series_videos.length + const p: ProgressSetting = mountVueComponent(Progress, mountPoint) + p.all = seriesVideos.length - for (let i = 0; i < series_videos.length; i++) { + for (let i = 0; i < seriesVideos.length; i++) { // 添加每一个视频至收藏夹 while (true) { // 做个延迟,防止太快而遭服务器拒绝 - await sleep(500) + await delay(500) const response = await fetch('https://api.bilibili.com/x/v3/fav/resource/deal', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, - body: `rid=${series_videos[i].aid}&add_media_ids=${fav_id}&type=2&csrf=${csrf}`, + body: `rid=${seriesVideos[i].aid}&add_media_ids=${favId}&type=2&csrf=${csrf}`, }).then(r => r.json()) if (response.code === 0) { @@ -112,7 +95,7 @@ export const realEntry = async () => { break } else { // 如果请求失败,等待2s后重试 - await sleep(2000) + await delay(2000) } } }