Skip to content

Commit

Permalink
feat(script): 修改浏览器下载链接,新增脚本版本更新检测
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Mar 11, 2023
1 parent 1c4523a commit 9265c2b
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions packages/scripts/src/projects/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export const BackgroundProject = Project.create({
oncomplete() {

if (self === top) {
const match = navigator.userAgent.match(/Chrome\/(\d+)/)
const version = match ? parseInt(match[1]) : undefined
const match = navigator.userAgent.match(/Chrome\/(\d+)/);
const version = match ? parseInt(match[1]) : undefined;
if (version) {
// dom.replaceChildren 在 chrome 86 以上版本才能使用
if (version < 86) {
Expand All @@ -184,16 +184,71 @@ export const BackgroundProject = Project.create({
'检测到您当前的浏览器版本过低,可能导致脚本无法运行,请下载/更新以下推荐浏览器:',
[
'- 微软浏览器(Edge) : ',
el('a', { href: 'https://www.microsoft.com/zh-cn/edge', target: '_blank' }, 'https://www.microsoft.com/zh-cn/edge')
el(
'a',
{ href: 'https://www.microsoft.com/zh-cn/edge/download', target: '_blank' },
'https://www.microsoft.com/zh-cn/edge'
)
],
[
'- 谷歌浏览器(Chrome) : ',
el('a', { href: 'https://www.google.com/intl/zh-CN/chrome/', target: '_blank' }, 'https://www.google.com/intl/zh-CN/chrome/')
],
el(
'a',
{ href: 'https://www.google.com/intl/zh-CN/chrome/', target: '_blank' },
'https://www.google.com/intl/zh-CN/chrome/'
)
]
]).outerHTML
})
});
}
}
}
}
}),
update: new Script({
name: '脚本更新检测',
url: [['所有页面', /.*/]],
hideInPanel: true,
namespace: 'background.update',
configs: {
notToday: {
defaultValue: -1
}
},
oncomplete() {
if (self === top) {

if (this.cfg.notToday === -1 || this.cfg.notToday !== new Date().getDate()) {
const infos = $gm.getInfos();
if (infos) {
// 避免阻挡用户操作,这里等页面运行一段时间后再进行更新提示
setTimeout(async () => {
const version: { 'last-version': string; resource: Record<string, string>, notes: string[] } = await request(
'https://cdn.ocsjs.com/ocs-version.json?t=' + Date.now(),
{ method: 'get', type: 'fetch' }
);
if (gt(version['last-version'], infos.script.version)) {
const model = $model('confirm', {
content: $creator.notes([
`检测到新版本发布 ${version['last-version']} :`,
[...(version.notes || [])]
]),
cancelButton: el('button', { className: 'base-style-button-secondary', innerText: '今日不再提示' }, (btn) => {
btn.onclick = () => {
this.cfg.notToday = new Date().getDate()
model?.remove()
}
}),
confirmButton: el('button', { className: 'base-style-button', innerText: '前往更新' }, (btn) => {
btn.onclick = () => {
window.open(version.resource[infos.scriptHandler], '_blank')
model?.remove()
}
})
});
}
}, 10 * 1000);
}
}
}
}
Expand Down

0 comments on commit 9265c2b

Please sign in to comment.