RenaiApp/src/main/modules/nhentai/nhentai-util.ts

79 lines
2.8 KiB
TypeScript

import type { WebContents } from 'electron';
export const hostname = 'nhentai.net';
export const url = `https://${hostname}/`;
export const paths = {
books: 'g/',
login: 'login/',
favorites: 'favorites/',
};
export const downloadLinkId = 'download';
export const nextFavoritePageSelector = 'a.next';
export const coverLinkSelector = 'a.cover';
export const preTitleSelector = 'h1.title .before';
export const mainTitleSelector = 'h1.title .pretty';
export const postTitleSelector = 'h1.title .after';
export const labeledTagContainerSelector = '.tag-container.field-name';
export const tagSelector = '.tag';
export const tagNameSelector = 'span.name';
export const timeSelector = 'time';
export const tagLabelParodies = 'Parodies';
export const tagLabelCharacters = 'Characters';
export const tagLabelTags = 'Tags';
export const tagLabelArtists = 'Artists';
export const tagLabelGroups = 'Groups';
export const tagLabelLanguages = 'Languages';
export const languageToLangCode = {
japanese: LangCode.JAPANESE,
english: LangCode.ENGLISH,
chinese: LangCode.CHINESE,
};
export type NhentaiRealLanguage = keyof typeof languageToLangCode;
export type NhentaiLanguage = NhentaiRealLanguage | 'translated';
export function isNhentaiLanguage(language: string): language is NhentaiLanguage {
return isNhentaiRealLanguage(language) || language === 'translated';
}
export function isNhentaiRealLanguage(language: string): language is NhentaiRealLanguage {
return Object.keys(languageToLangCode).includes(language);
}
export function pageIsReady(webContents: WebContents): Promise<boolean> {
return webContents.executeJavaScript(`!!document.getElementById('content')`) as Promise<boolean>;
}
export function galleryPageIsReady(webContents: WebContents): Promise<boolean> {
return webContents.executeJavaScript(`!!document.getElementById('${downloadLinkId}')`) as Promise<boolean>;
}
export function favoritePageIsReady(webContents: WebContents): Promise<boolean> {
return webContents.executeJavaScript(`!!document.getElementById('favorites-random-button')`) as Promise<boolean>;
}
export function loginPageIsReady(webContents: WebContents): Promise<boolean> {
return webContents.executeJavaScript(`!!document.getElementById('id_username_or_email')`) as Promise<boolean>;
}
export function getFavoritePageUrl(page?: number): string {
return `${url + paths.favorites}${page ? `?page=${page}` : ''}`;
}
export function getBookUrl(galleryId: string): string {
return `${url}g/${galleryId}/`;
}
export function getGalleryId(bookUrl: string): string {
const regExpExecArray = /https:\/\/nhentai\.net\/g\/(\d+)/.exec(bookUrl);
if (regExpExecArray && regExpExecArray[1]) {
return regExpExecArray[1];
} else {
throw new TypeError(`could not get nhentai gallery if from the book url ${bookUrl}`);
}
}