Skip to content

Commit

Permalink
refactor: ♻️ 减少在 dm5 和 mangabz 上加载图片的请求次数
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jul 15, 2024
1 parent 6fac2d2 commit 623384b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
22 changes: 16 additions & 6 deletions ComicRead.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -10514,9 +10514,14 @@ const main = require('main');
// manhuaren 和 1kkk 的移动端上会直接用一个变量存储所有图片的链接
if (Array.isArray(unsafeWindow.newImgs) && unsafeWindow.newImgs.every(main.isUrl)) return unsafeWindow.newImgs;
return dynamicUpdate(async setImg => {
for (let i = 0; i < imgNum;) {
const newImgs = await getPageImg(i + 1);
for (const url of newImgs) setImg(i++, url);
const imgList = new Set();
while (imgList.size < imgNum) {
// 因为每次会返回指定页数及上一页的图片链接,所以加个1减少请求次数
for (const url of await getPageImg(imgList.size + 1)) {
if (imgList.has(url)) continue;
imgList.add(url);
setImg(imgList.size - 1, url);
}
}
}, imgNum)();
},
Expand Down Expand Up @@ -10583,9 +10588,14 @@ const main = require('main');
getImgList: ({
dynamicUpdate
}) => dynamicUpdate(async setImg => {
for (let i = 0; i < imgNum; i++) {
const newImgs = await getPageImg(i);
for (const url of newImgs) setImg(i, url);
const imgList = new Set();
while (imgList.size < imgNum) {
// 因为每次会返回指定页数及上一页的图片链接,所以加个1减少请求次数
for (const url of await getPageImg(imgList.size + 1)) {
if (imgList.has(url)) continue;
imgList.add(url);
setImg(imgList.size - 1, url);
}
}
}, imgNum)(),
onNext: handlePrevNext('body > .container a[href^="/"]:last-child', '下一'),
Expand Down
22 changes: 16 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,14 @@ try {
return unsafeWindow.newImgs as string[];

return dynamicUpdate(async (setImg) => {
for (let i = 0; i < imgNum; ) {
const newImgs = await getPageImg(i + 1);
for (const url of newImgs) setImg(i++, url);
const imgList = new Set<string>();
while (imgList.size < imgNum) {
// 因为每次会返回指定页数及上一页的图片链接,所以加个1减少请求次数
for (const url of await getPageImg(imgList.size + 1)) {
if (imgList.has(url)) continue;
imgList.add(url);
setImg(imgList.size - 1, url);
}
}
}, imgNum)();
},
Expand Down Expand Up @@ -434,9 +439,14 @@ try {
name: 'mangabz',
getImgList: ({ dynamicUpdate }) =>
dynamicUpdate(async (setImg) => {
for (let i = 0; i < imgNum; i++) {
const newImgs = await getPageImg(i);
for (const url of newImgs) setImg(i, url);
const imgList = new Set<string>();
while (imgList.size < imgNum) {
// 因为每次会返回指定页数及上一页的图片链接,所以加个1减少请求次数
for (const url of await getPageImg(imgList.size + 1)) {
if (imgList.has(url)) continue;
imgList.add(url);
setImg(imgList.size - 1, url);
}
}
}, imgNum)(),
onNext: handlePrevNext(
Expand Down

0 comments on commit 623384b

Please sign in to comment.