Skip to content

Commit de40170

Browse files
committed
重构 ehentai 相关
1 parent 9c8193f commit de40170

14 files changed

Lines changed: 56 additions & 61 deletions

src/helper/useStyle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ const useStyleSheet = (e?: Element) => {
1717
return styleSheet;
1818
};
1919

20+
const addedCss = new Set<string>();
2021
export const useStyle = (css: string | Accessor<string>, e?: Element) => {
2122
const styleSheet = useStyleSheet(e);
22-
if (typeof css === 'string') styleSheet.replaceSync(css);
23-
else
24-
createEffectOn(createRootMemo(css), (style) =>
23+
if (typeof css !== 'string')
24+
return createEffectOn(createRootMemo(css), (style) =>
2525
styleSheet.replaceSync(style),
2626
);
27+
28+
if (addedCss.has(css)) return;
29+
styleSheet.replaceSync(css);
30+
addedCss.add(css);
2731
};
2832

2933
export type StyleMap = {

src/site/ehentai/colorizeTag.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { debounce, getGmValue, hijackFn } from 'helper';
22

33
import { updateMyTags, handleMyTagsChange, type Tag } from './myTags';
4-
import { type EhContext } from './context';
4+
import { type EhContext } from './helper';
55

66
// 为每个标签单独生成 css。用于方便调试时排查和修改样式时使用
77
// const buildTagColorCss = (
@@ -79,10 +79,10 @@ export const updateTagColor = async (tagList: Tag[]) => {
7979
};
8080

8181
/** 标签染色 */
82-
export const colorizeTag = async (context: EhContext) => {
82+
export const colorizeTag = async (contextType: EhContext['type']) => {
8383
handleMyTagsChange.add(updateTagColor);
8484

85-
switch (context.type) {
85+
switch (contextType) {
8686
case 'gallery': {
8787
let css =
8888
location.origin === 'https://exhentai.org'

src/site/ehentai/crossSiteLink.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { For, Show, type Component, type JSX } from 'solid-js';
1111
import { render } from 'solid-js/web';
1212
import { createStore } from 'solid-js/store';
1313

14-
import { type GalleryContext } from './context';
14+
import { type GalleryContext } from './helper';
1515

1616
type ItemData = {
1717
id: string;

src/site/ehentai/detectAd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ReactiveSet } from 'main';
22
import { getAdPageByFileName, getAdPageByContent } from 'userscript/detectAd';
33
import { querySelectorAll, useStyle, createRootMemo } from 'helper';
44

5-
import type { GalleryContext } from './context';
5+
import type { GalleryContext } from './helper';
66

77
/** 识别广告 */
88
export const detectAd = async ({

src/site/ehentai/floatTagList.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import {
1313
hijackFn,
1414
} from 'helper';
1515

16-
import { setEscHandler } from './helper';
17-
import type { GalleryContext } from './context';
16+
import { setEscHandler, type GalleryContext } from './helper';
1817

1918
const getDomPosition = (dom: HTMLElement) => {
2019
const rect = dom.getBoundingClientRect();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ export const getTagNameFull = (tag: string) => {
5858
if (target.includes(namespace)) return `${target[0]}:${name}`;
5959
return tag;
6060
};
61+
62+
export * from './context';

src/site/ehentai/hotkeys.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { linstenKeydown, querySelector } from 'helper';
22

3-
import { setEscHandler } from './helper';
4-
import { type EhContext } from './context';
3+
import { setEscHandler, type EhContext } from './helper';
54

65
/** 快捷键翻页 */
76
export const hotkeysPageTurn = (context: EhContext) => {

src/site/ehentai/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import {
2222
sleep,
2323
} from 'helper';
2424

25-
import { createEhContext } from './context';
26-
import { escHandler } from './helper';
25+
import { escHandler, createEhContext } from './helper';
2726
import { quickFavorite } from './quickFavorite';
2827
import { crossSiteLink } from './crossSiteLink';
2928
import { hotkeysPageTurn } from './hotkeys';
@@ -111,7 +110,7 @@ import { detectAd } from './detectAd';
111110

112111
// 标签染色
113112
if (options.colorize_tag) {
114-
colorizeTag(context);
113+
colorizeTag(context.type);
115114
sortTags(context);
116115
}
117116
// 快捷收藏。必须处于登录状态

src/site/ehentai/quickFavorite.tsx

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { type Accessor, For, createSignal, Show } from 'solid-js';
22
import { render } from 'solid-js/web';
33
import { request, toast } from 'main';
4-
import { t, domParse, querySelector, querySelectorAll } from 'helper';
4+
import { t, domParse, querySelector, querySelectorAll, useStyle } from 'helper';
55

6-
import type { EhContext } from './context';
6+
import type { EhContext } from './helper';
77

8-
let hasStyle = false;
98
const addQuickFavorite = (
109
favoriteButton: HTMLElement,
1110
root: HTMLElement,
1211
apiUrl: string,
13-
position: [number, number],
12+
height: number,
13+
top = 0,
1414
) => {
15-
if (!hasStyle) {
16-
hasStyle = true;
17-
GM_addStyle(`
15+
useStyle(`
1816
.comidread-favorites {
1917
position: absolute;
2018
z-index: 75;
@@ -31,42 +29,40 @@ const addQuickFavorite = (
3129
border-radius: 0;
3230
}
3331
34-
.comidread-favorites-item {
35-
cursor: pointer;
32+
.comidread-favorites-item {
33+
cursor: pointer;
3634
37-
display: flex;
38-
align-items: center;
35+
display: flex;
36+
align-items: center;
3937
40-
width: 100%;
41-
margin: 1em 0;
38+
width: 100%;
39+
margin: 1em 0;
4240
43-
text-align: left;
44-
overflow-wrap: anywhere;
45-
}
41+
text-align: left;
42+
overflow-wrap: anywhere;
43+
}
4644
47-
.comidread-favorites-item > input {
48-
pointer-events: none;
49-
margin: 0 0.5em 0 0;
50-
}
45+
.comidread-favorites-item > input {
46+
pointer-events: none;
47+
margin: 0 0.5em 0 0;
48+
}
5149
52-
.comidread-favorites-item > div {
53-
flex-shrink: 0;
50+
.comidread-favorites-item > div {
51+
flex-shrink: 0;
5452
55-
width: 15px;
56-
height: 15px;
57-
margin: 0 0.5em 0 0;
53+
width: 15px;
54+
height: 15px;
55+
margin: 0 0.5em 0 0;
5856
59-
background-image: url("https://ehgt.org/g/fav.png");
60-
background-repeat: no-repeat;
61-
}
57+
background-image: url("https://ehgt.org/g/fav.png");
58+
background-repeat: no-repeat;
59+
}
6260
6361
.gl1t > .comidread-favorites {
6462
padding: 1em 1.5em;
6563
}
66-
`);
67-
}
64+
`);
6865
root.style.position = 'relative';
69-
root.style.height = '100%';
7066

7167
const [show, setShow] = createSignal(false);
7268

@@ -155,8 +151,8 @@ const addQuickFavorite = (
155151
class="comidread-favorites"
156152
style={{
157153
background,
158-
height: `${position[1] - position[0]}px`,
159-
top: `${position[0]}px`,
154+
height: `${height}px`,
155+
top: `${top}px`,
160156
}}
161157
>
162158
<For
@@ -195,10 +191,8 @@ export const quickFavorite = (context: EhContext) => {
195191
case 'gallery': {
196192
const button = querySelector('#gdf')!;
197193
const root = querySelector('#gd3')!;
198-
addQuickFavorite(button, root, `${unsafeWindow.popbase}addfav`, [
199-
0,
200-
(button.firstElementChild as HTMLElement).offsetTop,
201-
]);
194+
const height = (button.firstElementChild as HTMLElement).offsetTop;
195+
addQuickFavorite(button, root, `${unsafeWindow.popbase}addfav`, height);
202196
break;
203197
}
204198

@@ -212,7 +206,7 @@ export const quickFavorite = (context: EhContext) => {
212206
item.lastElementChild!.getBoundingClientRect().top -
213207
item.getBoundingClientRect().top;
214208
const apiUrl = /http.+?(?=')/.exec(button.getAttribute('onclick')!)![0];
215-
addQuickFavorite(button, item, apiUrl, [top, bottom]);
209+
addQuickFavorite(button, item, apiUrl, bottom - top, top);
216210
}
217211
break;
218212
}
@@ -221,9 +215,9 @@ export const quickFavorite = (context: EhContext) => {
221215
for (const item of querySelectorAll('.gl1e')) {
222216
const button =
223217
item.nextElementSibling!.querySelector<HTMLElement>('[id^=posted_]')!;
224-
const bottom = Number.parseInt(getComputedStyle(item).height, 10);
218+
const height = Number.parseInt(getComputedStyle(item).height, 10);
225219
const apiUrl = /http.+?(?=')/.exec(button.getAttribute('onclick')!)![0];
226-
addQuickFavorite(button, item, apiUrl, [0, bottom]);
220+
addQuickFavorite(button, item, apiUrl, height);
227221
}
228222
break;
229223
}

0 commit comments

Comments
 (0)