Skip to content

Commit

Permalink
refactor: ♻️ 改为内联正则
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed May 12, 2024
1 parent d4dad99 commit 6d807b9
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/helper/detectAd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,13 @@ export const getAdPageByContent = async (
return getAdPage(imgList, byContent(qrEngine, canvas), adList);
};

const adFileNameRe = /^[zZ]+/;

/** 通过文件名判断是否是广告 */
export const getAdPageByFileName = async (
fileNameList: Array<string | undefined>,
adList = new Set<number>(),
) =>
getAdPage(
fileNameList,
(fileName: string) => adFileNameRe.test(fileName),
(fileName: string) => /^[zZ]+/.test(fileName),
adList,
);
4 changes: 1 addition & 3 deletions src/helper/eleSelector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const hasNumRe = /\d/;

const getTagText = (ele: HTMLElement) => {
let text = ele.nodeName;
if (ele.id && !hasNumRe.test(ele.id)) text += `#${ele.id}`;
if (ele.id && !/\d/.test(ele.id)) text += `#${ele.id}`;
return text;
};

Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,12 @@ try {
);
};

const urlMatchRe = /comic\/\d+\/chapter\/\d+\/images\//;

options = {
name: 'komiic',
getImgList,
SPA: {
isMangaPage: () => urlMatchRe.test(window.location.href),
isMangaPage: () =>
/comic\/\d+\/chapter\/\d+\/images\//.test(window.location.href),
getOnPrev: handlePrevNext('上一'),
getOnNext: handlePrevNext('下一'),
},
Expand Down
3 changes: 1 addition & 2 deletions src/pwa/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export const zipExtension = new Set<ZipExtension>([
'.cb7',
]);

const extensionRe = /\.[^.]+$/;
/** 根据文件名判断文件是否受支持 */
export const isSupportFile = (name: string) => {
const extension = extensionRe.exec(name)?.[0];
const extension = /\.[^.]+$/.exec(name)?.[0];
if (!extension) return null;
if (zipExtension.has(extension as any)) return extension as ZipExtension;
if (imgExtension.has(extension)) return 'img';
Expand Down
7 changes: 2 additions & 5 deletions src/site/dmzj.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ import { getComicId, getViewpoint, useComicDetail } from '../helper/dmzjApi';
return { comicId, chapterId };
};

const isListPageRe = /^\/[^/]*?\/?$/;
const isMangaPageRe = /^\/.*?\/\d+\.shtml$/;

const handleListPage = async () => {
await waitDom('.newpl_ans');
// 判断漫画被禁
Expand Down Expand Up @@ -165,8 +162,8 @@ import { getComicId, getViewpoint, useComicDetail } from '../helper/dmzjApi';
};

const isMangaPage = async () => {
if (isListPageRe.test(window.location.pathname)) return handleListPage();
return isMangaPageRe.test(window.location.pathname);
if (/^\/[^/]*?\/?$/.test(window.location.pathname)) return handleListPage();
return /^\/.*?\/\d+\.shtml$/.test(window.location.pathname);
};

await universalInit({
Expand Down
4 changes: 1 addition & 3 deletions src/site/dmzj_www.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { log, querySelector, toast, useInit, waitDom } from 'main';

import { getChapterInfo } from '../helper/dmzjApi';

const chapterIdRe = /(?<=\/)\d+(?=\.html)/;

const turnPage = (chapterId?: number) => {
if (!chapterId) return undefined;

Expand All @@ -19,7 +17,7 @@ const turnPage = (chapterId?: number) => {
await waitDom('.head_wz');
// 只在漫画页内运行
const comicId = querySelector('.head_wz [id]')?.id;
const chapterId = chapterIdRe.exec(window.location.pathname)?.[0];
const chapterId = /(?<=\/)\d+(?=\.html)/.exec(window.location.pathname)?.[0];

if (!comicId || !chapterId) return;

Expand Down

0 comments on commit 6d807b9

Please sign in to comment.