1- import { request , type useInit , toast } from 'main' ;
1+ import { request , toast } from 'main' ;
22import { t , querySelector , plimit , hijackFn , querySelectorAll } from 'helper' ;
33import { For , Show , type Component , type JSX } from 'solid-js' ;
44import { render } from 'solid-js/web' ;
55import { createStore } from 'solid-js/store' ;
66
7+ import { type GalleryContext } from './context' ;
8+
79type ItemData = {
810 id : string ;
911 title : string ;
@@ -12,11 +14,11 @@ type ItemData = {
1214 class : string ;
1315} ;
1416
15- const nhentai = async (
16- setComicLoad : AsyncReturnType < typeof useInit > [ 'setComicLoad' ] ,
17- dynamicLoad : AsyncReturnType < typeof useInit > [ 'dynamicLoad' ] ,
18- comicTitle : string ,
19- ) : Promise < ItemData [ ] > => {
17+ const nhentai = async ( {
18+ galleryTitle ,
19+ setComicLoad ,
20+ dynamicLoad ,
21+ } : GalleryContext ) : Promise < ItemData [ ] > => {
2022 interface ComicInfo {
2123 id : number ;
2224 media_id : string ;
@@ -41,7 +43,7 @@ const nhentai = async (
4143 const {
4244 response : { result } ,
4345 } = await request < { result : ComicInfo [ ] } > (
44- `https://nhentai.net/api/galleries/search?query=${ comicTitle } ` ,
46+ `https://nhentai.net/api/galleries/search?query=${ galleryTitle } ` ,
4547 {
4648 responseType : 'json' ,
4749 errorText : t ( 'site.ehentai.nhentai_error' ) ,
@@ -90,29 +92,28 @@ const nhentai = async (
9092 } ;
9193 } ) ;
9294} ;
93- nhentai . errorTip = ( comicTitle : string ) =>
95+ nhentai . errorTip = ( context : GalleryContext ) =>
9496 t ( 'site.ehentai.nhentai_failed' , {
95- nhentai : `<a href='https://nhentai.net/search/?q=${ comicTitle } ' target="_blank"> <u> nhentai </u> </a>` ,
97+ nhentai : `<a href='https://nhentai.net/search/?q=${ context . galleryTitle } ' target="_blank"> <u> nhentai </u> </a>` ,
9698 } ) ;
9799
98- const hitomi = async (
99- setComicLoad : AsyncReturnType < typeof useInit > [ 'setComicLoad' ] ,
100- dynamicLoad : AsyncReturnType < typeof useInit > [ 'dynamicLoad' ] ,
101- ) : Promise < ItemData [ ] > => {
102- const comicId = location . pathname . split ( '/' ) [ 2 ] ;
103-
100+ const hitomi = async ( {
101+ setComicLoad,
102+ dynamicLoad,
103+ galleryId,
104+ } : GalleryContext ) : Promise < ItemData [ ] > => {
104105 const domain = 'gold-usergeneratedcontent.net' ;
105106
106107 const downImg = async ( url : string ) => {
107108 const imgRes = await request < Blob > ( url , {
108- headers : { Referer : `https://hitomi.la/reader/${ comicId } .html` } ,
109+ headers : { Referer : `https://hitomi.la/reader/${ galleryId } .html` } ,
109110 responseType : 'blob' ,
110111 fetch : false ,
111112 } ) ;
112113 return URL . createObjectURL ( imgRes . response ) ;
113114 } ;
114115
115- const res = await request ( `https://ltn.${ domain } /galleries/${ comicId } .js` , {
116+ const res = await request ( `https://ltn.${ domain } /galleries/${ galleryId } .js` , {
116117 errorText : t ( 'site.ehentai.hitomi_error' ) ,
117118 noTip : true ,
118119 } ) ;
@@ -146,8 +147,7 @@ const hitomi = async (
146147 const imageId = gg . s ( hash ) ;
147148 const m = / [ \d a - f ] { 61 } ( [ \d a - f ] { 2 } ) ( [ \d a - f ] ) / . exec ( hash ) ! ;
148149 const g = Number . parseInt ( m [ 2 ] + m [ 1 ] , 16 ) ;
149- const subDomainOffset = gg . m ( g ) + 1 ;
150- return `https://w${ subDomainOffset } .${ domain } /${ gg . b } ${ imageId } /${ hash } .webp` ;
150+ return `https://w${ gg . m ( g ) + 1 } .${ domain } /${ gg . b } ${ imageId } /${ hash } .webp` ;
151151 } ) ;
152152
153153 // 顺序下载避免触发反爬限制
@@ -172,22 +172,11 @@ const hitomi = async (
172172hitomi . errorTip = ( ) => t ( 'site.ehentai.hitomi_error' ) ;
173173
174174/** 关联外站 */
175- export const crossSiteLink = async (
176- dynamicLoad : AsyncReturnType < typeof useInit > [ 'dynamicLoad' ] ,
177- setComicLoad : AsyncReturnType < typeof useInit > [ 'setComicLoad' ] ,
178- LoadButton : Component < { id : string } > ,
179- ) => {
175+ export const crossSiteLink = async ( context : GalleryContext ) => {
180176 /** 只处理「Doujinshi」「Manga」 */
181177 if ( ! querySelector ( '#gdc > .cs:is(.ct2, .ct3)' ) ) return ;
182-
183- const titleDom = document . getElementById ( 'gn' ) ;
184- if ( ! titleDom || ! querySelector ( '#taglist tbody' ) ) {
185- if ( ( document . getElementById ( 'taglist' ) ?. children . length ?? 1 ) > 0 )
186- toast . error ( t ( 'site.ehentai.html_changed_link_failed' ) ) ;
187- return ;
188- }
189-
190- const comicTitle = titleDom . textContent ! . replaceAll ( / \s + - / g, ' ' ) ;
178+ if ( ! context . galleryTitle )
179+ return toast . error ( t ( 'site.ehentai.html_changed_link_failed' ) ) ;
191180
192181 const [ comicMap , setComicMap ] = createStore <
193182 Record < string , ItemData [ ] | string >
@@ -262,7 +251,7 @@ export const crossSiteLink = async (
262251 ( ) => (
263252 < TagMenu >
264253 < a href = { a . href } target = "_blank" innerText = " Jump" />
265- < LoadButton id = { a . id } />
254+ < context . LoadButton id = { a . id } />
266255 </ TagMenu >
267256 ) ,
268257 tagmenu_act_dom ,
@@ -273,17 +262,21 @@ export const crossSiteLink = async (
273262 for ( const getSiteComic of [ hitomi , nhentai ] ) {
274263 setComicMap ( getSiteComic . name , 'searching...' ) ;
275264 try {
276- const itemList = await getSiteComic (
277- setComicLoad ,
278- dynamicLoad ,
279- comicTitle ,
280- ) ;
265+ const itemList = await getSiteComic ( context ) ;
281266 if ( itemList . length > 0 ) setComicMap ( getSiteComic . name , itemList ) ;
282267 else setComicMap ( getSiteComic . name , 'null' ) ;
283268 } catch ( error ) {
284- const errorTip = getSiteComic . errorTip ( comicTitle ) ;
269+ const errorTip = getSiteComic . errorTip ( context ) ;
285270 console . error ( errorTip , error ) ;
286271 setComicMap ( getSiteComic . name , errorTip ) ;
287272 }
288273 }
274+
275+ const { adList } = context . comicMap [ '' ] ;
276+ if ( ! adList ) return ;
277+ // 如果外站源只匹配到了一个漫画,就直接为其加上当前识别出的广告列表
278+ for ( const itemList of Object . values ( comicMap ) ) {
279+ if ( typeof itemList === 'string' ) continue ;
280+ if ( itemList . length === 1 ) context . setComicMap ( itemList [ 0 ] . id , { adList } ) ;
281+ }
289282} ;
0 commit comments