Skip to content

Commit

Permalink
perf: ⚡ 优化更新提示的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jun 21, 2023
1 parent 545c381 commit e6bbaaa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
21 changes: 11 additions & 10 deletions release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ const exec = (...commands) => {
(async () => {
if (process.argv.slice(2).includes('push')) {
const { version } = packageJSON;

// 打包代码
exec('pnpm build');

// 将打包出来的脚本文件复制到根目录上
shell.cp(
'-f',
path.join(__dirname, './dist/index.js'),
path.join(__dirname, './ComicRead.user.js'),
);

// 提交上传更改
exec(
'git add .',
Expand Down Expand Up @@ -50,14 +61,4 @@ const exec = (...commands) => {

// 将最新的更改日志写入 LatestChange.md
shell.echo(changelog).to('docs/LatestChange.md');

// 打包代码
exec('pnpm build');

// 将打包出来的脚本文件复制到根目录上
shell.cp(
'-f',
path.join(__dirname, './dist/index.js'),
path.join(__dirname, './ComicRead.user.js'),
);
})();
13 changes: 8 additions & 5 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,21 @@ export const buildOptions = (
switch (name) {
case 'main':
return `\`\n${fs
.readFileSync('./dist/cache/main.js')
.readFileSync(resolve(__dirname, 'dist/cache/main.js'))
.toString()
.replaceAll('\\', '\\\\')
.replaceAll('`', '\\`')
.replaceAll('${', '\\${')}\``;

case 'LatestChange':
return `\`\n${fs
.readFileSync(resolve(__dirname, `docs/LatestChange.md`))
.toString()}\`;`;

default:
return fs
.readFileSync(
resolve(__dirname, 'dist/cache/', `${name}.js`),
)
?.toString()
.readFileSync(resolve(__dirname, `dist/cache/${name}.js`))
.toString()
.replaceAll('require$1', 'require');
}
},
Expand Down
1 change: 1 addition & 0 deletions src/components/Toast/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

& ul {
margin: 0;
text-align: left;
}
}

Expand Down
76 changes: 35 additions & 41 deletions src/helper/useInit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useFab } from '../components/useComponents/Fab';
import { toast } from '../components/useComponents/Toast';
import { useSiteOptions } from './useSiteOptions';
import { useSpeedDial } from './useSpeedDial';
import { request } from '.';

/**
* 对所有支持站点页面的初始化操作的封装
Expand Down Expand Up @@ -34,46 +33,41 @@ export const useInit = async <T extends Record<string, any>>(
// 检查脚本的版本变化,提示用户
const version = await GM.getValue<string>('Version');
if (version && version !== GM.info.script.version) {
(async () => {
const res = await request(
`https://cdn.jsdelivr.net/gh/hymbz/ComicReadScript@${GM.info.script.version}/docs/LatestChange.md`,
{ errorText: '' },
);
toast(
() => (
<>
<h2>🥳 ComicRead 已更新到 v{GM.info.script.version}</h2>
<div class="md">
<For each={res.responseText.match(/^### [^[].+?$|^\* .+?$/gm)}>
{(mdText) => {
switch (mdText[0]) {
case '#':
return <h3>{mdText.replace('### ', '')}</h3>;
case '*':
return (
<ul>
<For each={mdText.match(/(?<=:.+?: ).+?(?= \()/)}>
{(item) => <li>{item}</li>}
</For>
</ul>
);
default:
return null;
}
}}
</For>
</div>
</>
),
{
id: 'Version Tip',
type: 'custom',
duration: Infinity,
// 手动点击关掉通知后才不会再次弹出
onDismiss: () => GM.setValue('Version', GM.info.script.version),
},
);
})();
const latestChange = inject('LatestChange');
toast(
() => (
<>
<h2>🥳 ComicRead 已更新到 v{GM.info.script.version}</h2>
<div>
<For each={latestChange.match(/^### [^[].+?$|^\* .+?$/gm)}>
{(mdText) => {
switch (mdText[0]) {
case '#':
return <h3>{mdText.replace('### ', '')}</h3>;
case '*':
return (
<ul>
<For each={mdText.match(/(?<=:.+?: ).+?(?= \()/)}>
{(item) => <li>{item}</li>}
</For>
</ul>
);
default:
return null;
}
}}
</For>
</div>
</>
),
{
id: 'Version Tip',
type: 'custom',
duration: Infinity,
// 手动点击关掉通知后才不会再次弹出
onDismiss: () => GM.setValue('Version', GM.info.script.version),
},
);

// 监听储存的版本数据的变动,如果和当前版本一致就关掉弹窗
// 防止在更新版本后一次性打开多个页面,不得不一个一个关过去
Expand Down

0 comments on commit e6bbaaa

Please sign in to comment.