Skip to content

Commit

Permalink
fix: 🐛 修复 ehentai 标签染色功能只对默认标签集生效的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jul 29, 2024
1 parent a064162 commit 1225fa0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Cotrans 也有自己的油猴脚本 —— 「[Cotrans 漫画/图片翻译器](h

![eh标签染色](/docs/public/eh标签染色.webp)

> 标签颜色数据将在 `进入「My Tags」时``在「My Tags」中修改后` 更新
> 标签颜色数据将在 `功能开启时``进入「My Tags」时``在「My Tags」中修改后` 更新
### 识别广告页

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Cotrans 也有自己的油猴脚本 —— 「[Cotrans 漫画/图片翻译器](h

![eh标签染色](https://comic-read-docs.pages.dev/eh标签染色.webp)

> 标签颜色数据将在 `进入「My Tags」时``在「My Tags」中修改后` 更新
> 标签颜色数据将在 `功能开启时``进入「My Tags」时``在「My Tags」中修改后` 更新
### 识别广告页

Expand Down
53 changes: 39 additions & 14 deletions src/site/ehentai/colorizeTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request } from 'main';
import { domParse, request } from 'main';

import { type PageType } from '.';

Expand All @@ -18,25 +18,50 @@ import { type PageType } from '.';
const buildTagList = (tagList: Set<string>, prefix: string) =>
`\n${[...tagList].map((tag) => `${prefix}${tag}`).join(',\n')}\n`;

const getTagSetHtml = async (tagset?: string) => {
const url = tagset ? `/mytags?tagset=${tagset}` : '/mytags';
const res = await request(url, { fetch: true });
return domParse(res.responseText);
};

/** 获取最新的标签颜色数据 */
export const updateTagColor = async () => {
const res = await request('/mytags', { fetch: true });

const backgroundMap: Record<string, Set<string>> = {};
const borderMap: Record<string, Set<string>> = {};
const colorMap: Record<string, Set<string>> = {};

for (const [, color, border, background, title] of res.responseText.matchAll(
/<div id="tagpreview_\d+.+?color:(.+?);border-color:(.+?);background:(.+?)".+title="(.+?)".+<\/div>/g,
)) {
const tag = title.replaceAll(' ', '_').replaceAll(':', '\\:');

backgroundMap[background] ||= new Set();
backgroundMap[background].add(tag);
borderMap[border] ||= new Set();
borderMap[border].add(tag);
colorMap[color] ||= new Set();
colorMap[color].add(tag);
const tagSetList: Document[] = [];
// 获取所有标签集的 html
const defaultTagSet = await getTagSetHtml();
await Promise.all(
[
...defaultTagSet.querySelectorAll<HTMLOptionElement>(
'#tagset_outer select option',
),
].map(async (option) => {
const tagSet = option.selected
? defaultTagSet
: await getTagSetHtml(option.value);
if (tagSet.querySelector<HTMLInputElement>('#tagset_enable')?.checked)
tagSetList.push(tagSet);
}),
);

for (const html of tagSetList) {
for (const tagDom of html.querySelectorAll<HTMLElement>(
'#usertags_outer [id^=tagpreview_]',
)) {
const { color, borderColor, background } = tagDom.style;
const tag = tagDom.title.replaceAll(' ', '_').replaceAll(':', '\\:');
if (!tag) continue;

backgroundMap[background] ||= new Set();
backgroundMap[background].add(tag);
borderMap[borderColor] ||= new Set();
borderMap[borderColor].add(tag);
colorMap[color] ||= new Set();
colorMap[color].add(tag);
}
}

let css = '';
Expand Down

0 comments on commit 1225fa0

Please sign in to comment.