Change notifyNewVersion to define api

This commit is contained in:
timongh 2022-03-21 19:33:19 +08:00
parent 5678c19868
commit fae9a7eeb3

View File

@ -1,13 +1,12 @@
import { cdnRoots } from '@/core/cdn-types'
import { ComponentMetadata, componentsTags } from '../types'
import { defineComponentMetadata } from '@/components/define'
import { componentsTags } from '../types'
export const component: ComponentMetadata = {
export const component = defineComponentMetadata({
name: 'notifyNewVersion',
displayName: '新版本提示',
description: '定期检查脚本本体的更新, 并弹出提示.',
tags: [
componentsTags.utils,
],
tags: [componentsTags.utils],
options: {
lastUpdateCheck: {
displayName: '最后检查更新日期',
@ -28,19 +27,28 @@ export const component: ComponentMetadata = {
const { getGeneralSettings } = await import('@/core/settings')
const now = Number(new Date())
const duration = now - options.lastUpdateCheck
if (duration < options.minimumDuration) { // 未到间隔期
if (duration < options.minimumDuration) {
// 未到间隔期
return
}
// 本地调试版不检查
if (!GM_info.scriptUpdateURL) {
return
}
const updateUrl = `${cdnRoots[getGeneralSettings().cdnRoot](meta.compilationInfo.branch)}dist/${meta.originalFilename}`
const scriptText: string = await monkey({ url: updateUrl, responseType: 'text' })
const updateUrl = `${cdnRoots[getGeneralSettings().cdnRoot](
meta.compilationInfo.branch,
)}dist/${meta.originalFilename}`
const scriptText: string = await monkey({
url: updateUrl,
responseType: 'text',
})
options.lastUpdateCheck = Number(new Date())
const versionMatch = scriptText.match(/^\/\/ @version\s*([\d\.]+)$/m)
const versionMatch = scriptText.match(/^\/\/ @version\s*([\d.]+)$/m)
if (!versionMatch?.[1]) {
console.warn('[新版本提示] 未能检测出脚本版本, scriptText.length =', scriptText.length)
console.warn(
'[新版本提示] 未能检测出脚本版本, scriptText.length =',
scriptText.length,
)
return
}
const latestVersion = new Version(versionMatch[1])
@ -48,9 +56,12 @@ export const component: ComponentMetadata = {
if (!latestVersion.greaterThan(currentVersion)) {
return
}
Toast.info(/* html */`新版本 <span>${latestVersion.versionString}</span> 已发布. <a href="https://github.com/the1812/Bilibili-Evolved/releases" target="_blank" class="link">更新说明</a><a href="${updateUrl}" target="_blank" class="link">安装</a>`, '检查更新')
Toast.info(
/* html */ `新版本 <span>${latestVersion.versionString}</span> 已发布. <a href='https://github.com/the1812/Bilibili-Evolved/releases' target='_blank' class='link'>更新说明</a><a href='${updateUrl}' target='_blank' class='link'>安装</a>`,
'检查更新',
)
} catch (error) {
console.warn('[新版本提示] 检查更新时发生错误: ', error)
}
},
}
})