Skip to content

Commit

Permalink
fix: 🐛 增加禁漫图片加载方式
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jul 25, 2024
1 parent b0a98e7 commit 2644e5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .xo-config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ module.exports = {
"no-script-url": "off",
// 允许 TODO 注释
"no-warning-comments": "off",
// 允许 return await
"no-return-await": "off",
// 允许调用大写开头的函数
"new-cap": ["error", { "capIsNew": false }],
// 允许使用复杂的数组解构
Expand Down
19 changes: 14 additions & 5 deletions src/site/jm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ import {
return;
}

const downloadImg = async (url: string) => {
try {
// 使用 fetch 可以复用本地缓存,但有时候会报 cors 问题
return await request<Blob>(url, { responseType: 'blob', fetch: true }, 3);
} catch {
return await request<Blob>(
url,
{ responseType: 'blob', revalidate: true, fetch: false },
3,
);
}
};

const getImgUrl = async (imgEle: HTMLImageElement) => {
if (imgEle.src.startsWith('blob:')) return imgEle.src;

const originalUrl = imgEle.src;
const res = await request<Blob>(imgEle.dataset.original!, {
responseType: 'blob',
revalidate: true,
fetch: false,
});
const res = await downloadImg(imgEle.dataset.original!);
if (res.response.size === 0) {
toast.warn(`下载原图时出错: ${imgEle.dataset.page}`);
return '';
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default defineConfig({
test: {
alias: { helper: resolve(__dirname, './src/helper') },
},
define: { isDevMode: false },
});

0 comments on commit 2644e5f

Please sign in to comment.