// ==UserScript== // @name ComicRead(改) // @namespace ComicRead // @version 12.1.0 // @description 为漫画站增加双页阅读、翻译等优化体验的增强功能。百合会(记录阅读历史、自动签到等)、百合会新站、动漫之家(解锁隐藏漫画)、E-Hentai(关联外站、快捷收藏、标签染色、识别广告页等)、nhentai(彻底屏蔽漫画、无限滚动)、Yurifans(自动签到)、拷贝漫画(copymanga)(显示最后阅读记录、解锁隐藏漫画)、Pixiv、再漫画、明日方舟泰拉记事社、禁漫天堂、漫画柜(manhuagui)、动漫屋(dm5)、绅士漫画(wnacg)、mangabz、komiic、MangaDex、NoyAcg、無限動漫、熱辣漫畫、hitomi、SchaleNetwork、kemono、nekohouse、welovemanga、HentaiZap、最前線、Tachidesk // @description:en Add enhanced features to the comic site for optimized experience, including dual-page reading and translation. E-Hentai (Associate nhentai, Quick favorite, Colorize tags, Floating tag list, etc.) | nhentai (Totally block comics, Auto page turning) | hitomi | Anchira | kemono | nekohouse | welovemanga. // @description:ru Добавляет расширенные функции для удобства на сайт, такие как двухстраничный режим и перевод. // @author hymbz // @license AGPL-3.0-or-later // @noframes // @match *://*/* // @connect yamibo.com // @connect dmzj.com // @connect idmzj.com // @connect exhentai.org // @connect e-hentai.org // @connect hath.network // @connect nhentai.net // @connect gold-usergeneratedcontent.net // @connect hypergryph.com // @connect mangabz.com // @connect copymanga.site // @connect copymanga.info // @connect copymanga.net // @connect copymanga.org // @connect copymanga.tv // @connect copy20.com // @connect mangacopy.com // @connect xsskc.com // @connect schale.network // @connect touhou.ai // @connect jsdelivr.net // @connect npmmirror.com // @connect self // @connect 127.0.0.1 // @connect * // @grant GM_addElement // @grant GM_getResourceText // @grant GM_xmlhttpRequest // @grant GM.addValueChangeListener // @grant GM.removeValueChangeListener // @grant GM.getResourceText // @grant GM.getValue // @grant GM.setValue // @grant GM.listValues // @grant GM.deleteValue // @grant GM.registerMenuCommand // @grant GM.unregisterMenuCommand // @grant unsafeWindow // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAACBUExURUxpcWB9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i2B9i////198il17idng49DY3PT297/K0MTP1M3X27rHzaCxupmstbTByK69xOfr7bfFy3WOmqi4wPz9/X+XomSBjqW1vZOmsN/l6GmFkomeqe7x8vn6+kv+1vUAAAAOdFJOUwDsAoYli9zV+lIqAZEDwV05SQAAAUZJREFUOMuFk+eWgjAUhGPBiLohjZACUqTp+z/gJkqJy4rzg3Nn+MjhwB0AANjv4BEtdITBHjhtQ4g+CIZbC4Qb9FGb0J4P0YrgCezQqgIA14EDGN8fYz+f3BGMASFkTJ+GDAYMUSONzrFL7SVvjNQIz4B9VERRmV0rbJWbrIwidnsd6ACMlEoip3uad3X2HJmqb3gCkkJELwk5DExRDxA6HnKaDEPSsBnAsZoANgJaoAkg12IJqBiPACImXQKF9IDULIHUkOk7kDpeAMykHqCEWACy8ACdSM7LGSg5F3HtAU1rrkaK9uGAshXS2lZ5QH/nVhmlD8rKlmbO3ZsZwLe8qnpdxJRnLaci1X1V5R32fjd5CndVkfYdGpy3D+htU952C/ypzPtdt3JflzZYBy7fi/O1euvl/XH1Pp+Cw3/1P1xOZwB+AWMcP/iw0AlKAAAAV3pUWHRSYXcgcHJvZmlsZSB0eXBlIGlwdGMAAHic4/IMCHFWKCjKT8vMSeVSAAMjCy5jCxMjE0uTFAMTIESANMNkAyOzVCDL2NTIxMzEHMQHy4BIoEouAOoXEXTyQjWVAAAAAElFTkSuQmCC // @resource solid-js https://registry.npmmirror.com/solid-js/1.9.8/files/dist/solid.cjs // @resource fflate https://registry.npmmirror.com/fflate/0.8.2/files/umd/index.js // @resource jsqr https://registry.npmmirror.com/jsqr/1.4.0/files/dist/jsQR.js // @resource comlink https://registry.npmmirror.com/comlink/4.4.2/files/dist/umd/comlink.min.js // @resource dmzjDecrypt https://update.sleazyfork.org/scripts/467177/1207199/dmzjDecrypt.js // @resource solid-js|store https://registry.npmmirror.com/solid-js/1.9.8/files/store/dist/store.cjs // @resource solid-js|web https://registry.npmmirror.com/solid-js/1.9.8/files/web/dist/web.cjs // @resource _tensorflow|tfjs https://registry.npmmirror.com/@tensorflow/tfjs/4.22.0/files/dist/tf.min.js // @resource _tensorflow|tfjs-backend-webgpu https://registry.npmmirror.com/@tensorflow/tfjs-backend-webgpu/4.22.0/files/dist/tf-backend-webgpu.js // @supportURL https://github.com/hymbz/ComicReadScript/issues // ==/UserScript== let supportWorker = typeof Worker !== 'undefined'; const gmApi = { GM, GM_addElement: typeof GM_addElement === 'undefined' ? undefined : GM_addElement, GM_getResourceText, GM_xmlhttpRequest, unsafeWindow }; const gmApiList = Object.keys(gmApi); const crsLib = { // 有些 cjs 模块会检查这个,所以在这里声明下 process: { env: { NODE_ENV: 'production' } }, ...gmApi }; const tempName = Math.random().toString(36).slice(2); const getResource = name => { const text = GM_getResourceText(name.replaceAll('/', '|').replaceAll('@', '_')); if (!text) throw new Error(`外部模块 ${name} 未在 @Resource 中声明`); if (name === '@tensorflow/tfjs-backend-webgpu') return text.replace('@tensorflow/tfjs-core', '@tensorflow/tfjs'); return text; }; const evalCode = code => { if (!code) return; // 因为部分网站会对 eval 进行限制,比如推特(CSP)、hitomi(代理 window.eval 进行拦截) // 所以优先使用最通用的 GM_addElement 来加载 if (gmApi.GM_addElement) return GM_addElement('script', { textContent: code })?.remove(); eval.call(unsafeWindow, code); }; /** * 通过 Resource 导入外部模块 * @param name \@resource 引用的资源名 */ const selfImportSync = name => { let code; // 为了方便打包、减少在无关站点上的运行损耗、顺带隔离下作用域 // 除站点逻辑外的代码会作为字符串存着,要用时再像外部模块一样导入 switch (name) { case 'helper/languages': code =` const langList = ['zh', 'en', 'ru']; /** 判断传入的字符串是否是支持的语言类型代码 */ const isLanguages = lang => Boolean(lang) && langList.includes(lang); /** 返回浏览器偏好语言 */ const getBrowserLang = () => { for (const language of navigator.languages) { const matchLang = langList.find(l => l === language.split('-')[0]); if (matchLang) return matchLang; } }; const getSaveLang = () => typeof GM === 'undefined' ? localStorage.getItem('@Languages') : GM.getValue('@Languages'); const setSaveLang = val => typeof GM === 'undefined' ? localStorage.setItem('@Languages', val) : GM.setValue('@Languages', val); const getInitLang = async () => { const saveLang = await getSaveLang(); if (isLanguages(saveLang)) return saveLang; const lang = getBrowserLang() ?? 'zh'; setSaveLang(lang); return lang; }; exports.getInitLang = getInitLang; exports.isLanguages = isLanguages; exports.langList = langList; exports.setSaveLang = setSaveLang; ` break; case 'helper': code =` const web = require('solid-js/web'); const solidJs = require('solid-js'); const languages = require('helper/languages'); const store = require('solid-js/store'); const getDom = id => { let dom = document.getElementById(id); if (dom) { dom.innerHTML = ''; return dom; } dom = document.createElement('div'); dom.id = id; document.body.append(dom); return dom; }; /** 挂载 solid-js 组件 */ const mountComponents = (id, fc) => { const dom = getDom(id); dom.style.setProperty('display', 'unset', 'important'); const shadowDom = dom.attachShadow({ mode: 'closed' }); web.render(fc, shadowDom); return dom; }; class FaviconProgress { constructor(color = '#607D8B') { this.color = color; this.canvas = document.createElement('canvas'); this.canvas.width = 32; this.canvas.height = 32; this.ctx = this.canvas.getContext('2d'); const existingLink = document.querySelector("link[rel~='icon']"); if (existingLink) this.link = existingLink;else { const link = document.createElement('link'); link.type = 'image/x-icon'; link.rel = 'icon'; document.head.append(link); this.link = link; } this.initLink = this.link.href || '/favicon.ico'; } update(progress) { this.ctx.clearRect(0, 0, 32, 32); // 绘制背景 this.ctx.beginPath(); this.ctx.arc(16, 16, 16, 0, Math.PI * 2); this.ctx.fillStyle = '#FAFAFA'; this.ctx.fill(); // 绘制进度扇形 const startAngle = -Math.PI / 2; const endAngle = Math.PI * 2 * progress + startAngle; this.ctx.beginPath(); this.ctx.moveTo(16, 16); this.ctx.arc(16, 16, 16, startAngle, endAngle); this.ctx.fillStyle = this.color; this.ctx.fill(); this.updateFavicon(); } updateFavicon() { if (!this.link || !this.canvas) return; this.link.href = this.canvas.toDataURL('image/png'); } /** 恢复默认图标 */ recover() { if (!this.link || !this.initLink) return; this.link.href = this.initLink; } } const useFaviconProgress = () => { // }; const en = {alert:{comic_load_error:"Comic loading error",download_failed:"Download failed",fetch_comic_img_failed:"Failed to fetch comic images",img_load_failed:"Image loading failed",no_img_download:"No images available for download",repeat_load:"Loading image, please wait",retry_get_img_url:"Retrieve the URL of the image on page {{i}} again",server_connect_failed:"Unable to connect to the server"},button:{auto_scroll:"Auto scroll",close_current_page_translation:"Close translation of the current page",download_completed:"Download completed",download_completed_error:"Download complete, but {{errorNum}} images failed to download",downloading:"Downloading",fullscreen:"Fullscreen",fullscreen_exit:"Exit Fullscreen",grid_mode:"Grid mode",packaging:"Packaging",page_fill:"Page fill",page_mode_double:"Double page mode",page_mode_single:"Single page mode",scroll_mode:"Scroll mode",translate_current_page:"Translate current page",zoom_in:"Zoom in",zoom_out:"Zoom out"},description:"Add enhanced features to the comic site for optimized experience, including dual-page reading and translation.",eh_tag_lint:{combo:"[tag]: In most cases, Should coexist with [tag]",conflict:"[tag]: Should not coexist with [tag]",correct_tag:"Should be the correct tag",miss_female:"Missing male tag, might need",miss_parody:"Missing parody tag, might need",possible_conflict:"[tag]: In most cases, Should not coexist with [tag]",prerequisite:"[tag]: The prerequisite tag [tag] does not exist"},end_page:{next_button:"Next chapter",prev_button:"Prev chapter",tip:{end_jump:"Reached the last page, scrolling down will jump to the next chapter",exit:"Reached the last page, scrolling down will exit",start_jump:"Reached the first page, scrolling up will jump to the previous chapter"}},hotkeys:{enter_read_mode:"Enter reading mode",float_tag_list:"Floating tag list",jump_to_end:"Jump to the last page",jump_to_home:"Jump to the first page",page_down:"Turn the page to the down",page_up:"Turn the page to the up",repeat_tip:"This hotkey has been bound to \\"{{hotkey}}\\"",scroll_down:"Scroll down",scroll_left:"Scroll left",scroll_right:"Scroll right",scroll_up:"Scroll up",switch_auto_enlarge:"Switch auto image enlarge option",switch_dir:"Switch reading direction",switch_grid_mode:"Switch grid mode",switch_page_fill:"Switch page fill",switch_scroll_mode:"Switch scroll mode",switch_single_double_page_mode:"Switch single/double page mode"},img_status:{error:"Load Error",loading:"Loading",wait:"Waiting for load"},other:{auto:"Auto",disable:"Disable",distance:"distance",download:"Download",enabled:"Enabled",enter_comic_read_mode:"Enter comic reading mode",exit:"Exit",fab_hidden:"Hide floating button",fab_show:"Show floating button",fill_page:"Fill Page",hotkeys:"Hotkeys",img_loading:"Image loading",interval:"interval",loading_img:"Loading image",none:"None",or:"or",other:"Other",page_range:"Please enter the page range.:\\n (e.g., 1, 3-5, 9-)",read_mode:"Reading mode",setting:"Settings"},pwa:{alert:{img_data_error:"Image data error",img_not_found:"Image not found",img_not_found_files:"Please select an image file or a compressed file containing image files",img_not_found_folder:"No image files or compressed files containing image files in the folder",not_valid_url:"Not a valid URL",repeat_load:"Loading other files…",unzip_error:"Decompression error",unzip_password_error:"Decompression password error",userscript_not_installed:"ComicRead userscript not installed"},button:{enter_url:"Enter URL",install:"Install",no_more_prompt:"Do not prompt again",resume_read:"Restore reading",select_files:"Select File",select_folder:"Select folder"},install_md:"### Tired of opening this webpage every time?\\nIf you wish to:\\n1. Have an independent window, as if using local software\\n1. Add to the local compressed file opening method for easy direct opening\\n1. Use offline\\n### Welcome to install this page as a PWA app on your computer😃👍",message:{enter_password:"Please enter your password",unzipping:"Unzipping"},tip_enter_url:"Please enter the URL of the compressed file",tip_md:"# ComicRead PWA\\nRead **local** comics using [ComicRead](https://github.com/hymbz/ComicReadScript) reading mode.\\n---\\n### Drag and drop image files, folders, or compressed files directly to start reading\\n*You can also choose to **paste directly** or **enter** the URL of the compressed file for downloading and reading*"},setting:{hotkeys:{add:"Add new hotkeys",restore:"Restore default hotkeys"},language:"Language",option:{abreast_duplicate:"Column duplicates ratio",abreast_mode:"Abreast scroll mode",always_load_all_img:"Always load all images",autoFullscreen:"Auto fullscreen",autoHiddenMouse:"Auto hide mouse",auto_scroll_trigger_end:"Continue scrolling on the end page",auto_switch_page_mode:"Auto switch single/double page mode by aspect ratio",background_color:"Background Color",click_page_turn_area:"Touch area",click_page_turn_enabled:"Click to turn page",click_page_turn_swap_area:"Swap LR clickable areas",dark_mode:"Dark mode",dark_mode_auto:"Dark mode follow system",dir_ltr:"LTR (American comics)",dir_rtl:"RTL (Japanese manga)",disable_auto_enlarge:"Disable automatic image enlarge",first_page_fill:"Enable first page fill by default",fit_to_width:"Fit to width",img_recognition:"Image Recognition",img_recognition_background:"Recognition background color",img_recognition_pageFill:"Auto switch page fill",img_recognition_warn:"❗ The current browser does not support Web Workers. Enabling this feature may cause page lag. It's recommended to upgrade or switch browsers.",img_recognition_warn_2:"❗ The current website does not support Web Workers. Enabling this feature may cause page lag.",paragraph_appearance:"Appearance",paragraph_dir:"Reading direction",paragraph_display:"Display",paragraph_scrollbar:"Scrollbar",paragraph_translation:"Translation",preload_page_num:"Preload page number",scroll_end:"After reaching the End",scroll_end_auto:"First jump to previous/next chapter, else exit",scroll_mode_img_scale:"Scroll mode image zoom ratio",scroll_mode_img_spacing:"Scroll mode image spacing",scrollbar_auto_hidden:"Auto hide",scrollbar_easy_scroll:"Easy scroll",scrollbar_position:"position",scrollbar_position_bottom:"Bottom",scrollbar_position_hidden:"Hidden",scrollbar_position_right:"Right",scrollbar_position_top:"Top",scrollbar_show_img_status:"Show image loading status",show_clickable_area:"Show clickable areas",show_comments:"Show comments on the end page",swap_page_turn_key:"Swap LR page-turning keys",zoom:"Image zoom ratio"},translation:{cotrans_tip:"
Using the interface provided by Cotrans to translate images, which is maintained by its maintainer at their own expense.
\\nWhen multiple people use it at the same time, they need to queue and wait. If the waiting queue reaches its limit, uploading new images will result in an error. Please try again after a while.
\\nSo please mind the frequency of use.
\\nIt is highly recommended to use your own locally deployed project, as it does not consume server resources and does not require queuing.
",options:{box_threshold:"Box threshold",detection_resolution:"Text detection resolution",direction:"Render text orientation",direction_auto:"Follow source",direction_horizontal:"Horizontal only",direction_vertical:"Vertical only",force_retry:"Force retry (ignore cache)",inpainter:"Inpainter",inpainting_size:"Inpainting size",local_url:"customize server URL",mask_dilation_offset:"Mask dilation offset",only_download_translated:"Download only the translated images",target_language:"Target language",text_detector:"Text detector",translator:"Translator",unclip_ratio:"Unclip ratio"},range:"Scope of Translation",server:"Translation server",server_selfhosted:"Selfhosted",translate_all:"Translate all images",translate_to_end:"Translate the current page to the end"}},site:{add_feature:{add_hotkeys_actions:"Add hotkeys actions",auto_adjust_option:"Auto adjust reading option",auto_page_turn:"Infinite scroll",auto_show:"Auto enter reading mode",block_totally:"Totally block comics",colorize_tag:"Colorize tags",cross_site_link:"Cross-site Link",detect_ad:"Detect advertise page",expand_tag_list:"Expand tag list",float_tag_list:"Floating tag list",load_original_image:"Load original image",lock_option:"Lock site option",open_link_new_page:"Open links in a new page",quick_favorite:"Quick favorite",quick_rating:"Quick rating",quick_tag_define:"Quick view tag define",remember_current_site:"Remember the current site",tag_lint:"Tag Lint"},changed_load_failed:"The website has undergone changes, unable to load comics",ehentai:{change_favorite_failed:"Failed to change the favorite",change_favorite_success:"Successfully changed the favorite",change_rating_failed:"Failed to change the rating",change_rating_success:"Successfully changed the rating",fetch_favorite_failed:"Failed to get favorite info",fetch_img_page_source_failed:"Failed to get the source code of the image page",fetch_img_page_url_failed:"Failed to get the image page address from the detail page",fetch_img_url_failed:"Failed to get the image address from the image page",hitomi_error:"hitomi matching error",html_changed_link_failed:"The page structure has changed, and the associated external site features are not functioning properly",ip_banned:"IP address is banned",nhentai_error:"nhentai matching error",nhentai_failed:"Matching failed, please refresh after confirming login to {{nhentai}}"},nhentai:{fetch_next_page_failed:"Failed to get next page of comic data",tag_blacklist_fetch_failed:"Failed to fetch tag blacklist"},show_settings_menu:"Show settings menu",simple:{auto_read_mode_message:"\\"Auto enter reading mode\\" is enabled by default",no_img:"No suitable comic images were found.\\nIf necessary, you can click here to close the simple reading mode.",simple_read_mode:"Enter simple reading mode"}},touch_area:{menu:"Menu",next:"Next Page",prev:"Prev Page",type:{edge:"Edge",l:"L",left_right:"Left Right",up_down:"Up Down"}},translation:{status:{colorizing:"Colorizing","default":"Unknown status",detection:"Detecting text",downscaling:"Downscaling",error:"Error during translation","error-lang":"The target language is not supported by the chosen translator","error-translating":"Did not get any text back from the text translation service","error-with-id":"Error during translation",finished:"Finishing",inpainting:"Inpainting","mask-generation":"Generating mask",ocr:"Scanning text",pending:"Pending","pending-pos":"Pending",preparing:"Waiting for idle window",rendering:"Rendering",saved:"Saved","skip-no-regions":"No text regions detected in the image","skip-no-text":"No text detected in the image",textline_merge:"Merging text lines",translating:"Translating",upscaling:"Upscaling"},tip:{check_img_status_failed:"Failed to check image status",download_img_failed:"Failed to download image",get_translator_list_error:"Error occurred while getting the list of available translation services",id_not_returned:"No id returned",img_downloading:"Downloading images",img_not_fully_loaded:"Image has not finished loading",pending:"Pending, {{pos}} in queue",resize_img_failed:"Failed to resize image",translating:"Translating image",translation_completed:"Translation completed",upload:"Uploading image",upload_error:"Image upload error",upload_return_error:"Error during server translation",wait_translation:"Waiting for translation"},translator:{baidu:"baidu",deepl:"DeepL",google:"Google","gpt3.5":"GPT-3.5",none:"Remove texts",offline:"offline translator",original:"Original",youdao:"youdao"}},upscale:{module_download_complete:"Image Upscaling Model Download Complete",module_download_failed:"Image Upscaling Model Download Failed",module_downloading:"Image Upscaling Model Downloading...",title:"Upscale Image",upscaled:"upscaled",upscaling:"upscaling",webgpu_tip:"Unable to upscale images using WebGPU, processing will be slower"}}; const ru = {alert:{comic_load_error:"Ошибка загрузки комикса",download_failed:"Ошибка загрузки",fetch_comic_img_failed:"Не удалось загрузить изображения",img_load_failed:"Не удалось загрузить изображение",no_img_download:"Нет доступных картинок для загрузки",repeat_load:"Загрузка изображения, пожалуйста подождите",retry_get_img_url:"Повторно получить адрес изображения на странице {{i}}",server_connect_failed:"Не удалось подключиться к серверу"},button:{auto_scroll:"Автопрокрутка",close_current_page_translation:"Скрыть перевод текущей страницы",download_completed:"Загрузка завершена",download_completed_error:"Загрузка завершена, но {{errorNum}} изображений не удалось загрузить",downloading:"Скачивание",fullscreen:"полноэкранный",fullscreen_exit:"выйти из полноэкранного режима",grid_mode:"Режим сетки",packaging:"Упаковка",page_fill:"Заполнить страницу",page_mode_double:"Двухчастичный режим",page_mode_single:"Одностраничный режим",scroll_mode:"Режим прокрутки",translate_current_page:"Перевести текущую страницу",zoom_in:"Приблизить",zoom_out:"Уменьшить"},description:"Добавляет расширенные функции для удобства на сайт, такие как двухстраничный режим и перевод.",eh_tag_lint:{combo:"[тег]: В большинстве случаев должен сосуществовать с [тегом]",conflict:"[tag]: Не должен сосуществовать с [tag]",correct_tag:"Должен быть правильный тег",miss_female:"Отсутствует мужской тег, возможно, понадобится",miss_parody:"Отсутствует тег пародии, возможно, понадобится",possible_conflict:"[tag]: В большинстве случаев не должен сосуществовать с [tag]",prerequisite:"[tag]: Предварительный тег [tag] не существует"},end_page:{next_button:"Следующая глава",prev_button:"Предыдущая глава",tip:{end_jump:"Последняя страница, следующая глава ниже",exit:"Последняя страница, ниже комикс будет закрыт",start_jump:"Первая страница, выше будет загружена предыдущая глава"}},hotkeys:{enter_read_mode:"Режим чтения",float_tag_list:"Плавающий список тегов",jump_to_end:"Перейти к последней странице",jump_to_home:"Перейти к первой странице",page_down:"Перелистнуть страницу вниз",page_up:"Перелистнуть страницу вверх",repeat_tip:"Эта горячая клавиша была назначена на \\"{{hotkey}}\\"",scroll_down:"Прокрутить вниз",scroll_left:"Прокрутить влево",scroll_right:"Прокрутите вправо",scroll_up:"Прокрутите вверх",switch_auto_enlarge:"Автоматическое приближение",switch_dir:"Направление чтения",switch_grid_mode:"Режим сетки",switch_page_fill:"Заполнение страницы",switch_scroll_mode:"Режим прокрутки",switch_single_double_page_mode:"Одностраничный/Двухстраничный режим"},img_status:{error:"Ошибка загрузки",loading:"Загрузка",wait:"Ожидание загрузки"},other:{auto:"Авто",disable:"Отключить",distance:"расстояние",download:"Скачать",enabled:"Включено",enter_comic_read_mode:"Режим чтения комиксов",exit:"Выход",fab_hidden:"Скрыть плавающую кнопку",fab_show:"Показать плавающую кнопку",fill_page:"Заполнить страницу",hotkeys:"Горячие клавиши",img_loading:"Изображение загружается",interval:"интервал",loading_img:"Загрузка изображения",none:"Отсутствует",or:"или",other:"Другое",page_range:"Введите диапазон страниц.:\\n (например, 1, 3-5, 9-)",read_mode:"Режим чтения",setting:"Настройки"},pwa:{alert:{img_data_error:"Ошибка данных изображения",img_not_found:"Изображение не найдено",img_not_found_files:"Пожалуйста выберите файл или архив с изображениями",img_not_found_folder:"В папке не найдены изображения или архивы с изображениями",not_valid_url:"Невалидный URL",repeat_load:"Загрузка других файлов…",unzip_error:"Ошибка распаковки",unzip_password_error:"Неверный пароль от архива",userscript_not_installed:"ComicRead не установлен"},button:{enter_url:"Ввести URL",install:"Установить",no_more_prompt:"Больше не показывать",resume_read:"Продолжить чтение",select_files:"Выбрать файл",select_folder:"Выбрать папку"},install_md:"### Устали открывать эту страницу каждый раз?\\nЕсли вы хотите:\\n1. Иметь отдельное окно, как если бы вы использовали обычное программное обеспечение\\n1. Открывать архивы напрямую\\n1. Пользоваться оффлайн\\n### Установите эту страницу в качестве [PWA](https://ru.wikipedia.org/wiki/%D0%9F%D1%80%D0%BE%D0%B3%D1%80%D0%B5%D1%81%D1%81%D0%B8%D0%B2%D0%BD%D0%BE%D0%B5_%D0%B2%D0%B5%D0%B1-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5) на свой компьютер 🐺☝️",message:{enter_password:"Пожалуйста введите пароль",unzipping:"Распаковка"},tip_enter_url:"Введите URL архива",tip_md:"# ComicRead PWA\\nИспользуйте [ComicRead](https://github.com/hymbz/ComicReadScript) для чтения комиксов **локально**.\\n---\\n### Перетащите изображения, папки или архивы чтобы начать читать\\n*Вы так же можете **открыть** или **вставить** URL архива на напрямую*"},setting:{hotkeys:{add:"Добавить горячие клавиши",restore:"Восстановить горячие клавиши по умолчанию"},language:"Язык",option:{abreast_duplicate:"Коэффициент дублирования столбцов",abreast_mode:"Режим прокрутки в ряд",always_load_all_img:"Всегда загружать все изображения",autoFullscreen:"Авто полный экран",autoHiddenMouse:"Автоматически скрывать курсор мыши",auto_scroll_trigger_end:"Продолжить прокрутку на конечной странице",auto_switch_page_mode:"Автоматическое переключение режима одной/двойной страницы в зависимости от соотношения сторон",background_color:"Цвет фона",click_page_turn_area:"Область нажатия",click_page_turn_enabled:"Перелистывать по клику",click_page_turn_swap_area:"Поменять местами правую и левую области переключения страниц",dark_mode:"Ночная тема",dark_mode_auto:"Тёмный режим следует за системой",dir_ltr:"Чтение слева направо (Американские комиксы)",dir_rtl:"Чтение справа налево (Японская манга)",disable_auto_enlarge:"Отключить автоматическое масштабирование изображений",first_page_fill:"Включить заполнение первой страницы по умолчанию",fit_to_width:"По ширине",img_recognition:"распознавание изображений",img_recognition_background:"Определить цвет фона",img_recognition_pageFill:"Автоматическое переключение заполнения страницы",img_recognition_warn:"❗ Текущий браузер не поддерживает Web Workers. Включение этой функции может вызвать задержку страницы. Рекомендуется обновить или сменить браузер.",img_recognition_warn_2:"❗ Текущий веб-сайт не поддерживает Web Workers. Включение этой функции может привести к задержке страницы.",paragraph_appearance:"Внешность",paragraph_dir:"Направление чтения",paragraph_display:"Отображение",paragraph_scrollbar:"Полоса прокрутки",paragraph_translation:"Перевод",preload_page_num:"Предзагружать страниц",scroll_end:"После достижения конца",scroll_end_auto:"Сначала переход к предыдущей/следующей главе, иначе выход",scroll_mode_img_scale:"Коэффициент масштабирования изображения в режиме скроллинга",scroll_mode_img_spacing:"Расстояние между страницами в режиме скроллинга",scrollbar_auto_hidden:"Автоматически скрывать",scrollbar_easy_scroll:"Лёгкая прокрутка",scrollbar_position:"Позиция",scrollbar_position_bottom:"Снизу",scrollbar_position_hidden:"Спрятано",scrollbar_position_right:"Справа",scrollbar_position_top:"Сверху",scrollbar_show_img_status:"Показывать статус загрузки изображения",show_clickable_area:"Показывать кликабельные области",show_comments:"Показывать комментарии на последней странице",swap_page_turn_key:"Поменять местами клавиши переключения страниц",zoom:"Коэффициент масштабирования изображения"},translation:{cotrans_tip:"Использует для перевода Cotrans API, работающий исключительно за счёт своего создателя.
\\nЗапросы обрабатываются по одному в порядке синхронной очереди. Когда очередь превышает лимит новые запросы будут приводить к ошибке. Если такое случилось попробуйте позже.
\\nТак что пожалуйста учитывайте загруженность при выборе
\\nНастоятельно рекомендовано использовать проект развёрнутый локально т.к. это не потребляет серверные ресурсы и вы не ограничены очередью.
",options:{box_threshold:"Порог коробки",detection_resolution:"Разрешение распознавания текста",direction:"Ориетнация текста",direction_auto:"Следование оригиналу",direction_horizontal:"Только горизонтально",direction_vertical:"Только вертикально",force_retry:"Принудительный повтор(Игнорировать кэш)",inpainter:"Инпейнтер",inpainting_size:"Инпейнтинг размер области",local_url:"Настроить URL сервера",mask_dilation_offset:"Маскировочное смещение дилатации",only_download_translated:"Скачать только переведённые изображения",target_language:"Целевой язык",text_detector:"Детектор текста",translator:"Переводчик",unclip_ratio:"Необрезанное соотношение"},range:"Объем перевода",server:"Сервер",server_selfhosted:"Свой",translate_all:"Перевести все изображения",translate_to_end:"Переводить страницу до конца"}},site:{add_feature:{add_hotkeys_actions:"Добавить операции с горячими клавишами",auto_adjust_option:"Автоматическая настройка параметра чтения",auto_page_turn:"Автопереворот страниц",auto_show:"Автоматически включать режим чтения",block_totally:"Глобально заблокировать комиксы",colorize_tag:"Раскрасить теги",cross_site_link:"Кросс-сайтовая ссылка",detect_ad:"Detect advertise page",expand_tag_list:"Развернуть список тегов",float_tag_list:"Плавающий список тегов",load_original_image:"Загружать оригинальное изображение",lock_option:"Блокировка опции сайта",open_link_new_page:"Открывать ссылки в новой вкладке",quick_favorite:"Быстрый фаворит",quick_rating:"Быстрый рейтинг",quick_tag_define:"Определение тега быстрого просмотра",remember_current_site:"Запомнить текущий сайт",tag_lint:"Тэг Линт"},changed_load_failed:"Страница изменилась, невозможно загрузить комикс",ehentai:{change_favorite_failed:"Не удалось изменить избранное",change_favorite_success:"Избранное успешно изменено",change_rating_failed:"Не удалось изменить оценку",change_rating_success:"Успешно изменен рейтинг",fetch_favorite_failed:"Не удалось получить информацию о избранном",fetch_img_page_source_failed:"Не удалось получить исходный код страницы с изображениями",fetch_img_page_url_failed:"Не удалось получить адрес страницы изображений из деталей",fetch_img_url_failed:"Не удалось получить адрес изображения",hitomi_error:"Ошибка сопоставления hitomi",html_changed_link_failed:"Структура страницы изменилась, и связанные функции внешнего сайта не работают должным образом",ip_banned:"IP адрес забанен",nhentai_error:"Ошибка сопоставления nhentai",nhentai_failed:"Ошибка сопостовления. Пожалуйста перезагрузите страницу после входа на {{nhentai}}"},nhentai:{fetch_next_page_failed:"Не удалось получить следующую страницу",tag_blacklist_fetch_failed:"Не удалось получить заблокированные теги"},show_settings_menu:"Показать меню настроек",simple:{auto_read_mode_message:"\\"Автоматически включать режим чтения\\" по умолчанию",no_img:"Не найдено подходящих изображений. Можно нажать тут что бы выключить режим простого чтения.",simple_read_mode:"Включить простой режим чтения"}},touch_area:{menu:"Меню",next:"Следующая страница",prev:"Предыдущая страница",type:{edge:"Грань",l:"L",left_right:"Лево Право",up_down:"Верх Низ"}},translation:{status:{colorizing:"Раскрашивание","default":"Неизвестный статус",detection:"Распознавание текста",downscaling:"Уменьшение масштаба",error:"Ошибка перевода","error-lang":"Целевой язык не поддерживается выбранным переводчиком","error-translating":"Ошибка перевода(пустой ответ)","error-with-id":"Ошибка во время перевода",finished:"Завершение",inpainting:"Наложение","mask-generation":"Генерация маски",ocr:"Распознавание текста",pending:"Ожидание","pending-pos":"Ожидание",preparing:"Ожидание окна бездействия",rendering:"Отрисовка",saved:"Сохранено","skip-no-regions":"На изображении не обнаружено текстовых областей.","skip-no-text":"Текст на изображении не обнаружен",textline_merge:"Обьединение текста",translating:"Переводится",upscaling:"Увеличение изображения"},tip:{check_img_status_failed:"Не удалось проверить статус изображения",download_img_failed:"Не удалось скачать изображение",get_translator_list_error:"Произошла ошибка во время получения списка доступных переводчиков",id_not_returned:"ID не вернули(",img_downloading:"Скачивание изображений",img_not_fully_loaded:"Изображение всё ещё загружается",pending:"Ожидение, позиция в очереди {{pos}}",resize_img_failed:"Не удалось изменить размер изображения",translating:"Изображение переводится",translation_completed:"Перевод завершён",upload:"Загрузка изображения",upload_error:"Ошибка загрузки изображения",upload_return_error:"Ошибка перевода на сервере",wait_translation:"Ожидание перевода"},translator:{baidu:"baidu",deepl:"DeepL",google:"Google","gpt3.5":"GPT-3.5",none:"Убрать текст",offline:"Оффлайн переводчик",original:"Оригинал",youdao:"youdao"}},upscale:{module_download_complete:"Загрузка модели увеличения изображений завершена",module_download_failed:"Сбой загрузки модели увеличения изображений",module_downloading:"Загрузка модели увеличения изображений...",title:"Увеличение изображения",upscaled:"Увеличенный",upscaling:"Увеличивается",webgpu_tip:"Невозможно увеличить изображения с помощью WebGPU, обработка будет медленнее"}}; const zh = {alert:{comic_load_error:"漫画加载出错",download_failed:"下载失败",fetch_comic_img_failed:"获取漫画图片失败",img_load_failed:"图片加载失败",no_img_download:"没有能下载的图片",repeat_load:"加载图片中,请稍候",retry_get_img_url:"重新获取第 {{i}} 页图片的地址",server_connect_failed:"无法连接到服务器"},button:{auto_scroll:"自动滚动",close_current_page_translation:"关闭当前页的翻译",download_completed:"下载完成",download_completed_error:"下载完成,但有 {{errorNum}} 张图片下载失败",downloading:"下载中",fullscreen:"全屏",fullscreen_exit:"退出全屏",grid_mode:"网格模式",packaging:"打包中",page_fill:"页面填充",page_mode_double:"双页模式",page_mode_single:"单页模式",scroll_mode:"卷轴模式",translate_current_page:"翻译当前页",zoom_in:"放大",zoom_out:"缩小"},description:"为漫画站增加双页阅读、翻译等优化体验的增强功能。",eh_tag_lint:{combo:"存在 [tag] 时,一般也存在 [tag]",conflict:"存在 [tag] 时,不应该存在 [tag]",correct_tag:"应该是正确的标签",miss_female:"缺少男性标签,可能需要",miss_parody:"缺少原作标签,可能需要",possible_conflict:"存在 [tag] 时,一般不应该存在 [tag]",prerequisite:"[tag] 的前置标签 [tag] 不存在"},end_page:{next_button:"下一话",prev_button:"上一话",tip:{end_jump:"已到结尾,继续向下翻页将跳至下一话",exit:"已到结尾,继续翻页将退出",start_jump:"已到开头,继续向上翻页将跳至上一话"}},hotkeys:{enter_read_mode:"进入阅读模式",float_tag_list:"悬浮标签列表",jump_to_end:"跳至尾页",jump_to_home:"跳至首页",page_down:"向下翻页",page_up:"向上翻页",repeat_tip:"此快捷键已被绑定至「{{hotkey}}」",scroll_down:"向下滚动",scroll_left:"向左滚动",scroll_right:"向右滚动",scroll_up:"向上滚动",switch_auto_enlarge:"切换图片自动放大选项",switch_dir:"切换阅读方向",switch_grid_mode:"切换网格模式",switch_page_fill:"切换页面填充",switch_scroll_mode:"切换卷轴模式",switch_single_double_page_mode:"切换单双页模式"},img_status:{error:"加载出错",loading:"正在加载",wait:"等待加载"},other:{auto:"自动",disable:"禁用",distance:"距离",download:"下载",enabled:"启用",enter_comic_read_mode:"进入漫画阅读模式",exit:"退出",fab_hidden:"隐藏悬浮按钮",fab_show:"显示悬浮按钮",fill_page:"填充页",hotkeys:"快捷键",img_loading:"图片加载中",interval:"间隔",loading_img:"加载图片中",none:"无",or:"或",other:"其他",page_range:"请输入页码范围:\\n(例如:1, 3-5, 9-)",read_mode:"阅读模式",setting:"设置"},pwa:{alert:{img_data_error:"图片数据错误",img_not_found:"找不到图片",img_not_found_files:"请选择图片文件或含有图片文件的压缩包",img_not_found_folder:"文件夹下没有图片文件或含有图片文件的压缩包",not_valid_url:"不是有效的 URL",repeat_load:"正在加载其他文件中……",unzip_error:"解压出错",unzip_password_error:"解压密码错误",userscript_not_installed:"未安装 ComicRead 脚本"},button:{enter_url:"输入 URL",install:"安装",no_more_prompt:"不再提示",resume_read:"恢复阅读",select_files:"选择文件",select_folder:"选择文件夹"},install_md:"### 每次都要打开这个网页很麻烦?\\n如果你希望\\n1. 能有独立的窗口,像是在使用本地软件一样\\n1. 加入本地压缩文件的打开方式之中,方便直接打开\\n1. 离线使用~~(主要是担心国内网络抽风无法访问这个网页~~\\n### 欢迎将本页面作为 PWA 应用安装到电脑上😃👍",message:{enter_password:"请输入密码",unzipping:"解压缩中"},tip_enter_url:"请输入压缩包 URL",tip_md:"# ComicRead PWA\\n使用 [ComicRead](https://github.com/hymbz/ComicReadScript) 的阅读模式阅读**本地**漫画\\n---\\n### 将图片文件、文件夹、压缩包直接拖入即可开始阅读\\n*也可以选择**直接粘贴**或**输入**压缩包 URL 下载阅读*"},setting:{hotkeys:{add:"添加新快捷键",restore:"恢复默认快捷键"},language:"语言",option:{abreast_duplicate:"每列重复比例",abreast_mode:"并排卷轴模式",always_load_all_img:"始终加载所有图片",autoFullscreen:"自动全屏",autoHiddenMouse:"自动隐藏鼠标",auto_scroll_trigger_end:"在结束页上继续滚动",auto_switch_page_mode:"根据屏幕比例切换单双页",background_color:"背景颜色",click_page_turn_area:"点击区域",click_page_turn_enabled:"点击翻页",click_page_turn_swap_area:"左右点击区域交换",dark_mode:"黑暗模式",dark_mode_auto:"黑暗模式跟随系统",dir_ltr:"从左到右(美漫)",dir_rtl:"从右到左(日漫)",disable_auto_enlarge:"禁止图片自动放大",first_page_fill:"默认启用首页填充",fit_to_width:"图片适合宽度",img_recognition:"图像识别",img_recognition_background:"识别背景色",img_recognition_pageFill:"自动调整页面填充",img_recognition_warn:"❗ 当前浏览器不支持 Web Worker,开启此功能可能导致页面卡顿,建议升级或更换浏览器。",img_recognition_warn_2:"❗ 当前网站不支持 Web Worker,开启此功能可能导致页面卡顿。",paragraph_appearance:"外观",paragraph_dir:"阅读方向",paragraph_display:"显示",paragraph_scrollbar:"滚动条",paragraph_translation:"翻译",preload_page_num:"预加载页数",scroll_end:"翻页至尽头后",scroll_end_auto:"优先跳至上/下一话,否则退出",scroll_mode_img_scale:"卷轴图片缩放",scroll_mode_img_spacing:"卷轴图片间距",scrollbar_auto_hidden:"自动隐藏",scrollbar_easy_scroll:"快捷滚动",scrollbar_position:"位置",scrollbar_position_bottom:"底部",scrollbar_position_hidden:"隐藏",scrollbar_position_right:"右侧",scrollbar_position_top:"顶部",scrollbar_show_img_status:"显示图片加载状态",show_clickable_area:"显示点击区域",show_comments:"在结束页显示评论",swap_page_turn_key:"左右翻页键交换",zoom:"图片缩放"},translation:{cotrans_tip:"将使用 Cotrans 提供的接口翻译图片,该服务器由其维护者用爱发电自费维护
\\n多人同时使用时需要排队等待,等待队列达到上限后再上传新图片会报错,需要过段时间再试
\\n所以还请 注意用量
\\n更推荐使用自己本地部署的项目,既不占用服务器资源也不需要排队
",options:{box_threshold:"文本框阈值",detection_resolution:"文本扫描清晰度",direction:"渲染字体方向",direction_auto:"原文一致",direction_horizontal:"仅限水平",direction_vertical:"仅限垂直",force_retry:"忽略缓存强制重试",inpainter:"图像修复器",inpainting_size:"图像修复尺寸",local_url:"自定义服务器 URL",mask_dilation_offset:"掩码膨胀偏移量",only_download_translated:"只下载翻译完的图片",target_language:"目标语言",text_detector:"文本扫描器",translator:"翻译服务",unclip_ratio:"文本框膨胀比率"},range:"翻译范围",server:"翻译服务器",server_selfhosted:"本地部署",translate_all:"翻译全部图片",translate_to_end:"翻译当前页至结尾"}},site:{add_feature:{add_hotkeys_actions:"增加快捷键操作",auto_adjust_option:"自动调整阅读配置",auto_page_turn:"无限滚动",auto_show:"自动进入阅读模式",block_totally:"彻底屏蔽漫画",colorize_tag:"标签染色",cross_site_link:"关联外站",detect_ad:"识别广告页",expand_tag_list:"展开标签列表",float_tag_list:"悬浮标签列表",load_original_image:"加载原图",lock_option:"锁定站点配置",open_link_new_page:"在新页面中打开链接",quick_favorite:"快捷收藏",quick_rating:"快捷评分",quick_tag_define:"快捷查看标签定义",remember_current_site:"记住当前站点",tag_lint:"标签检查"},changed_load_failed:"网站发生变化,无法加载漫画",ehentai:{change_favorite_failed:"收藏夹修改失败",change_favorite_success:"收藏夹修改成功",change_rating_failed:"评分修改失败",change_rating_success:"评分修改成功",fetch_favorite_failed:"获取收藏夹信息失败",fetch_img_page_source_failed:"获取图片页源码失败",fetch_img_page_url_failed:"从详情页获取图片页地址失败",fetch_img_url_failed:"从图片页获取图片地址失败",hitomi_error:"hitomi 匹配出错",html_changed_link_failed:"页面结构发生改变,关联外站功能无法正常生效",ip_banned:"IP地址被禁",nhentai_error:"nhentai 匹配出错",nhentai_failed:"匹配失败,请在确认登录 {{nhentai}} 后刷新"},nhentai:{fetch_next_page_failed:"获取下一页漫画数据失败",tag_blacklist_fetch_failed:"标签黑名单获取失败"},show_settings_menu:"显示设置菜单",simple:{auto_read_mode_message:"已默认开启「自动进入阅读模式」",no_img:"未找到合适的漫画图片,\\n如有需要可点此关闭简易阅读模式",simple_read_mode:"使用简易阅读模式"}},touch_area:{menu:"菜单",next:"下页",prev:"上页",type:{edge:"边缘",l:"L",left_right:"左右",up_down:"上下"}},translation:{status:{colorizing:"正在上色","default":"未知状态",detection:"正在检测文本",downscaling:"正在缩小图片",error:"翻译出错","error-lang":"你选择的翻译服务不支持你选择的语言","error-translating":"翻译服务没有返回任何文本","error-with-id":"翻译出错",finished:"正在整理结果",inpainting:"正在修补图片","mask-generation":"正在生成文本掩码",ocr:"正在识别文本",pending:"正在等待","pending-pos":"正在等待",preparing:"等待空闲窗口",rendering:"正在渲染",saved:"保存结果","skip-no-regions":"图片中没有检测到文本区域","skip-no-text":"图片中没有检测到文本",textline_merge:"正在整合文本",translating:"正在翻译文本",upscaling:"正在放大图片"},tip:{check_img_status_failed:"检查图片状态失败",download_img_failed:"下载图片失败",get_translator_list_error:"获取可用翻译服务列表时出错",id_not_returned:"未返回 id",img_downloading:"下载图片中",img_not_fully_loaded:"图片未加载完毕",pending:"正在等待,列队还有 {{pos}} 张图片",resize_img_failed:"缩放图片失败",translating:"翻译图片中",translation_completed:"翻译完成",upload:"上传图片中",upload_error:"上传图片出错",upload_return_error:"服务器翻译出错",wait_translation:"等待翻译"},translator:{baidu:"百度",deepl:"DeepL",google:"谷歌","gpt3.5":"GPT-3.5",none:"删除文本",offline:"离线模型",original:"原文",youdao:"有道"}},upscale:{module_download_complete:"图片放大模型下载完成",module_download_failed:"图片放大模型下载失败",module_downloading:"图片放大模型下载中...",title:"放大图片",upscaled:"已放大",upscaling:"放大中",webgpu_tip:"无法使用 WebGPU 放大图片,处理速度将变慢"}}; const prefix = ['%cComicRead', 'background-color: #607d8b; color: white; padding: 2px 4px; border-radius: 4px;']; // oxlint-disable-next-line no-console const log = (...args) => console.log(...prefix, ...args); log.warn = (...args) => console.warn(...prefix, ...args); log.error = (...args) => console.error(...prefix, ...args); /** * Creates a callback that is debounced and cancellable. The debounced callback is called on **trailing** edge. * * The timeout will be automatically cleared on root dispose. * * @param callback The callback to debounce * @param wait The duration to debounce in milliseconds * @returns The debounced function * * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#debounce * * @example * \`\`\`ts * const fn = debounce((message: string) => console.log(message), 250); * fn('Hello!'); * fn.clear() // clears a timeout in progress * \`\`\` */ const debounce$1 = (callback, wait) => { if (web.isServer) { return Object.assign(() => void 0, { clear: () => void 0 }); } let timeoutId; const clear = () => clearTimeout(timeoutId); if (solidJs.getOwner()) solidJs.onCleanup(clear); const debounced = (...args) => { if (timeoutId !== undefined) clear(); timeoutId = setTimeout(() => callback(...args), wait); }; return Object.assign(debounced, { clear }); }; /** * Creates a callback that is throttled and cancellable. The throttled callback is called on **trailing** edge. * * The timeout will be automatically cleared on root dispose. * * @param callback The callback to throttle * @param wait The duration to throttle * @returns The throttled callback trigger * * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#throttle * * @example * \`\`\`ts * const trigger = throttle((val: string) => console.log(val), 250); * trigger('my-new-value'); * trigger.clear() // clears a timeout in progress * \`\`\` */ const throttle$1 = (callback, wait) => { if (web.isServer) { return Object.assign(() => void 0, { clear: () => void 0 }); } let isThrottled = false, timeoutId, lastArgs; const throttled = (...args) => { lastArgs = args; if (isThrottled) return; isThrottled = true; timeoutId = setTimeout(() => { callback(...lastArgs); isThrottled = false; }, wait); }; const clear = () => { clearTimeout(timeoutId); isThrottled = false; }; if (solidJs.getOwner()) solidJs.onCleanup(clear); return Object.assign(throttled, { clear }); }; /** * Creates a scheduled and cancellable callback that will be called on the **leading** edge for the first call, and **trailing** edge for other calls. * * The timeout will be automatically cleared on root dispose. * * @param schedule {@link debounce} or {@link throttle} * @param callback The callback to debounce/throttle * @param wait timeout duration * @returns The scheduled callback trigger * * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#leadingAndTrailing * * @example * \`\`\`ts * const trigger = leadingAndTrailing(throttle, (val: string) => console.log(val), 250); * trigger('my-new-value'); * trigger.clear() // clears a timeout in progress * \`\`\` */ function leadingAndTrailing(schedule, callback, wait) { if (web.isServer) { let called = false; const scheduled = (...args) => { if (called) return; called = true; callback(...args); }; return Object.assign(scheduled, { clear: () => void 0 }); } let State; (function (State) { State[State["Ready"] = 0] = "Ready"; State[State["Leading"] = 1] = "Leading"; State[State["Trailing"] = 2] = "Trailing"; })(State || (State = {})); let state = State.Ready; const scheduled = schedule((args) => { state === State.Trailing && callback(...args); state = State.Ready; }, wait); const fn = (...args) => { if (state !== State.Trailing) { if (state === State.Ready) callback(...args); state += 1; } scheduled(args); }; const clear = () => { state = State.Ready; scheduled.clear(); }; if (solidJs.getOwner()) solidJs.onCleanup(clear); return Object.assign(fn, { clear }); } /** * Creates a signal used for scheduling execution of solid computations by tracking. * * @param schedule Schedule the invalidate function (can be {@link debounce} or {@link throttle}) * @returns A function used to track the signal. It returns \`true\` if the signal is dirty *(callback should be called)* and \`false\` otherwise. * * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/scheduled#createScheduled * * @example * \`\`\`ts * const debounced = createScheduled(fn => debounce(fn, 250)); * * createEffect(() => { * // track source signal * const value = count(); * // track the debounced signal and check if it's dirty * if (debounced()) { * console.log('count', value); * } * }); * \`\`\` */ // Thanks to Fabio Spampinato (https://github.com/fabiospampinato) for the idea for the primitive function createScheduled(schedule) { let listeners = 0; let isDirty = false; const [track, dirty] = solidJs.createSignal(void 0, { equals: false }); const call = schedule(() => { isDirty = true; dirty(); }); return () => { if (!isDirty) call(), track(); if (isDirty) { isDirty = !!listeners; return true; } if (solidJs.getListener()) { listeners++; solidJs.onCleanup(() => listeners--); } return false; }; } function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var es6 = function equal(a, b) { if (a === b) return true; if (a && b && typeof a == 'object' && typeof b == 'object') { if (a.constructor !== b.constructor) return false; var length, i, keys; if (Array.isArray(a)) { length = a.length; if (length != b.length) return false; for (i = length; i-- !== 0;) if (!equal(a[i], b[i])) return false; return true; } if ((a instanceof Map) && (b instanceof Map)) { if (a.size !== b.size) return false; for (i of a.entries()) if (!b.has(i[0])) return false; for (i of a.entries()) if (!equal(i[1], b.get(i[0]))) return false; return true; } if ((a instanceof Set) && (b instanceof Set)) { if (a.size !== b.size) return false; for (i of a.entries()) if (!b.has(i[0])) return false; return true; } if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) { length = a.length; if (length != b.length) return false; for (i = length; i-- !== 0;) if (a[i] !== b[i]) return false; return true; } if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags; if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf(); if (a.toString !== Object.prototype.toString) return a.toString() === b.toString(); keys = Object.keys(a); length = keys.length; if (length !== Object.keys(b).length) return false; for (i = length; i-- !== 0;) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; for (i = length; i-- !== 0;) { var key = keys[i]; if (!equal(a[key], b[key])) return false; } return true; } // true if both NaN, false otherwise return a!==a && b!==b; }; const isEqual = /*@__PURE__*/getDefaultExportFromCjs(es6); /** 图片文件扩展名缩写 */ const fileType = { j: 'jpg', p: 'png', g: 'gif', w: 'webp', b: 'bmp' }; const throttle = (fn, wait = 100) => leadingAndTrailing(throttle$1, fn, wait); const debounce = (fn, wait = 100) => debounce$1(fn, wait); const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); const clamp = (min, val, max) => Math.max(Math.min(max, val), min); const inRange = (min, val, max) => val >= min && val <= max; const getFileName = url => url.match(/.+\\/([^?]+)/)?.[1]; /** 判断两个数是否在指定误差范围内相等 */ const approx = (val, target, range) => Math.abs(target - val) <= range; /** 创建一个只会执行一次的函数 */ const onec = fn => { let hasRun = false; return () => { if (hasRun) return; hasRun = true; fn(); }; }; // oxlint-disable-next-line func-style function range(a, b, c) { switch (typeof b) { case 'undefined': return [...Array.from({ length: a }).keys()]; case 'number': { const list = []; for (let i = a; i < b; i++) list.push(c ? c(i) : i); return list; } case 'function': return Array.from({ length: a }, (_, i) => b(i)); } } /** * 对 document.querySelector 的封装 * 将默认返回类型改为 HTMLElement */ const querySelector = selector => document.querySelector(selector); /** * 对 document.querySelector 的封装 * 将默认返回类型改为 HTMLElement */ const querySelectorAll = selector => [...document.querySelectorAll(selector)]; /** 返回 Dom 的点击函数 */ const querySelectorClick = (selector, textContent) => { let getDom; if (typeof selector === 'function') getDom = selector;else if (textContent) { getDom = () => querySelectorAll(selector).find(e => e.textContent?.includes(textContent)); } else getDom = () => querySelector(selector); if (getDom()) return () => getDom()?.click(); }; /** 找出数组中出现最多次的元素 */ const getMostItem = list => { const counts = new Map(); for (const val of list) counts.set(val, (counts.get(val) ?? 0) + 1); return [...counts.entries()].reduce((maxItem, item) => maxItem[1] > item[1] ? maxItem : item)[0]; }; /** 判断字符串是否为 URL */ const isUrl = text => { // 等浏览器版本上来后可以直接使用 URL.canParse try { return Boolean(new URL(text)); } catch { return false; } }; /** 将 blob 数据作为文件保存至本地 */ const saveAs = (blob, name = 'download') => { const a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); a.download = name; a.rel = 'noopener'; a.href = URL.createObjectURL(blob); setTimeout(() => a.dispatchEvent(new MouseEvent('click'))); }; /** 滚动页面到指定元素的所在位置 */ const scrollIntoView = (selector, behavior = 'instant') => querySelector(selector)?.scrollIntoView({ behavior }); /** 确保函数在同一时间下只有一个在运行 */ const singleThreaded = (callback, initState) => { const state = { running: false, argList: [], continueRun: (...args) => state.argList.length > 0 || state.argList.push(args), ...initState }; const work = async () => { if (state.argList.length === 0) return; const args = state.argList.shift(); try { state.running = true; await callback(state, ...args); } catch (error) { await sleep(100); if (state.argList.length === 0) throw error; } finally { if (state.abandon) state.argList.length = 0; if (state.argList.length > 0) setTimeout(work, state.timeout);else state.running = false; } }; return (...args) => { state.argList.push(args); if (!state.running) return work(); }; }; /** * 限制 Promise 并发 * @param fnList 任务函数列表 * @param callBack 成功执行一个 Promise 后调用,主要用于显示进度 * @param limit 限制数 * @returns 所有 Promise 的返回值 */ const plimit = async (fnList, callBack = undefined, limit = 10) => { let doneNum = 0; const totalNum = fnList.length; const resList = []; const execPool = new Set(); const taskList = fnList.map((fn, i) => { let p; return () => { p = (async () => { resList[i] = await fn(); doneNum += 1; execPool.delete(p); callBack?.(doneNum, totalNum, resList, i); })(); execPool.add(p); }; }); // eslint-disable-next-line no-unmodified-loop-condition while (doneNum !== totalNum) { while (taskList.length > 0 && execPool.size < limit) taskList.shift()(); await Promise.race(execPool); } return resList; }; /** * 判断使用参数颜色作为默认值时是否需要切换为黑暗模式 * @param hexColor 十六进制颜色。例如 #112233 */ const needDarkMode = hexColor => { // by: https://24ways.org/2010/calculating-color-contrast const r = Number.parseInt(hexColor.slice(1, 3), 16); const g = Number.parseInt(hexColor.slice(3, 5), 16); const b = Number.parseInt(hexColor.slice(5, 7), 16); const yiq = (r * 299 + g * 587 + b * 114) / 1000; return yiq < 128; }; // oxlint-disable-next-line func-style async function wait(fn, timeout = Number.POSITIVE_INFINITY, waitTime = 100) { let res = await fn(); let _timeout = timeout; while (_timeout > 0 && !res) { await sleep(waitTime); _timeout -= waitTime; res = await fn(); } return res; } async function waitDom(selector, timeout) { return wait(() => querySelector(selector), timeout); } /** 等待指定的图片元素加载完成 */ const waitImgLoad = (target, timeout) => new Promise((resolve, reject) => { const img = typeof target === 'string' ? new Image() : target; if (img.complete && img.naturalHeight) resolve(img); const id = timeout ? window.setTimeout(() => reject(new Error('timeout')), timeout) : undefined; const handleError = e => { window.clearTimeout(id); reject(new Error(e.message)); }; const handleLoad = () => { window.clearTimeout(id); img.removeEventListener('error', handleError); resolve(img); }; img.addEventListener('load', handleLoad, { once: true }); img.addEventListener('error', handleError, { once: true }); if (typeof target === 'string') img.src = target; }); /** 将指定的布尔值转换为字符串或未定义 */ const boolDataVal = val => val ? '' : undefined; /** 测试图片 url 能否正确加载 */ const testImgUrl = url => new Promise(resolve => { const img = new Image(); img.onload = () => resolve(true); img.onerror = () => resolve(false); img.src = url; }); const canvasToBlob = (canvas, type, quality = 1) => { if (canvas instanceof OffscreenCanvas) return canvas.convertToBlob({ type, quality }); return new Promise((resolve, reject) => { canvas.toBlob(blob => blob ? resolve(blob) : reject(new Error('Canvas toBlob failed')), type, quality); }); }; /** * 求 a 和 b 的差集,相当于从 a 中删去和 b 相同的属性 * * 不会修改参数对象,返回的是新对象 */ const difference = (a, b) => { const res = {}; const keys = Object.keys(a); for (const key of keys) { if (typeof a[key] === 'object' && typeof b[key] === 'object') { const _res = difference(a[key], b[key]); if (Object.keys(_res).length > 0) res[key] = _res; } else if (a[key] !== b?.[key]) res[key] = a[key]; } return res; }; const _assign = (a, b) => { // oxlint-disable-next-line prefer-structured-clone const res = JSON.parse(JSON.stringify(a)); const keys = Object.keys(b); for (const key of keys) { if (res[key] === undefined) res[key] = b[key];else if (typeof b[key] === 'object') { const _res = _assign(res[key], b[key]); if (Object.keys(_res).length > 0) res[key] = _res; } else if (res[key] !== b[key]) res[key] = b[key]; } return res; }; /** * Object.assign 的深拷贝版,不会导致子对象属性的缺失 * * 不会修改参数对象,返回的是新对象 */ const assign = (target, ...sources) => { let res = target; for (const source of sources) if (typeof source === 'object') res = _assign(res, source); return res; }; /** 根据路径获取对象下的指定值 */ const byPath = (obj, path, handleVal) => { const keys = typeof path === 'string' ? path.split('.') : path; let target = obj; for (let i = 0; i < keys.length; i++) { let key = keys[i]; // 兼容含有「.」的 key while (!Reflect.has(target, key) && i < keys.length) { i += 1; if (keys[i] === undefined) break; key += \`.\${keys[i]}\`; } if (handleVal && i > keys.length - 2 && Reflect.has(target, key)) { const res = handleVal(target, key); while (i < keys.length - 1) { target = target[key]; i += 1; key = keys[i]; } if (res !== undefined) target[key] = res; break; } target = target[key]; } if (target === obj) return null; return target; }; const requestIdleCallback = (callback, timeout) => { if (Reflect.has(window, 'requestIdleCallback')) return window.requestIdleCallback(callback, { timeout }); return window.setTimeout(callback, 16); }; /** 获取键盘事件的编码 */ const getKeyboardCode = e => { let { key } = e; switch (key) { case 'Shift': case 'Control': case 'Alt': return key; } key = key.replaceAll(/\\b[A-Z]\\b/g, match => match.toLowerCase()); if (e.ctrlKey) key = \`Ctrl + \${key}\`; if (e.altKey) key = \`Alt + \${key}\`; if (e.shiftKey) key = \`Shift + \${key}\`; return key; }; /** 将快捷键的编码转换成更易读的形式 */ const keyboardCodeToText = code => code.replace('Control', 'Ctrl').replace('ArrowUp', '↑').replace('ArrowDown', '↓').replace('ArrowLeft', '←').replace('ArrowRight', '→').replace(/^\\s$/, 'Space'); /** 将 HTML 字符串转换为 DOM 对象 */ const domParse = html => new DOMParser().parseFromString(html, 'text/html'); /** 监听键盘事件 */ const linstenKeydown = (handler, capture) => window.addEventListener('keydown', e => { // 跳过输入框的键盘事件 switch (e.target.tagName) { case 'INPUT': case 'TEXTAREA': return; } return handler(e); }, { capture }); /** * 劫持修改原网页上的函数 * * 如果传入函数的所需参数为零,将在原函数执行完后自动调用 */ const hijackFn = (fnName, fn) => { const rawFn = unsafeWindow[fnName]; unsafeWindow[fnName] = fn.length === 0 ? (...args) => { const res = rawFn(...args); fn(); return res; } : (...args) => fn(rawFn, args); }; const getGmValue = async (name, setValueFn) => { const value = await GM.getValue(name); if (value !== undefined) return value; await setValueFn(); return await GM.getValue(name); }; /** 根据范围文本提取指定范围的元素的 index */ const extractRange = (rangeText, length) => { const list = new Set(); for (const text of rangeText.replaceAll(/[^\\d,-]/g, '').split(',')) { if (/^\\d+$/.test(text)) list.add(Number(text) - 1);else if (/^\\d*-\\d*$/.test(text)) { let [start, end] = text.split('-').map(Number); end ||= length; for (start--, end--; start <= end; start++) list.add(start); } } return list; }; /** extractRange 的逆向,按照相同的语法表述一个结果数组 */ const descRange = (list, length) => { let text = ''; const nowRange = []; const pushRange = newIndex => { if (nowRange.length === 0) return; if (text.length > 0) text += ', '; if (nowRange.length === 1) text += nowRange[0] + 1;else { const end = newIndex === undefined && nowRange[1] === length - 1 ? '' : nowRange[1] + 1; text += \`\${nowRange[0] + 1}-\${end}\`; } nowRange.length = 0; if (newIndex !== undefined) nowRange[0] = newIndex; }; for (const i of list) { switch (nowRange.length) { case 0: nowRange[0] = i; break; case 1: if (i === nowRange[0] + 1) nowRange[1] = i;else pushRange(i); break; case 2: if (i === nowRange[1] + 1) nowRange[1] = i;else pushRange(i); break; } } pushRange(); return text; }; /** 监听 url 变化 */ const onUrlChange = (fn, handleUrl = location => location.href) => { let lastUrl = ''; const refresh = singleThreaded(async () => { if (!(await wait(() => handleUrl(location) !== lastUrl, 5000))) return; const nowUrl = handleUrl(location); await fn(lastUrl, nowUrl); lastUrl = nowUrl; }); const controller = new AbortController(); for (const eventName of ['click', 'popstate']) window.addEventListener(eventName, refresh, { capture: true, signal: controller.signal }); refresh(); return () => controller.abort(); }; /** wait,但是只在 url 变化时判断 */ const waitUrlChange = isValidUrl => new Promise(resolve => { const abort = onUrlChange(() => { if (!isValidUrl()) return; resolve(); abort(); }); }); // TODO: 用这个重构相关实现 class AnimationFrame { animationId = 0; call = () => { this.animationId = requestAnimationFrame(this.frame); }; cancel = () => { if (!this.animationId) return; cancelAnimationFrame(this.animationId); this.animationId = 0; }; } /** 锁定屏幕禁止自动熄屏 */ class WakeLock { isSupported = false; lock = null; constructor() { if (!('wakeLock' in navigator)) return; this.isSupported = true; } on = async () => { if (!this.isSupported) return null; try { this.lock = await navigator.wakeLock.request('screen'); return this.lock.released; } catch { return false; } }; off = async () => { if (!this.lock) return; await this.lock.release(); this.lock = null; }; } const getImageData = img => { const { naturalWidth: width, naturalHeight: height } = img; const canvas = new OffscreenCanvas(width, height); const ctx = canvas.getContext('2d', { willReadFrequently: true }); ctx.drawImage(img, 0, 0); return ctx.getImageData(0, 0, width, height); }; const [lang, setLang] = solidJs.createSignal('zh'); const setInitLang = async () => setLang(await languages.getInitLang()); const t = solidJs.createRoot(() => { solidJs.createEffect(solidJs.on(lang, () => languages.setSaveLang(lang()), { defer: true })); const locales = solidJs.createMemo(() => { switch (lang()) { case 'en': return en; case 'ru': return ru; default: return zh; } }); return (keys, variables) => { let text = byPath(locales(), keys) ?? ''; if (variables) for (const [k, v] of Object.entries(variables)) text = text.replaceAll(\`{{\${k}}}\`, \`\${String(v)}\`); return text; }; }); let publicOwner; solidJs.createRoot(() => { publicOwner = solidJs.getOwner(); }); /** 会自动设置 equals 的 createSignal */ const createEqualsSignal = (init, options) => solidJs.createSignal(init, { equals: isEqual, ...options }); /** 会自动设置 equals 和 createRoot 的 createMemo */ const createRootMemo = (fn, init, options) => { // 如果函数已经是 createMemo 创建的,就直接使用 if (fn.name === 'bound readSignal') return fn; const _init = init ?? fn(undefined); // 自动为对象类型设置 equals const _options = options?.equals === undefined && typeof _init === 'object' ? { ...options, equals: isEqual } : options; return solidJs.getOwner() ? solidJs.createMemo(fn, _init, _options) : solidJs.runWithOwner(publicOwner, () => solidJs.createMemo(fn, _init, _options)); }; /** 节流的 createMemo */ const createThrottleMemo = (fn, wait = 100, init = fn(undefined), options = undefined) => { const scheduled = createScheduled(_fn => throttle(_fn, wait)); return createRootMemo(prev => scheduled() ? fn(prev) : prev, init, options); }; const createMemoMap = fnMap => { const memoMap = Object.fromEntries(Object.entries(fnMap).map(([key, fn]) => [key, createRootMemo(fn)])); const map = createRootMemo(() => { const obj = {}; for (const key of Object.keys(memoMap)) Reflect.set(obj, key, memoMap[key]()); return obj; }); return map; }; const createRootEffect = (fn, val, options) => solidJs.getOwner() ? solidJs.createEffect(fn, val, options) : solidJs.runWithOwner(publicOwner, () => solidJs.createEffect(fn, val, options)); const createEffectOn = (deps, fn, options) => createRootEffect(solidJs.on(deps, fn, options)); const onAutoMount = fn => { const owner = solidJs.getOwner(); if (!owner) return fn(owner); solidJs.onMount(() => { const cleanFn = fn(owner); if (cleanFn) solidJs.onCleanup(cleanFn); }); }; const promisifyRequest = request => new Promise((resolve, reject) => { request.onsuccess = () => resolve(request.result); request.onerror = () => reject(request.error); }); const openDb = (name, version, initSchema) => new Promise((resolve, reject) => { const request = indexedDB.open(\`ComicReadScript\${name}\`, version); request.onupgradeneeded = () => initSchema(request.result); request.onsuccess = () => resolve(request.result); request.onerror = error => { console.error('数据库打开失败', error); reject(new Error('数据库打开失败')); }; }); const useCache = async (schema, name = '', version = 2) => { const db = await openDb(name, version, typeof schema === 'function' ? schema : db => { for (const storeName of db.objectStoreNames) if (!Reflect.has(schema, storeName)) db.deleteObjectStore(storeName); for (const storeName of Object.keys(schema)) { if (!db.objectStoreNames.contains(storeName)) db.createObjectStore(storeName, { keyPath: schema[storeName] }); } }); return { set: (storeName, value) => promisifyRequest(db.transaction(storeName, 'readwrite').objectStore(storeName).put(value)), get: (storeName, query) => promisifyRequest(db.transaction(storeName, 'readonly').objectStore(storeName).get(query)), del: (storeName, query) => promisifyRequest(db.transaction(storeName, 'readwrite').objectStore(storeName).delete(query)), each(storeName, callback) { const request = db.transaction(storeName, 'readwrite').objectStore(storeName).openCursor(); request.onsuccess = async function onsuccess(event) { const cursor = event.target.result; if (!cursor) return; await callback(cursor.value, cursor); cursor.continue(); }; } }; }; const createPointerState = (e, type = 'down') => { const xy = [e.clientX, e.clientY]; return { id: e.pointerId, type, xy, initial: xy, last: xy, startTime: performance.now(), target: e.target }; }; const useDrag = ({ ref, handleDrag, easyMode, handleClick, skip, setCapture, touches = new Map() }) => { onAutoMount(() => { const controller = new AbortController(); const options = { capture: false, passive: true, signal: controller.signal }; let allowClick = -1; const handleDown = e => { if (skip?.(e)) return; e.stopPropagation(); if (!easyMode?.() && e.buttons !== 1) return; if (setCapture) ref.setPointerCapture(e.pointerId); const state = createPointerState(e); touches.set(e.pointerId, state); handleDrag(state, e); // 在时限内松手才触发 click 事件 allowClick = window.setTimeout(() => { allowClick = 0; }, 300); }; const handleMove = e => { e.preventDefault(); if (!easyMode?.() && e.buttons !== 1) return; const state = touches.get(e.pointerId); if (!state) return; state.type = 'move'; state.xy = [e.clientX, e.clientY]; handleDrag(state, e); state.last = state.xy; // 拖拽一段距离后就不触发 click 了 if (allowClick > 0 && (Math.abs(e.clientX - state.initial[0]) > 5 || Math.abs(e.clientY - state.initial[1]) > 5)) { window.clearTimeout(allowClick); allowClick = -2; } }; const handleUp = e => { e.stopPropagation(); ref.releasePointerCapture(e.pointerId); const state = touches.get(e.pointerId); if (!state) return; touches.delete(e.pointerId); state.type = 'up'; state.xy = [e.clientX, e.clientY]; // 判断单击 if (handleClick && allowClick && touches.size === 0 && approx(state.xy[0] - state.initial[0], 0, 5) && approx(state.xy[1] - state.initial[1], 0, 5)) handleClick(e, state.target); window.clearTimeout(allowClick); handleDrag(state, e); }; const handleCancel = e => { e.stopPropagation(); ref.releasePointerCapture(e.pointerId); const state = touches.get(e.pointerId); if (!state) return; state.type = 'cancel'; handleDrag(state, e); touches.clear(); }; ref.addEventListener('pointerdown', handleDown, options); ref.addEventListener('pointermove', handleMove, { ...options, passive: false }); ref.addEventListener('pointerup', handleUp, options); ref.addEventListener('pointercancel', handleCancel, options); if (easyMode) { ref.addEventListener('pointerover', handleDown, options); ref.addEventListener('pointerout', handleUp, options); } ref.addEventListener('click', e => { if (allowClick > 0 && touches.size === 0 || skip?.(e)) return; e.stopPropagation(); e.preventDefault(); }, { capture: true }); return () => controller.abort(); }); }; const useStore = initState => { const [store$1, _setState] = store.createStore(initState); const setState = (...args) => { if (args.length === 1 && typeof args[0] === 'function') return _setState(store.produce(args[0])); return _setState(...args); }; return { store: store$1, setState }; }; const useStyleSheet = e => { const styleSheet = new CSSStyleSheet(); onAutoMount(() => { const root = e?.getRootNode() ?? document; root.adoptedStyleSheets = [...root.adoptedStyleSheets, styleSheet]; return () => { const index = root.adoptedStyleSheets.indexOf(styleSheet); if (index !== -1) root.adoptedStyleSheets.splice(index, 1); }; }); return styleSheet; }; const useStyle = (css, e) => { const styleSheet = useStyleSheet(e); if (typeof css === 'string') styleSheet.replaceSync(css);else createEffectOn(createRootMemo(css), style => styleSheet.replaceSync(style)); }; /** 用 CSSStyleSheet 实现和修改 style 一样的效果 */ const useStyleMemo = (selector, styleMapArg, e) => { const styleSheet = useStyleSheet(e); styleSheet.insertRule(\`\${selector} { }\`); const { style } = styleSheet.cssRules[0]; // 等火狐实现了 CSS Typed OM 后改用 styleMap 性能会更好,也能使用 CSS Typed OM 的 单位 const setStyle = (key, val) => { if (val === undefined || val === '') return style.removeProperty(key); style.setProperty(key, typeof val === 'string' ? val : \`\${val}\`); }; const styleMapList = Array.isArray(styleMapArg) ? styleMapArg : [styleMapArg]; for (const styleMap of styleMapList) { if (typeof styleMap === 'object') { for (const [key, val] of Object.entries(styleMap)) { const styleText = createRootMemo(val); createEffectOn(styleText, newVal => setStyle(key, newVal)); } } else { const styleMemoMap = createRootMemo(styleMap); createEffectOn(styleMemoMap, map => { for (const [key, val] of Object.entries(map)) setStyle(key, val); }); } } }; exports.AnimationFrame = AnimationFrame; exports.FaviconProgress = FaviconProgress; exports.WakeLock = WakeLock; exports.approx = approx; exports.assign = assign; exports.boolDataVal = boolDataVal; exports.byPath = byPath; exports.canvasToBlob = canvasToBlob; exports.clamp = clamp; exports.createEffectOn = createEffectOn; exports.createEqualsSignal = createEqualsSignal; exports.createMemoMap = createMemoMap; exports.createRootEffect = createRootEffect; exports.createRootMemo = createRootMemo; exports.createScheduled = createScheduled; exports.createThrottleMemo = createThrottleMemo; exports.debounce = debounce; exports.descRange = descRange; exports.difference = difference; exports.domParse = domParse; exports.extractRange = extractRange; exports.fileType = fileType; exports.getFileName = getFileName; exports.getGmValue = getGmValue; exports.getImageData = getImageData; exports.getKeyboardCode = getKeyboardCode; exports.getMostItem = getMostItem; exports.hijackFn = hijackFn; exports.inRange = inRange; exports.isEqual = isEqual; exports.isUrl = isUrl; exports.keyboardCodeToText = keyboardCodeToText; exports.lang = lang; exports.linstenKeydown = linstenKeydown; exports.log = log; exports.mountComponents = mountComponents; exports.needDarkMode = needDarkMode; exports.onAutoMount = onAutoMount; exports.onUrlChange = onUrlChange; exports.onec = onec; exports.plimit = plimit; exports.promisifyRequest = promisifyRequest; exports.querySelector = querySelector; exports.querySelectorAll = querySelectorAll; exports.querySelectorClick = querySelectorClick; exports.range = range; exports.requestIdleCallback = requestIdleCallback; exports.saveAs = saveAs; exports.scrollIntoView = scrollIntoView; exports.setInitLang = setInitLang; exports.setLang = setLang; exports.singleThreaded = singleThreaded; exports.sleep = sleep; exports.t = t; exports.testImgUrl = testImgUrl; exports.throttle = throttle; exports.useCache = useCache; exports.useDrag = useDrag; exports.useFaviconProgress = useFaviconProgress; exports.useStore = useStore; exports.useStyle = useStyle; exports.useStyleMemo = useStyleMemo; exports.wait = wait; exports.waitDom = waitDom; exports.waitImgLoad = waitImgLoad; exports.waitUrlChange = waitUrlChange; ` break; case 'request': code =` const Toast = require('components/Toast'); const helper = require('helper'); // 将 xmlHttpRequest 包装为 Promise const xmlHttpRequest = details => new Promise((resolve, reject) => { const handleError = error => { details.onerror?.(error); console.error('GM_xmlhttpRequest Error', error); reject(new Error(error?.responseText || 'GM_xmlhttpRequest Error')); }; const abort = GM_xmlhttpRequest({ ...details, onload(res) { details.onload?.call(res, res); resolve(res); }, onerror: handleError, ontimeout: handleError, onabort: handleError }); details.signal?.addEventListener('abort', abort.abort); }); /** 发起请求 */ const request = async (url, details = {}, retryNum = 0, errorNum = 0) => { const headers = { Referer: location.href }; const errorText = \`\${details?.errorText ?? helper.t('alert.comic_load_error')}\\nurl: \${url}\`; details.fetch ??= url.startsWith('/') || url.startsWith(location.origin); try { // 虽然 GM_xmlhttpRequest 有 fetch 选项,但在 stay 上不太稳定 // 为了支持 ios 端只能自己实现一下了 if (details.fetch) { const res = await fetch(url, { method: 'GET', headers, signal: AbortSignal.timeout?.(details.timeout ?? 1000 * 10), body: details.data, ...details }); if (!details.noCheckCode && res.status !== 200) { helper.log.error(errorText, res); throw new Error(errorText); } let response = null; switch (details.responseType) { case 'arraybuffer': response = await res.arrayBuffer(); break; case 'blob': response = await res.blob(); break; case 'json': response = await res.json(); break; } const _res = { status: res.status, statusText: res.statusText, response, responseText: response ? '' : await res.text() }; details.onload?.call(_res, _res); return _res; } if (typeof GM_xmlhttpRequest === 'undefined') throw new Error(helper.t('pwa.alert.userscript_not_installed')); let targetUrl = url; // https://github.com/hymbz/ComicReadScript/issues/195 // 在某些情况下 Tampermonkey 无法正确处理相对协议的 url // 实际 finalUrl 会变成 \`///xxx.xxx\` 莫名多了一个斜杠 // 然而在修改代码发出正确的请求后,就再也无法复现了 // 不过以防万一还是在这里手动处理下 if (url.startsWith('//')) targetUrl = \`http:\${url}\`; // stay 没法处理相对路径,也得转换一下 else if (url.startsWith('/')) targetUrl = \`\${location.origin}\${url}\`; const res = await xmlHttpRequest({ method: 'GET', url: targetUrl, headers, timeout: 1000 * 10, ...details }); if (!details.noCheckCode && res.status !== 200) { helper.log.error(errorText, res); throw new Error(errorText); } // stay 好像没有正确处理 json,只能再单独判断处理一下 if (details.responseType === 'json' && (typeof res.response !== 'object' || Object.keys(res.response).length === 0)) { try { Reflect.set(res, 'response', JSON.parse(res.responseText)); } catch {} } return res; } catch (error) { if (details && details.retryFetch && retryNum === 0) { console.warn('retryFetch', url); details.fetch = !details.fetch; if (typeof details.retryFetch === 'function') details.retryFetch(details); return request(url, details, retryNum + 1, errorNum); } if (errorNum >= retryNum) { (details.noTip ? console.error : Toast.toast.error)(\`\${errorText}\\nerror: \${error.message}\`); throw new Error(errorText); } helper.log.error(errorText, error); await helper.sleep(1000); return request(url, details, retryNum, errorNum + 1); } }; /** 轮流向多个 api 发起请求 */ const eachApi = async (url, baseUrlList, details) => { for (const baseUrl of baseUrlList) { try { return await request(\`\${baseUrl}\${url}\`, { ...details, noTip: true }); } catch {} } const errorText = details?.errorText ?? helper.t('alert.comic_load_error'); if (!details?.noTip) Toast.toast.error(errorText); helper.log.error('所有 api 请求均失败', url, baseUrlList, details); throw new Error(errorText); }; exports.eachApi = eachApi; exports.request = request; ` break; case 'components/Manga': code =` const web = require('solid-js/web'); const solidJs = require('solid-js'); const helper = require('helper'); const request = require('request'); const Comlink = require('comlink'); const store$1 = require('solid-js/store'); const worker = require('worker/ImageRecognition'); const Toast = require('components/Toast'); const worker$1 = require('worker/ImageUpscale'); const imgState = { imgMap: {}, imgList: [], pageList: [], fillEffect: { '-1': true }, showRange: [0, 0], renderRange: [0, 0], loadingRange: [0, 0], defaultImgType: '' }; const _defaultOption = { dir: 'rtl', scrollbar: { position: 'auto', autoHidden: false, showImgStatus: true, easyScroll: false }, clickPageTurn: { enabled: 'ontouchstart' in document.documentElement, reverse: false, area: 'left_right' }, firstPageFill: true, disableZoom: false, darkMode: false, autoDarkMode: false, swapPageTurnKey: false, scroolEnd: 'auto', alwaysLoadAllImg: false, showComment: true, preloadPageNum: 20, pageNum: 0, autoSwitchPageMode: true, autoHiddenMouse: true, autoFullscreen: false, zoom: { ratio: 100, offset: { x: 0, y: 0 } }, scrollMode: { enabled: false, spacing: 0, imgScale: 1, fitToWidth: false, abreastMode: false, abreastDuplicate: 0.1, doubleMode: false }, imgRecognition: { enabled: false, background: true, pageFill: true, upscale: false }, translation: { server: 'disable', localUrl: undefined, forceRetry: false, // 一些参数没有使用默认值,而是直接使用文档的推荐值 // https://github.com/zyddnys/manga-image-translator?tab=readme-ov-file#recommended-modules options: { detector: { detector: 'ctd', detection_size: '1536', box_threshold: 0.7, unclip_ratio: 2.3 }, render: { direction: 'auto', font_size_offset: 4 }, translator: { translator: 'chatgpt', target_lang: { zh: 'CHS', en: 'ENG', ru: 'RUS' }[helper.lang()] ?? 'CHS' }, inpainter: { inpainter: 'lama_large', inpainting_size: '2048' }, mask_dilation_offset: 30 }, onlyDownloadTranslated: false }, autoScroll: { enabled: false, interval: 3000, distance: 200, triggerEnd: false } }; const defaultOption = () => structuredClone(_defaultOption); const optionState = { defaultOption: defaultOption(), option: defaultOption() }; const otherState = { title: '', /** * 用于防止滚轮连续滚动导致过快触发事件的锁 * * - 在首次触发结束页时开启,一段时间关闭。开启时禁止触发结束页的上下话切换功能。 */ scrollLock: false, /** 当前是否处于全屏状态 */ fullscreen: false, rootSize: { width: 0, height: 0 }, scrollbarSize: { width: 0, height: 0 }, autoScroll: { play: false, progress: 0 }, supportWorker: false, supportUpscaleImage: true }; const propState = { commentList: undefined, hotkeys: {}, prop: { onExit: undefined, onPrev: undefined, onNext: undefined, onLoading: undefined, onOptionChange: undefined, onHotkeysChange: undefined, editButtonList: list => list, editSettingList: list => list } }; const showState = { isMobile: false, isDragMode: false, activePageIndex: 0, gridMode: false, show: { toolbar: false, scrollbar: false, touchArea: false, endPage: undefined }, page: { anima: '', vertical: false, offset: { x: { pct: 0, px: 0 }, y: { pct: 0, px: 0 } } } }; const initStore = { ...imgState, ...showState, ...propState, ...optionState, ...otherState }; const { store, setState } = helper.useStore({ ...initStore }); const refs = { root: undefined, mangaBox: undefined, mangaFlow: undefined, touchArea: undefined, scrollbar: undefined, settingPanel: undefined, // 结束页上的按钮 prev: undefined, next: undefined, exit: undefined }; // 1. 因为不同汉化组处理情况不同不可能全部适配,所以只能是尽量适配*出现频率更多*的情况 /** 判断图片是否是跨页图 */ const isWideImg = img => { switch (img.type ?? store.defaultImgType) { case 'long': case 'wide': return true; default: return false; } }; /** 根据填充页设置双页排列单页图片 */ const arrangeImg = (pageList, fill) => { if (pageList.length === 0) return []; const newPageList = []; let imgCache = fill ? [-1] : []; for (const i of pageList) { imgCache.push(i); if (imgCache.length === 2) { newPageList.push(imgCache); imgCache = []; } } if (imgCache.length === 1 && imgCache[0] !== -1) { imgCache.push(-1); newPageList.push(imgCache); } return newPageList; }; /** 计算指定图片流中的左右页位置正确的页数 */ const computeAccuracy = (imgList, pageList) => { let accuracy = 0; for (const [a, b] of pageList) { if ((imgList[a]?.blankMargin?.left ?? 0) > 0.04) accuracy += 1; if (b === undefined) break; if ((imgList[b]?.blankMargin?.right ?? 0) > 0.04) accuracy += 1; } return accuracy; }; /** 自动切换填充页设置到左右页正确率更高的情况 */ const arrangePage = (pageList, { imgList, fillEffect, nowFillIndex, switchFill }) => { const fill = Boolean(fillEffect[nowFillIndex]); const newPageList = arrangeImg(pageList, fill); if (!switchFill || typeof fillEffect[nowFillIndex] === 'number') return newPageList; const anotherPageList = arrangeImg(pageList, !fill); const anotherAccuracy = computeAccuracy(imgList, anotherPageList); if (anotherAccuracy === 0) return newPageList; const nowAccuracy = computeAccuracy(imgList, newPageList); if (anotherAccuracy <= nowAccuracy) return newPageList; helper.log(\`\${nowFillIndex} 自动切换页面填充\`); fillEffect[nowFillIndex] = !fill; return anotherPageList; }; /** 根据图片比例和填充页设置对漫画图片进行排列 */ const handleComicData = (imgList, fillEffect, switchFill) => { const context = { imgList, fillEffect, nowFillIndex: -1, switchFill }; const pageList = []; const cacheList = []; for (let i = 0; i < imgList.length; i += 1) { const img = imgList[i]; if (!isWideImg(img)) { cacheList.push(i); if (Reflect.has(fillEffect, i)) Reflect.deleteProperty(fillEffect, i); continue; } // 在除结尾(可能是汉化组图)外的位置出现了跨页图的话,那张跨页图大概率是页序的「正确答案」 // 如果这张跨页导致了上面一页缺页,就说明在这之前的填充有误,应该据此调整之前的填充 if (typeof fillEffect[context.nowFillIndex] === 'boolean' && i < imgList.length - 2 && (cacheList.length + (fillEffect[context.nowFillIndex] ? 1 : 0)) % 2 === 1) { fillEffect[context.nowFillIndex] = !fillEffect[context.nowFillIndex]; return handleComicData(imgList, fillEffect, switchFill); } pageList.push(...arrangePage(cacheList, context), [i]); cacheList.length = 0; if (fillEffect[i] === undefined) fillEffect[i] = false; context.nowFillIndex = i; } if (cacheList.length > 0) pageList.push(...arrangePage(cacheList, context)); return pageList; }; const getImg = (i, state = store) => state.imgMap[state.imgList[i]]; /** 找到指定 url 图片在 imgList 里的 index */ const getImgIndexs = url => { const indexList = []; for (const [i, imgUrl] of store.imgList.entries()) if (imgUrl === url) indexList.push(i); return indexList; }; /** 找到指定 url 图片的 dom */ const getImgEle = url => refs.mangaFlow.querySelector(\`img[data-src="\${url}"]\`); /** 找到指定页面所处的图片流 */ const findFillIndex = (pageIndex, fillEffect) => { let nowFillIndex = pageIndex; while (!Reflect.has(fillEffect, nowFillIndex)) nowFillIndex -= 1; return nowFillIndex; }; /** 触发 onOptionChange */ const triggerOnOptionChange = helper.throttle(() => store.prop.onOptionChange?.(helper.difference(store.option, store.defaultOption)), 1000); /** 在 option 后手动触发 onOptionChange */ const setOption = fn => { setState(state => fn(state.option, state)); triggerOnOptionChange(); }; /** 创建用于将 ref 绑定到对应 state 上的工具函数 */ const bindRef = name => e => Reflect.set(refs, name, e); const watchDomSize = (name, e) => { const resizeObserver = new ResizeObserver(([{ contentRect }]) => { if (!contentRect.width || !contentRect.height) return; setState(state => { state[name] = { width: contentRect.width, height: contentRect.height }; }); }); resizeObserver.disconnect(); resizeObserver.observe(e); solidJs.onCleanup(() => resizeObserver.disconnect()); }; /** 将界面恢复到正常状态 */ const resetUI = state => { state.show.toolbar = false; state.show.scrollbar = false; state.show.touchArea = false; }; const bindOption$1 = (...path) => ({ value: helper.byPath(store.option, path), onChange: val => setOption(draftOption => helper.byPath(draftOption, path, () => val)) }); const imgList = helper.createRootMemo(() => store.imgList.map(url => store.imgMap[url])); /** 当前是否为并排卷轴模式 */ const isAbreastMode = helper.createRootMemo(() => store.option.scrollMode.enabled && store.option.scrollMode.abreastMode); /** 当前是否为双页卷轴模式 */ const isDoubleMode = helper.createRootMemo(() => store.option.scrollMode.enabled && store.option.scrollMode.doubleMode); /** 当前是否为普通卷轴模式(包含了双页卷轴模式) */ const isScrollMode = helper.createRootMemo(() => store.option.scrollMode.enabled && !store.option.scrollMode.abreastMode); /** 当前是否开启了识别背景色 */ const isEnableBg = helper.createRootMemo(() => store.option.imgRecognition.enabled && store.option.imgRecognition.background); /** 当前是否开启了图像放大 */ const isUpscale = helper.createRootMemo(() => !store.isMobile && store.option.imgRecognition.enabled && store.option.imgRecognition.upscale); /** 当前显示页面 */ const activePage = helper.createRootMemo(() => store.pageList[store.activePageIndex] ?? []); /** 当前显示的第一张图片的 index */ const activeImgIndex = helper.createRootMemo(() => activePage().find(i => i !== -1) ?? 0); /** 当前所处的图片流 */ const nowFillIndex = helper.createRootMemo(() => findFillIndex(activeImgIndex(), store.fillEffect)); /** 预加载页数 */ const preloadNum = helper.createRootMemo(() => ({ back: store.option.preloadPageNum, front: Math.floor(store.option.preloadPageNum / 2) })); /** 获取图片列表中指定属性的中位数 */ const getImgMedian = sizeFn => { const list = imgList().filter(img => img.loadType === 'loaded' && img.width).map(sizeFn).sort((a, b) => a - b); // 因为涉及到图片默认类型的计算,所以至少等到加载完三张图片再计算,避免被首页大图干扰 if (list.length < 3) return null; return list[Math.floor(list.length / 2)]; }; /** 图片占位尺寸 */ const placeholderSize = helper.createThrottleMemo(() => ({ width: getImgMedian(img => img.width) ?? 800, height: getImgMedian(img => img.height) ?? 1200 }), 500); /** 并排卷轴模式下的列宽度 */ const abreastColumnWidth = helper.createRootMemo(() => isAbreastMode() ? placeholderSize().width * store.option.scrollMode.imgScale : 0); const autoPageNum = helper.createThrottleMemo(() => store.rootSize.width >= store.rootSize.height ? 2 : 1); const pageNum = helper.createRootMemo(() => store.option.pageNum || autoPageNum()); /** 是否为单页模式 */ const isOnePageMode = helper.createRootMemo(() => { if (store.isMobile || store.imgList.length <= 1) return true; if (store.option.scrollMode.enabled) { if (store.option.scrollMode.abreastMode) return true; return !store.option.scrollMode.doubleMode; } return pageNum() === 1; }); /** 并排卷轴模式下的全局滚动填充 */ const [abreastScrollFill, _setAbreastScrollFill] = solidJs.createSignal(0); /** 并排卷轴模式下的每列布局 */ const abreastArea = helper.createRootMemo(prev => { if (!isAbreastMode()) return prev; const columns = [[]]; const position = {}; let length = 0; const rootHeight = store.rootSize.height; if (!rootHeight || store.imgList.length === 0) return { columns, position, length }; const repeatHeight = rootHeight * store.option.scrollMode.abreastDuplicate; /** 当前图片在当前列的所在高度 */ let top = abreastScrollFill(); while (top > rootHeight) { top -= rootHeight - repeatHeight; columns.push([]); } for (let i = 0; i < store.imgList.length; i++) { const img = getImg(i); const imgPosition = []; const imgHeight = img.size.height; length += imgHeight; let height = imgHeight; while (height > 0) { columns.at(-1).push(i); imgPosition.push({ column: columns.length - 1, top }); if (top < 0 && imgPosition.length > 1) top = 0; const availableHeight = rootHeight - top; top += height; height -= availableHeight; // 填满一列后换行 if (top < rootHeight) continue; columns.push([]); top = height - imgHeight; // 复现上列结尾 if (!repeatHeight || columns.length === 1) continue; top += repeatHeight; height = Math.min(imgHeight, height + repeatHeight); /** 为了复现而出现的空白部分高度 */ let emptyTop = top; let prevImgIndex = i; while (prevImgIndex >= 1 && emptyTop > 0) { prevImgIndex -= 1; // 把上一张图片加进来填补空白 columns.at(-1).push(prevImgIndex); const prevImgHeight = getImg(prevImgIndex).size.height; emptyTop -= prevImgHeight; position[prevImgIndex].push({ column: columns.length - 1, top: emptyTop }); } } position[i] = imgPosition; } return { columns, position, length }; }, { columns: [], position: {}, length: 0 }); /** 头尾滚动的限制值 */ const scrollFillLimit = helper.createRootMemo(() => abreastArea().length - store.rootSize.height); const setAbreastScrollFill = val => _setAbreastScrollFill(helper.clamp(-scrollFillLimit(), val, scrollFillLimit())); /** 并排卷轴模式下当前要显示的列 */ const abreastShowColumn = helper.createThrottleMemo(() => { if (!isAbreastMode() || abreastArea().columns.length === 0) return { start: 0, end: 0 }; const columnWidth = abreastColumnWidth() + store.option.scrollMode.spacing * 7; return { start: helper.clamp(0, Math.floor(store.page.offset.x.px / columnWidth), abreastArea().columns.length - 1), end: helper.clamp(0, Math.floor((store.page.offset.x.px + store.rootSize.width) / columnWidth), abreastArea().columns.length - 1) }; }); /** 并排卷轴模式下的漫画流宽度 */ const abreastContentWidth = helper.createRootMemo(() => abreastArea().columns.length * abreastColumnWidth() + (abreastArea().columns.length - 1) * store.option.scrollMode.spacing * 7); /** 并排卷轴模式下的最大滚动距离 */ const abreastScrollWidth = helper.createRootMemo(() => abreastContentWidth() - store.rootSize.width); /** 并排卷轴模式下每个图片所在位置的样式 */ const imgAreaStyle = helper.createRootMemo(() => { if (!isAbreastMode() || store.gridMode) return ''; let styleText = ''; for (const index of store.imgList.keys()) { let imgNum = 0; for (const { column, top } of abreastArea().position[index] ?? []) { const itemStyle = \`grid-area: _\${column} !important; transform: translateY(\${top}px);\`; styleText += \`#_\${index}_\${imgNum} { \${itemStyle} }\\n\`; imgNum += 1; } } return styleText; }); const [defaultHotkeys, setDefaultHotkeys] = solidJs.createSignal({ scroll_up: ['w', 'ArrowUp'], scroll_down: ['s', 'ArrowDown', ' '], scroll_left: ['a', 'Shift + a', ',', 'ArrowLeft'], scroll_right: ['d', 'Shift + d', '.', 'ArrowRight'], page_up: ['PageUp', 'Shift + w'], page_down: [' ', 'PageDown', 'Shift + s'], jump_to_home: ['Home'], jump_to_end: ['End'], exit: ['Escape'], switch_page_fill: ['/', 'm', 'z'], switch_scroll_mode: [], switch_grid_mode: [], switch_single_double_page_mode: [], switch_dir: [], switch_auto_enlarge: [], translate_current_page: [], translate_all: [], translate_to_end: [], fullscreen: [], auto_scroll: [] }); /** 快捷键配置 */ const hotkeysMap = helper.createRootMemo(() => Object.fromEntries(Object.entries(store.hotkeys).flatMap(([name, key]) => key.map(k => [k, name])))); /** 重新计算图片排列 */ const updatePageData = state => { const lastActiveImgIndex = activeImgIndex(); let newPageList = []; newPageList = isOnePageMode() ? state.imgList.map((_, i) => [i]) : handleComicData(state.imgList.map(url => state.imgMap[url]), state.fillEffect, state.option.imgRecognition.pageFill); if (helper.isEqual(state.pageList, newPageList)) return; state.pageList = newPageList; // 在图片排列改变后自动跳转回原先显示图片所在的页数 if (lastActiveImgIndex !== activeImgIndex()) { const newActivePageIndex = state.pageList.findIndex(page => page.includes(lastActiveImgIndex)); if (newActivePageIndex !== -1) state.activePageIndex = newActivePageIndex; } }; updatePageData.throttle = helper.throttle(() => setState(updatePageData), 100); /** * 将处理图片的相关变量恢复到初始状态 * * 必须按照以下顺序调用 * 1. 修改 imgList * 2. resetImgState * 3. updatePageData */ const resetImgState = state => { if (state.imgList.length === 0) { state.fillEffect = { '-1': true }; return; } // 如果用户没有手动修改过首页填充,才将其恢复初始 if (typeof state.fillEffect['-1'] === 'boolean') state.fillEffect['-1'] = state.option.firstPageFill && state.imgList.length > 3; }; helper.createEffectOn([pageNum, isOnePageMode], () => setState(updatePageData)); /** 阻止事件冒泡 */ const stopPropagation = e => { e.stopPropagation(); }; /** 从头开始播放元素的动画 */ const playAnimation = e => { if (!e) return; for (const animation of e.getAnimations()) { animation.cancel(); animation.play(); } }; const downloadImgHeaders = { Accept: 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', 'User-Agent': navigator.userAgent, Referer: location.href }; const downloadImg = async (imgUrl, details) => { const url = store.imgMap[imgUrl]?.blobUrl ?? imgUrl; if (url.startsWith('blob:')) { const res = await fetch(url); return res.blob(); } const res = await request.request(url, { responseType: 'blob', errorText: helper.t('translation.tip.download_img_failed'), headers: downloadImgHeaders, retryFetch: true, ...details }); if (Reflect.has(store.imgMap, imgUrl)) setState('imgMap', imgUrl, 'blobUrl', URL.createObjectURL(res.response)); return res.response; }; const handleImgRecognition = async (url, imgEle) => { const img = store.imgMap[url]; const needRecognition = store.option.imgRecognition.background && img.background === undefined || store.option.imgRecognition.pageFill && img.blankMargin === undefined; if (needRecognition) { imgEle ??= await helper.wait(() => getImgEle(url), 1000); if (!imgEle) return helper.log.warn('获取图片元素失败'); const { data, width, height } = helper.getImageData(imgEle); initWorker$1(); return worker.recognitionImg(Comlink.transfer(data, [data.buffer]), width, height, url, store$1.unwrap(store.option.imgRecognition)); } }; const initWorker$1 = helper.onec(() => { const mainFn = { log: helper.log, updatePageData: helper.throttle(() => setState(updatePageData), 1000), setImg: (url, key, val) => Reflect.has(store.imgMap, url) && setState('imgMap', url, key, val) }; worker.setMainFn(Comlink.proxy(mainFn), Object.keys(mainFn)); }); /** 记录每张图片所在的页面 */ const imgPageMap = helper.createRootMemo(() => { const map = {}; for (let i = 0; i < store.pageList.length; i++) { for (const imgIndex of store.pageList[i]) if (imgIndex !== -1) map[imgIndex] = i; } return map; }); const [_scrollTop, setScrollTop] = solidJs.createSignal(0); /** 卷轴模式下的滚动距离 */ const scrollModTop = _scrollTop; /** 滚动距离 */ const scrollTop = helper.createRootMemo(() => isAbreastMode() ? store.page.offset.x.px : scrollModTop()); const bindScrollTop = dom => { dom.addEventListener('scroll', () => setScrollTop(dom.scrollTop), { passive: true }); }; // 自动切换黑暗模式 const darkModeQuery = matchMedia('(prefers-color-scheme: dark)'); const autoSwitchDarkMode = query => { if (!store.option.autoDarkMode) return; if (query.matches === store.option.darkMode) return; setState('option', 'darkMode', query.matches); }; darkModeQuery.addEventListener('change', autoSwitchDarkMode); autoSwitchDarkMode(darkModeQuery); helper.createEffectOn(() => store.option.autoDarkMode, () => autoSwitchDarkMode(darkModeQuery)); // 窗口宽度小于800像素时,标记为移动端 helper.createEffectOn(() => store.rootSize.width, width => { const isMobile = helper.inRange(1, width, 800); if (isMobile === store.isMobile) return; setState(state => { state.isMobile = isMobile; resetImgState(state); updatePageData(state); }); }); const isWideType = type => type === 'wide' || type === 'long'; // https://github.com/hymbz/ComicReadScript/issues/174#issuecomment-2252114640 // 用于判断图片类型的比例 const 单页比例 = 1920 / 2 / 1080; const 横幅比例 = 1920 / 1080; const 条漫比例 = 1920 / 2 / 1080 / 2; /** 根据比例判断图片类型 */ const getImgType = img => { const imgRatio = img.width / img.height; if (imgRatio <= 单页比例) return imgRatio < 条漫比例 ? 'vertical' : ''; return imgRatio > 横幅比例 ? 'long' : 'wide'; }; /** 更新图片类型。返回是否修改了图片类型 */ const updateImgType = (state, draftImg) => { const { type } = draftImg; if (!draftImg.width || !draftImg.height) return false; draftImg.type = getImgType(draftImg); if (isWideType(type) !== isWideType(draftImg.type)) updatePageData.throttle(); return (type ?? state.defaultImgType) !== draftImg.type; }; /** 是否自动开启过卷轴模式 */ let autoScrollMode = false; helper.createRootEffect(prevIsWide => { if (store.rootSize.width === 0 || store.rootSize.height === 0) return; const defaultImgType = getImgType(placeholderSize()); if (defaultImgType === store.defaultImgType) return prevIsWide; const isWide = isWideType(defaultImgType); setState(state => { state.defaultImgType = defaultImgType; // 连续出现多张长图后,自动开启卷轴模式 if (defaultImgType === 'vertical' && !autoScrollMode && !state.option.scrollMode.enabled) { state.option.scrollMode.enabled = true; autoScrollMode = true; return; } if (isWide !== prevIsWide) updatePageData(state); }); return isWide; }, false); /** 获取指定图片的显示尺寸 */ const getImgDisplaySize = (state, img) => { let height = img.height ?? placeholderSize().height; let width = img.width ?? placeholderSize().width; if (!state.option.scrollMode.enabled) return { height, width }; const setWidth = w => { height *= w / width; width = w; return { height, width }; }; if (isAbreastMode()) return setWidth(abreastColumnWidth()); if (state.option.scrollMode.fitToWidth) return setWidth(state.rootSize.width); height *= state.option.scrollMode.imgScale; width *= state.option.scrollMode.imgScale; if (width > state.rootSize.width) return setWidth(state.rootSize.width); return { height, width }; }; /** 更新图片尺寸 */ const updateImgSize = (url, width, height) => setState(state => { const img = state.imgMap[url]; if (img.width === width && img.height === height) return; img.width = width; img.height = height; img.size = getImgDisplaySize(state, img); updateImgType(state, img); }); helper.createEffectOn([imgList, () => store.option.scrollMode.enabled, () => store.option.scrollMode.abreastMode, () => store.option.scrollMode.fitToWidth, () => store.option.scrollMode.imgScale, () => store.rootSize, placeholderSize], ([{ length }]) => { if (length === 0) return; setState(state => { for (const url of state.imgList) state.imgMap[url].size = getImgDisplaySize(state, state.imgMap[url]); }); }); /** 卷轴模式下每张图片的位置 */ const imgTopList = helper.createRootMemo(() => { if (!isScrollMode()) return []; const list = Array.from({ length: store.imgList.length }); let top = 0; for (let i = 0; i < store.imgList.length; i++) { list[i] = top; top += getImg(i).size.height + store.option.scrollMode.spacing * 7; } return list; }); /** 卷轴模式下漫画流的总高度 */ const contentHeight = helper.createRootMemo(() => store.option.scrollMode.enabled ? (imgTopList().at(-1) ?? 0) + (imgList().at(-1)?.size.height ?? 0) : 0); /** 双页卷轴模式下的每行高度 */ const doubleScrollLineHeight = helper.createRootMemo(() => { if (!isDoubleMode()) return []; const doubleWidth = store.rootSize.width / 2; return store.pageList.map(indexs => { if (indexs.length === 1) return getImg(indexs[0]).size.height; // 选择更高的那张图片作为行高度,尽量放大图片 let targetImg; for (const i of indexs) { if (i === -1) continue; const img = getImg(i); if (!targetImg || img.size.height > targetImg.size.height) targetImg = img; } if (!targetImg) throw new Error('找不到图片'); if (targetImg.size.width < doubleWidth && !store.option.scrollMode.fitToWidth) return targetImg.size.height; return targetImg.size.height * (doubleWidth / targetImg.size.width); }); }); // /** 预加载图片尺寸 */ // const preloadImgSize = singleThreaded(async () => { // let index = 0; // for (; index < store.imgList.length; index++) { // const img = store.imgList[index]; // if (img.size === undefined) continue; // const size = await getImgSize(img.src); // if (!size) continue; // // 防止加载过程中 imgList 变了的情况 // if (store.imgList[index].src !== img.src) break; // // eslint-disable-next-line @typescript-eslint/no-loop-func // setState((state) => updateImgSize(state, index, ...size)); // } // // if (index < store.imgList.length) requestIdleCallback(preloadImgSize); // }); // // 空闲期间预加载所有图片的尺寸 // 卷轴模式下需要提前知道尺寸方便正确布局 // 翻页模式下也需要提前发现跨页图重新排序 // requestIdleCallback(preloadImgSize); /** 滚动内容的总长度 */ const scrollLength = helper.createRootMemo(() => { if (store.option.scrollMode.enabled) { if (store.option.scrollMode.abreastMode) return abreastContentWidth(); if (store.option.scrollMode.doubleMode) return doubleScrollLineHeight().reduce((sum, height) => sum + height, 0); return contentHeight(); } return store.pageList.length; }); /** 滚动内容的滚动进度 */ const scrollProgress = helper.createRootMemo(() => { if (isScrollMode()) return scrollTop(); if (isAbreastMode()) return store.page.offset.x.px; return store.activePageIndex; }); /** 滚动内容的滚动进度百分比 */ const scrollPercentage = helper.createRootMemo(() => scrollProgress() / scrollLength()); /** 滚动条滑块长度 */ const sliderHeight = helper.createRootMemo(() => { let itemLength = 1; if (isScrollMode()) itemLength = store.rootSize.height; if (isAbreastMode()) itemLength = store.rootSize.width; return itemLength / scrollLength(); }); /** 当前是否已经滚动到底部 */ const isBottom = helper.createRootMemo(() => scrollPercentage() + sliderHeight() >= 0.9999); /** 当前是否已经滚动到顶部 */ const isTop = helper.createRootMemo(() => scrollPercentage() === 0); const _scrollTo = x => refs.mangaBox.scrollTo({ top: x, behavior: 'instant' }); /** 实现卷轴模式下的平滑滚动 */ const scrollStep = new class extends helper.AnimationFrame { /** 动画时长 */ duration = 100; /** 要滚动的距离 */ distance = 0; /** 滚动开始时间 */ startTime = 0; /** 滚动开始位置 */ startTop = 0; frame = timestamp => { this.cancel(); this.startTime ||= timestamp; /** 已滚动时间 */ const elapsed = timestamp - this.startTime; if (elapsed >= this.duration) return _scrollTo(this.startTop + this.distance); _scrollTo(this.startTop + elapsed / this.duration * this.distance); this.call(); }; start = x => { this.startTime = 0; this.startTop = scrollTop(); this.distance = x - this.startTop; this.frame(0); }; }(); /** 实现卷轴模式下的匀速滚动 */ const constantScroll = new class extends helper.AnimationFrame { speed = 0; lastTime = 0; frame = timestamp => { if (!this.animationId) return; if (this.lastTime) { const scrollDelta = this.speed * (timestamp - this.lastTime); _scrollTo(scrollTop() + scrollDelta); } this.lastTime = timestamp; this.call(); }; start = speed => { if (this.animationId && speed === this.speed) return; this.cancel(); this.speed = speed; this.lastTime = 0; this.call(); }; }(); /** 在卷轴模式下滚动到指定进度 */ const scrollTo = (x, smooth = false) => { if (!store.option.scrollMode.enabled) return; if (store.option.scrollMode.abreastMode) { _scrollTo(0); const val = helper.clamp(0, x, abreastScrollWidth()); return setState('page', 'offset', 'x', 'px', val); } if (!smooth) { scrollStep.cancel(); _scrollTo(x); return; } if (scrollStep.animationId) { scrollStep.cancel(); _scrollTo(x); } scrollStep.start(x); }; /** 保存当前滚动进度,并在之后恢复 */ const saveScrollProgress = () => { const oldScrollPercentage = scrollPercentage(); return () => scrollTo(oldScrollPercentage * scrollLength()); }; /** 在卷轴模式下,滚动到能显示指定图片的位置 */ const scrollViewImg = i => { if (!store.option.scrollMode.enabled) return; let top; if (store.option.scrollMode.abreastMode) { const columnNum = abreastArea().columns.findIndex(column => column.includes(i)); top = columnNum * abreastColumnWidth() + 1; } else if (store.option.scrollMode.doubleMode) { const pageNum = imgPageMap()[i]; top = doubleScrollLineHeight().slice(0, pageNum).reduce((sum, height) => sum + height, 0); } else top = imgTopList()[i] + 1; scrollTo(top); }; /** 跳转到指定图片的显示位置 */ const jumpToImg = index => { if (store.option.scrollMode.enabled) return scrollViewImg(index); const pageNum = imgPageMap()[index]; if (pageNum === undefined) return; setState(state => { state.activePageIndex = pageNum; state.gridMode = false; }); }; /** 在卷轴模式下进行缩放,并且保持滚动进度不变 */ const zoomScrollModeImg = (zoomLevel, set = false) => { const jump = saveScrollProgress(); setOption(draftOption => { const newVal = set ? zoomLevel : store.option.scrollMode.imgScale + zoomLevel; draftOption.scrollMode.imgScale = helper.clamp(0.1, Number(newVal.toFixed(2)), 3); }); jump(); // 并排卷轴模式下并没有一个明确直观的滚动进度, // 也想不出有什么实现效果能和普通卷轴模式的效果一致, // 所以就摆烂不管了,反正现在这样也已经能避免乱跳了 }; /** 找到普通卷轴模式下指定高度上的图片 */ const findTopImg = (top, initIndex = 0) => { if (top > contentHeight()) return imgTopList().length - 1; let i = initIndex; for (; i < imgTopList().length; i++) if (imgTopList()[i] > top) return i === 0 ? 0 : i - 1; return imgTopList().length - 1; }; /** 获取并排卷轴模式下指定列的指定图片 */ const getAbreastColumnImg = (column, img) => { const { columns } = abreastArea(); return columns[helper.clamp(0, column, columns.length - 1)]?.at(img) ?? 0; }; /** 计算显示页面 */ const updateShowRange = state => { if (scrollLength() === 0) { state.showRange = [0, 0]; state.renderRange = state.showRange; } else if (!state.option.scrollMode.enabled) { // 翻页模式 state.showRange = [state.activePageIndex, state.activePageIndex]; state.renderRange = [helper.clamp(0, state.activePageIndex - 1, state.pageList.length - 1), helper.clamp(0, state.activePageIndex + 1, state.pageList.length - 1)]; } else if (state.option.scrollMode.abreastMode) { // 并排卷轴模式 const { start, end } = abreastShowColumn(); state.showRange = [getAbreastColumnImg(start, 0), getAbreastColumnImg(end, -1)]; state.renderRange = [getAbreastColumnImg(start - 2, 0), getAbreastColumnImg(end + 2, -1)]; } else if (state.option.scrollMode.doubleMode) { // 双页卷轴模式 const top = scrollTop(); const bottom = scrollTop() + state.rootSize.height; const renderTop = top - state.rootSize.height; const rednerBottom = bottom + state.rootSize.height; state.showRange = [-1, -1]; state.renderRange = [-1, -1]; let height = 0; for (const [pageIndex, lineHeight] of doubleScrollLineHeight().entries()) { height += lineHeight; if (state.renderRange[0] === -1) { if (height >= renderTop) state.renderRange[0] = pageIndex;else continue; } if (state.showRange[0] === -1) { if (height >= top) state.showRange[0] = pageIndex;else continue; } if (state.showRange[1] === -1) { if (height >= bottom) state.showRange[1] = pageIndex;else continue; } if (state.renderRange[1] === -1) { if (height >= rednerBottom) state.renderRange[1] = pageIndex;else continue; } break; } if (state.renderRange[1] === -1) state.renderRange[1] = state.pageList.length - 1; if (state.showRange[1] === -1) state.showRange[1] = state.pageList.length - 1; } else { // 普通卷轴模式 const top = scrollTop(); const bottom = scrollTop() + state.rootSize.height; const renderTop = top - state.rootSize.height; const rednerBottom = bottom + state.rootSize.height; const renderTopImg = findTopImg(renderTop); const topImg = findTopImg(top, renderTopImg); const bottomImg = findTopImg(bottom, topImg); const renderBottomImg = findTopImg(rednerBottom, bottomImg); state.showRange = [topImg, bottomImg]; state.renderRange = [renderTopImg, renderBottomImg]; } }; helper.createEffectOn([scrollLength, () => store.gridMode, () => store.option.scrollMode.enabled, () => store.activePageIndex, () => store.option.scrollMode.abreastMode, () => store.rootSize, abreastShowColumn, scrollTop], helper.throttle(() => setState(updateShowRange)) // 两种卷轴模式下都可以通过在每次滚动后记录 // 当前 \`显示的第一张图片的 bottom\` 和 \`最后一张图片的 top\` 作为忽略范围, // 在每次滚动后检查是否超出了这个范围,没超出就说明本次滚动不会显示或消失任何图片 // 以此进行性能优化 // 不过两个卷轴模式都要这么处理挺麻烦的,姑且先用 throttle 顶上,后面有需要再优化 ); /** 获取指定范围内页面所包含的图片 */ const getRangeImgList = range => { let list; if (range[0] === range[1]) list = new Set(store.pageList[range[0]]);else { list = new Set(); for (const [a, b] of store.pageList.slice(range[0], range[1] + 1)) { list.add(a); if (b !== undefined) list.add(b); } } list.delete(-1); return list; }; const renderImgList = helper.createRootMemo(() => getRangeImgList(store.renderRange)); const showImgList = helper.createRootMemo(() => getRangeImgList(store.showRange)); /** * 图片显示状态 * * 0 - 页面中的第一张图片 * 1 - 页面中的最后一张图片 * '' - 页面中的唯一一张图片 */ const imgShowState = helper.createRootMemo(() => { if (store.pageList.length === 0) return new Map(); const showRange = store.gridMode ? [0, store.pageList.length - 1] : store.renderRange; const stateList = new Map(); for (let [i] = showRange; i <= showRange[1]; i++) { const page = store.pageList[i]; if (!page) continue; const [a, b] = page; if (b === undefined) { stateList.set(a, ''); } else { stateList.set(a, 0); stateList.set(b, 1); } } return stateList; }); // 卷轴模式下,将当前显示的第一页作为当前页 helper.createEffectOn(() => store.showRange, ([firstPage]) => { if (!store.gridMode && store.option.scrollMode.enabled) setState('activePageIndex', firstPage ?? 0); }); // 图片发生变化时触发回调 helper.createEffectOn(showImgList, showImgs => { if (showImgs.size === 0) return; store.prop.onShowImgsChange?.(showImgs, imgList()); }, { defer: true }); const setMessage = (url, msg) => setState('imgMap', url, 'translationMessage', msg); const sizeDict = { '1024': 'S', '1536': 'M', '2048': 'L', '2560': 'X' }; const createFormData = (imgBlob, type) => { const formData = new FormData(); const { options } = store.option.translation; const file = new File([imgBlob], \`image.\${imgBlob.type.split('/').at(-1)}\`, { type: imgBlob.type }); if (type === 'selfhosted') { formData.append('image', file); formData.append('config', JSON.stringify(options)); } else { formData.append('file', file); formData.append('mime', file.type); formData.append('size', sizeDict[options.detector.detection_size]); formData.append('detector', options.detector.detector); formData.append('direction', options.render.direction); formData.append('translator', options.translator.translator); formData.append(type === 'cotrans' ? 'target_language' : 'target_lang', options.translator.target_lang); formData.append('retry', \`\${store.option.translation.forceRetry}\`); } return formData; }; /** 将站点列表转为选择器中的选项 */ const createOptions = list => list.map(name => [name, helper.t(\`translation.translator.\${name}\`) || name]); const handleMessage = (msg, url) => { switch (msg.type) { case 'result': return msg.result.translation_mask; case 'pending': setMessage(url, helper.t('translation.tip.pending', { pos: msg.pos })); break; case 'status': setMessage(url, helper.t(\`translation.status.\${msg.status}\`) || msg.status); break; case 'error': throw new Error(\`\${helper.t('translation.status.error')}:id \${msg.error_id}\`); case 'not_found': throw new Error(\`\${helper.t('translation.status.error')}:Not Found\`); } }; const waitTranslationPolling = async (id, url) => { let result; while (result === undefined) { const res = await request.request(\`https://api.cotrans.touhou.ai/task/\${id}/status/v1\`, { responseType: 'json' }); result = handleMessage(res.response, url); await helper.sleep(1000); } return result; }; /** 等待翻译完成 */ const waitTranslation = async (id, url) => { const ws = new WebSocket(\`wss://api.cotrans.touhou.ai/task/\${id}/event/v1\`); // 如果网站设置了 CSP connect-src 就只能轮询了 if (ws.readyState > 1) return waitTranslationPolling(id, url); return new Promise((resolve, reject) => { ws.onmessage = e => { try { const result = handleMessage(JSON.parse(e.data), url); if (result) resolve(result); } catch (error) { reject(error); } }; }); }; /** 将翻译后的内容覆盖到原图上 */ const mergeImage = async (rawImage, maskUri) => { const img = await helper.waitImgLoad(URL.createObjectURL(rawImage)); const canvas = new OffscreenCanvas(img.naturalWidth, img.naturalHeight); const canvasCtx = canvas.getContext('2d'); canvasCtx.drawImage(img, 0, 0); const img2 = new Image(); img2.src = URL.createObjectURL(await downloadImg(maskUri)); await helper.waitImgLoad(img2); canvasCtx.drawImage(img2, 0, 0); return URL.createObjectURL(await helper.canvasToBlob(canvas)); }; /** 缩小过大的图片 */ const resize = async (blob, w, h) => { if (w <= 4096 && h <= 4096) return blob; const scale = Math.min(4096 / w, 4096 / h); const width = Math.floor(w * scale); const height = Math.floor(h * scale); const img = await helper.waitImgLoad(URL.createObjectURL(blob)); const canvas = new OffscreenCanvas(width, height); const ctx = canvas.getContext('2d'); ctx.imageSmoothingQuality = 'high'; ctx.drawImage(img, 0, 0, width, height); URL.revokeObjectURL(img.src); return helper.canvasToBlob(canvas); }; /** 使用 cotrans 翻译指定图片 */ const cotransTranslation = async url => { const img = store.imgMap[url]; setMessage(url, helper.t('translation.tip.img_downloading')); let imgBlob; try { imgBlob = await downloadImg(img.src); } catch (error) { helper.log.error(error); store.prop.onImgError?.(url); throw new Error(helper.t('translation.tip.download_img_failed')); } try { imgBlob = await resize(imgBlob, img.width, img.height); } catch (error) { helper.log.error(error); throw new Error(helper.t('translation.tip.resize_img_failed')); } setMessage(url, helper.t('translation.tip.upload')); let res; try { res = await request.request('https://api.cotrans.touhou.ai/task/upload/v1', { method: 'POST', data: createFormData(imgBlob, 'cotrans'), headers: { Origin: 'https://cotrans.touhou.ai', Referer: 'https://cotrans.touhou.ai/' } }); } catch (error) { helper.log.error(error); throw new Error(helper.t('translation.tip.upload_error')); } let resData; try { resData = JSON.parse(res.responseText); helper.log(resData); } catch { throw new Error(\`\${helper.t('translation.tip.upload_return_error')}:\${res.responseText}\`); } if ('error_id' in resData) throw new Error(\`\${helper.t('translation.tip.upload_return_error')}:\${resData.error_id}\`); if (!resData.id) throw new Error(helper.t('translation.tip.id_not_returned')); const translation_mask = resData.result?.translation_mask || (await waitTranslation(resData.id, url)); return mergeImage(imgBlob, translation_mask); }; const cotransTranslators = ['google', 'youdao', 'baidu', 'deepl', 'gpt3.5', 'offline', 'none']; const apiUrl = () => store.option.translation.localUrl || 'http://192.168.0.158:5001'; /** 使用自部署服务器翻译指定图片 */ const selfhostedTranslation = async url => { const html = await request.request(apiUrl(), { errorText: \`\${helper.t('setting.option.paragraph_translation')} - \${helper.t('alert.server_connect_failed')}\` }); setMessage(url, helper.t('translation.tip.img_downloading')); let imgBlob; try { imgBlob = await downloadImg(url); } catch (error) { helper.log.error(error, url); store.prop.onImgError?.(url); throw new Error(helper.t('translation.tip.download_img_failed')); } // 支持旧版 manga-image-translator // https://sleazyfork.org/zh-CN/scripts/374903/discussions/273466 if (html.responseText.includes('value="S">1024px')) { let task_id; // 上传图片取得任务 id try { const res = await request.request(\`\${apiUrl()}/submit\`, { method: 'POST', responseType: 'json', data: createFormData(imgBlob, 'selfhosted-old') }); ({ task_id } = res.response); } catch (error) { helper.log.error(error); throw new Error(helper.t('translation.tip.upload_error')); } let errorNum = 0; let taskState; // 等待翻译完成 while (!taskState?.finished) { try { await helper.sleep(200); const res = await request.request(\`\${apiUrl()}/task-state?taskid=\${task_id}\`, { responseType: 'json' }); taskState = res.response; setMessage(url, \`\${helper.t(\`translation.status.\${taskState.state}\`) || taskState.state}\`); } catch (error) { helper.log.error(error); if (errorNum > 5) throw new Error(helper.t('translation.tip.check_img_status_failed')); errorNum += 1; } } return URL.createObjectURL(await downloadImg(\`\${apiUrl()}/result/\${task_id}\`)); } try { const res = await fetch(\`\${apiUrl()}/translate/with-form/image/stream\`, { method: 'POST', body: createFormData(imgBlob, 'selfhosted') }); if (res.status !== 200 || !res.body) throw new Error(helper.t('translation.status.error')); const reader = res.body.getReader(); const decoder = new TextDecoder('utf8'); let buffer = new Uint8Array(); while (true) { const { done, value } = await reader.read(); if (done) break; buffer = Uint8Array.from([...buffer, ...value]); while (buffer.length >= 5) { const dataSize = new DataView(buffer.buffer).getUint32(1, false); const totalSize = 5 + dataSize; if (buffer.length < totalSize) break; const data = buffer.slice(5, totalSize); switch (buffer[0]) { case 0: return URL.createObjectURL(new Blob([data], { type: 'image/png' })); case 1: setMessage(url, helper.t(\`translation.status.\${decoder.decode(data)}\`)); break; case 2: throw new Error(\`\${helper.t('translation.status.error')}: \${decoder.decode(data)}\`); case 3: { const pos = decoder.decode(data); if (pos !== '0') { setMessage(url, helper.t('translation.tip.pending', { pos })); break; } // falls through } case 4: setMessage(url, helper.t('translation.status.pending')); break; } buffer = buffer.slice(totalSize); } } throw new Error(helper.t('translation.status.error')); } catch (error) { // 如果因为 cors 无法使用 fetch,就只能使用拿不到翻译状态的非流式接口了 if (error.message.includes('Failed to fetch')) { setMessage(url, helper.t('translation.tip.translating')); // 在拷贝漫画上莫名有概率报错 // 虽然猜测可能是 cors connect-src 导致的,但在类似的 fantia 上却也无法复现 // 也找不到第二个同样问题的网站,考虑到应该没人会在拷贝上翻译,就暂且不管了 const res = await request.request(\`\${apiUrl()}/translate/with-form/image\`, { method: 'POST', responseType: 'blob', fetch: false, timeout: 1000 * 60 * 10, data: createFormData(imgBlob, 'selfhosted'), errorText: helper.t('translation.tip.upload_error') }); return URL.createObjectURL(res.response); } throw error; } }; const [selfhostedOptions, setSelfOptions] = helper.createEqualsSignal([]); /** 更新部署服务的可用翻译 */ const updateSelfhostedOptions = async (noTip = false) => { if (store.option.translation.server !== 'selfhosted') return; try { const res = await request.request(\`\${apiUrl()}\`, { noTip, errorText: \`\${helper.t('setting.option.paragraph_translation')} - \${helper.t('alert.server_connect_failed')}\` }); const translatorsText = /(?<=validTranslators: )\\[.+?\\](?=,)/s.exec(res.responseText)?.[0]; if (!translatorsText) return undefined; const list = JSON.parse(translatorsText.replaceAll(/\\s|,\\s*(?=\\])/g, \`\`).replaceAll(\`'\`, \`"\`)); setSelfOptions(createOptions(list)); } catch (error) { helper.log.error(helper.t('translation.tip.get_translator_list_error'), error); setSelfOptions([]); } // 如果更新后原先选择的翻译服务失效了,就换成第一个翻译 if (!selfhostedOptions().some(([val]) => val === store.option.translation.options.translator.translator)) { setOption(draftOption => { draftOption.translation.options.translator.translator = selfhostedOptions()[0]?.[0]; }); } }; // 在切换翻译服务器的同时切换可用翻译的选项列表 helper.createEffectOn([() => store.option.translation.server, () => store.option.translation.localUrl, helper.lang], () => store.imgList.length > 0 && updateSelfhostedOptions(true), { defer: true }); /** 翻译指定图片 */ const translationImage = async url => { try { if (typeof GM_xmlhttpRequest === 'undefined') { Toast.toast?.error(helper.t('pwa.alert.userscript_not_installed')); throw new Error(helper.t('pwa.alert.userscript_not_installed')); } if (!url) return; const img = store.imgMap[url]; if (img.translationType !== 'wait') return; if (img.translationUrl) return setState('imgMap', url, 'translationType', 'show'); if (img.loadType !== 'loaded') return setMessage(url, helper.t('translation.tip.img_not_fully_loaded')); const translationUrl = await (store.option.translation.server === 'cotrans' ? cotransTranslation : selfhostedTranslation)(url); setState('imgMap', url, { translationUrl, translationMessage: helper.t('translation.tip.translation_completed'), translationType: 'show' }); } catch (error) { setState('imgMap', url, 'translationType', 'error'); if (error?.message) setState('imgMap', url, 'translationMessage', error.message); } }; /** 逐个翻译状态为等待翻译的图片 */ const translationAll = helper.singleThreaded(async state => { const targetImg = imgList().find(img => img.translationType === 'wait' && img.loadType === 'loaded'); if (!targetImg) return; await translationImage(targetImg.src); state.continueRun(); }); /** 开启或关闭指定图片的翻译 */ const setImgTranslationEnbale = (list, enbale) => { if (store.option.translation.server === 'disable' && enbale) return; setState(state => { for (const i of list) { const img = state.imgMap[state.imgList[i]]; if (!img) continue; const url = img.src; if (enbale) { if (state.option.translation.forceRetry) { img.translationType = 'wait'; img.translationUrl = undefined; setMessage(url, helper.t('translation.tip.wait_translation')); } else { switch (img.translationType) { case 'hide': { img.translationType = 'show'; break; } case 'error': case undefined: { img.translationType = 'wait'; setMessage(url, helper.t('translation.tip.wait_translation')); break; } } } } else { switch (img.translationType) { case 'show': { img.translationType = 'hide'; break; } case 'error': case 'wait': { img.translationType = undefined; break; } } } } }); return translationAll(); }; const translatorOptions = helper.createRootMemo(() => store.option.translation.server === 'selfhosted' ? selfhostedOptions() : createOptions(cotransTranslators)); /** 翻译范围的图片 */ const translationImgs = helper.createRootMemo(() => { const list = new Set(); for (const [i, img] of imgList().entries()) { switch (img.translationType) { case 'error': case 'show': case 'wait': list.add(i); } } return list; }); /** 当前显示的图片是否正在翻译 */ const isTranslatingImage = helper.createRootMemo(() => activePage().some(i => translationImgs().has(i))); /** 翻译当前页 */ const translateCurrent = () => setImgTranslationEnbale(activePage(), !isTranslatingImage()); const createTranslateRange = imgs => { const isTranslating = helper.createRootMemo(() => imgs().every(i => translationImgs().has(i))); const translateRange = () => { if (store.option.translation.server !== 'selfhosted') return; setImgTranslationEnbale(imgs(), !isTranslating()); }; return [isTranslating, translateRange]; }; // 翻译全部图片 const [isTranslatingAll, translateAll] = createTranslateRange(helper.createRootMemo(() => helper.range(store.imgList.length))); // 翻译当前页以后的全部图片 const [isTranslatingToEnd, translateToEnd] = createTranslateRange(helper.createRootMemo(() => helper.range(activeImgIndex(), store.imgList.length))); /** 图片上次加载出错的时间 */ const imgErrorTime = new Map(); /** 图片加载完毕的回调 */ const handleImgLoaded = (url, e) => { // 内联图片元素被创建后立刻就会触发 load 事件,如果在调用这个函数前 url 发生改变 // 就会导致这里获得的是上个 url 图片的尺寸 if (e && !e.isConnected) return; imgErrorTime.delete(url); const img = store.imgMap[url]; if (img.translationType === 'show') return; if (img.loadType !== 'loaded') { setState('imgMap', url, 'loadType', 'loaded'); updateImgLoadType(); store.prop.onLoading?.(imgList(), store.imgMap[url]); } if (!e) return; updateImgSize(url, e.naturalWidth, e.naturalHeight); if (store.option.imgRecognition.enabled && e.src === img.blobUrl) setTimeout(handleImgRecognition, 0, url, e); translationAll(); }; /** 图片加载出错的回调 */ const handleImgError = (url, e) => { if (e && !e.isConnected) return; const isRetry = !imgErrorTime.has(url); imgErrorTime.set(url, Date.now()); setState(state => { const img = state.imgMap[url]; if (!img) return; const imgIndexs = getImgIndexs(url); helper.log.error(imgIndexs, helper.t('alert.img_load_failed'), e); img.loadType = 'error'; img.type = undefined; if (imgIndexs.some(i => renderImgList().has(i)) && isRetry) img.loadType = 'wait'; }); store.prop.onLoading?.(imgList(), store.imgMap[url]); store.prop.onImgError?.(url); updateImgLoadType(); }; /** 需要加载的图片 */ const needLoadImgList = helper.createRootMemo(() => { const list = new Set(); for (const img of imgList()) if (img.loadType !== 'loaded' && img.src) list.add(img.src); return list; }); /** 当前加载的图片 */ const loadImgList = new Set(); /** 加载指定图片。返回是否已加载完成 */ const loadImg = url => { const img = store.imgMap[url]; if (!needLoadImgList().has(img.src)) return true; if (img.loadType === 'error') return true; loadImgList.add(url); return false; }; /** 获取指定页数下的头/尾图片 */ const getPageImg = (pageNum, imgType) => { const page = store.pageList[pageNum].filter(i => i !== -1); if (page.length === 1) return page[0]; return imgType === 'start' ? Math.min(...page) : Math.max(...page); }; /** * 以当前显示页为基准,预加载附近指定页数的图片,并取消其他预加载的图片 * @param target 加载目标页 * @param loadNum 加载图片数量 * @returns 返回指定范围内是否还有未加载的图片 */ const loadRangeImg = (target = 0, loadNum = 2) => { let start = getPageImg(store.showRange[0], 'start'); let end = getPageImg(store.showRange[1], 'end'); if (target !== 0) { if (target < 0) { end = start + target; start -= 1; } else { start = end + 1; end += target; } start = helper.clamp(0, start, store.imgList.length - 1); end = helper.clamp(0, end, store.imgList.length - 1); } /** 是否还有未加载的图片 */ let hasUnloadedImg = false; let index = start; const condition = start <= end ? () => index <= end : () => index >= end; const step = start <= end ? 1 : -1; while (condition()) { if (!loadImg(getImg(index).src)) hasUnloadedImg = true; if (loadImgList.size >= loadNum) return index !== end || hasUnloadedImg; index += step; } return hasUnloadedImg; }; /** 加载期间尽快获取图片尺寸 */ const checkImgSize = url => { const imgDom = getImgEle(url); if (!imgDom) return; const timeoutId = setInterval(() => { if (!imgDom?.isConnected || store.option.imgRecognition.enabled) return clearInterval(timeoutId); const img = store.imgMap[url]; if (!img || img.loadType !== 'loading') return clearInterval(timeoutId); if (imgDom.naturalWidth && imgDom.naturalHeight) { updateImgSize(url, imgDom.naturalWidth, imgDom.naturalHeight); return clearInterval(timeoutId); } }, 200); }; const updateImgLoadType = helper.singleThreaded(() => { if (store.showRange[0] < 0 || needLoadImgList().size === 0) return; loadImgList.clear(); if (store.imgList.length > 0) { // 优先加载当前显示的图片 loadRangeImg() || // 再加载后面几页 loadRangeImg(preloadNum().back) || // 再加载前面几页 loadRangeImg(-preloadNum().front) || // 根据设置决定是否要继续加载其余图片 !store.option.alwaysLoadAllImg || // 加载当前页后面的图片 loadRangeImg(Number.POSITIVE_INFINITY, 5) || // 加载当前页前面的图片 loadRangeImg(Number.NEGATIVE_INFINITY, 5); } setState(state => { for (const url of needLoadImgList()) { const img = state.imgMap[url]; if (loadImgList.has(url)) { if (img.loadType !== 'loading') { img.loadType = 'loading'; if (!store.option.imgRecognition.enabled && img.width === undefined) setTimeout(checkImgSize, 0, img.src); } } else if (img.loadType === 'loading') img.loadType = 'wait'; } }); }); helper.createEffectOn([preloadNum, helper.createRootMemo(() => [...renderImgList()].map(i => store.imgList[i])), () => store.option.alwaysLoadAllImg], updateImgLoadType); helper.createEffectOn(showImgList, helper.debounce(_showImgList => { // 如果当前显示页面有出错的图片,就重新加载一次 if (imgErrorTime.size === 0) return; for (const img of [..._showImgList].map(i => getImg(i))) { if (img?.loadType !== 'error') continue; setState('imgMap', img.src, 'loadType', 'wait'); updateImgLoadType(); } }, 500), { defer: true }); /** 隔一段时间重新加载出错的图片 */ const retryErrorImg = () => { if (imgErrorTime.size > 0) { const retryTime = Date.now() - 1000 * 60 * 3; for (const [url, time] of imgErrorTime.entries()) { if (time > retryTime) continue; setState('imgMap', url, 'loadType', 'wait'); updateImgLoadType(); } } // 重新加载间隔一定时间,避免因为短时间频繁加载而失败 setTimeout(retryErrorImg, 1000 * 5); }; retryErrorImg(); /** 加载中的图片 */ const loadingImgList = helper.createRootMemo(() => { const list = new Set(); for (const [url, img] of Object.entries(store.imgMap)) if (img.loadType === 'loading') list.add(url); return list; }); const abortMap = new Map(); const timeoutAbort = url => { if (!abortMap.has(url)) return; abortMap.get(url).abort(); abortMap.delete(url); handleImgError(url); }; helper.createEffectOn(loadingImgList, (downImgList, prevImgList) => { if (!store.option.imgRecognition.enabled) return; if (prevImgList) { // 中断取消下载的图片 for (const url of prevImgList) { if (downImgList.has(url) || !abortMap.has(url)) continue; abortMap.get(url)?.abort(); abortMap.delete(url); helper.log(\`中断下载 \${url}\`); } } for (const url of downImgList.values()) { if (abortMap.has(url) || store.imgMap[url].blobUrl) continue; const controller = new AbortController(); const handleTimeout = helper.debounce(() => timeoutAbort(url), 1000 * 3); controller.signal.addEventListener('abort', handleTimeout.clear); abortMap.set(url, controller); handleTimeout(); request.request(url, { responseType: 'blob', retryFetch: true, signal: controller.signal, timeout: undefined, noTip: true, headers: downloadImgHeaders, onerror: () => handleImgError(url), onprogress({ loaded, total }) { setState('imgMap', url, 'progress', loaded / total * 100); // 一段时间内都没进度后超时中断 handleTimeout(); }, onload({ response }) { abortMap.delete(url); setState('imgMap', url, { blobUrl: URL.createObjectURL(response), progress: undefined }); handleImgLoaded(url); } }); } }); const upscaleImage = async (url, imgEle) => { setState('imgMap', url, 'upscaleUrl', ''); const { data, width, height } = helper.getImageData(imgEle); initWorker(); await worker$1.upscaleImage(Comlink.transfer(data, [data.buffer]), width, height, url); }; let upscaleing = false; const findUpscaleImage = async (start, end) => { for (let i = start; i < end; i++) { const img = typeof i === 'number' ? getImg(i) : i; if (img.upscaleUrl !== undefined) continue; const imgEle = await helper.wait(() => getImgEle(img.src), 1000); if (imgEle) return [img.src, imgEle]; } }; const handleUpscaleImage = async () => { if (upscaleing || !isUpscale()) return; // 优先放大 当前显示的图片 > 后面的图片 > 前面的图片 const targetImg = (await findUpscaleImage(activeImgIndex(), store.imgList.length)) ?? (await findUpscaleImage(0, activeImgIndex())); if (!targetImg) return; upscaleing = true; await upscaleImage(...targetImg); upscaleing = false; return handleUpscaleImage(); }; helper.createEffectOn([isUpscale, imgList], handleUpscaleImage); const bufferToBase64 = buffer => { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) binary += String.fromCodePoint(bytes[i]); return window.btoa(binary); }; const getModel = async () => { try { let base64; let buffer; if (typeof GM !== 'undefined') base64 = await GM.getValue('@model.bin'); if (!base64) { Toast.toast(helper.t('upscale.module_downloading'), { id: 'upscale', duration: Number.POSITIVE_INFINITY }); // TODO: 修改网址 const bin = await request.request('https://cdn.jsdelivr.net/npm/@hymbz/comic-read-script@11.12.1/public/realcugan/2x-conservative-128/group1-shard1of1.bin', { responseType: 'arraybuffer', noTip: true }); Toast.toast(helper.t('upscale.module_download_complete'), { id: 'upscale', duration: 1000 * 3 }); buffer = bin.response; base64 = bufferToBase64(buffer); await GM.setValue('@model.bin', base64); } let json = await GM.getValue('@model.json'); if (!json) { const jsonFile = await request.request('https://cdn.jsdelivr.net/npm/@hymbz/comic-read-script@11.12.1/public/realcugan/2x-conservative-128/model.json', { noTip: true }); json = jsonFile.responseText; await GM.setValue('@model.json', json); } return { base64, json, buffer }; } catch (error) { helper.log.error('获取图片放大模型出错', error); Toast.toast.dismiss('upscale'); Toast.toast.error(helper.t('upscale.module_download_failed'), { id: 'upscale', duration: Number.POSITIVE_INFINITY }); setState('supportUpscaleImage', false); setState('option', 'imgRecognition', 'upscale', false); throw error; } }; const initWorker = helper.onec(() => { const mainFn = { log: helper.log, toast: Toast.toast, t: helper.t, setImg: (url, key, val) => Reflect.has(store.imgMap, url) && setState('imgMap', url, key, val), getModel }; worker$1.setMainFn(Comlink.proxy(mainFn), Object.keys(mainFn)); }); var css$1 = ".img____hash_base64_5_ img{display:block;height:100%;object-fit:contain;width:100%}.img____hash_base64_5_{align-content:center;content-visibility:hidden;display:none;height:100%;margin-left:auto;margin-right:auto;position:relative;width:100%}.img____hash_base64_5_[data-show]{content-visibility:visible;display:block}.img____hash_base64_5_>picture{display:block;height:auto;inset:0;margin-bottom:auto;margin-left:inherit;margin-right:inherit;margin-top:auto;max-height:100%;max-width:100%;position:absolute;width:auto}.img____hash_base64_5_>picture,.img____hash_base64_5_>picture:after{background-color:var(--hover-bg-color,#fff3);background-image:var(--md-photo);background-position:50%;background-repeat:no-repeat;background-size:30%}.img____hash_base64_5_[data-load-type=error]>picture:after{background-color:#eee;background-image:var(--md-image-not-supported);content:\\"\\";height:100%;pointer-events:none;position:absolute;right:0;top:0;width:100%}.img____hash_base64_5_[data-load-type=loading]>picture{background-image:var(--md-cloud-download)}:is(.img____hash_base64_5_[data-load-type=loading]>picture) img{animation:show____hash_base64_5_ .1s forwards}.mangaFlow____hash_base64_5_[dir=ltr] .img____hash_base64_5_[data-show=\\"1\\"],.mangaFlow____hash_base64_5_[dir=rtl] .img____hash_base64_5_[data-show=\\"0\\"]{margin-left:0;margin-right:auto}.mangaFlow____hash_base64_5_[dir=ltr] .img____hash_base64_5_[data-show=\\"0\\"],.mangaFlow____hash_base64_5_[dir=rtl] .img____hash_base64_5_[data-show=\\"1\\"]{margin-left:auto;margin-right:0}.mangaFlow____hash_base64_5_{backface-visibility:hidden;color:var(--text);contain:layout;display:grid;grid-auto-columns:100%;grid-auto-flow:column;grid-auto-rows:100%;height:100%;overflow:visible;place-items:center;position:absolute;row-gap:0;touch-action:none;transform-origin:0 0;-webkit-user-select:none;user-select:none;width:100%;will-change:left,top}.mangaFlow____hash_base64_5_[data-disable-zoom] .img____hash_base64_5_>picture{height:fit-content;width:fit-content}.mangaFlow____hash_base64_5_[data-hidden-mouse=true]{cursor:none}.mangaFlow____hash_base64_5_[data-vertical]{grid-auto-flow:row}.mangaBox____hash_base64_5_{contain:layout style;height:100%;transform-origin:0 0;transition-duration:0s;width:100%}.mangaBox____hash_base64_5_[data-animation=page] .mangaFlow____hash_base64_5_,.mangaBox____hash_base64_5_[data-animation=zoom]{transition-duration:.3s}.root____hash_base64_5_:not([data-grid-mode]) .mangaBox____hash_base64_5_{scrollbar-width:none}:is(.root____hash_base64_5_:not([data-grid-mode]) .mangaBox____hash_base64_5_)::-webkit-scrollbar{display:none}.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_{align-items:end;box-sizing:border-box;grid-auto-columns:1fr;grid-auto-flow:row;grid-auto-rows:max-content;grid-template-rows:unset;overflow:auto;row-gap:1.5em}:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_{cursor:pointer;margin-left:auto;margin-right:auto}:is(:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_)>picture{position:relative}:is(:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_)>.gridModeTip____hash_base64_5_{bottom:-1.5em;cursor:auto;direction:ltr;line-height:1.5em;opacity:.5;overflow:hidden;position:absolute;text-align:center;text-overflow:ellipsis;white-space:nowrap;width:100%}[data-load-type=error]:is(:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_),[data-load-type=wait]:is(:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_),[src=\\"\\"]:is(:is(.root____hash_base64_5_[data-grid-mode] .mangaFlow____hash_base64_5_) .img____hash_base64_5_){height:100%}.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_{overflow:auto}:is(.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_) .mangaFlow____hash_base64_5_{height:fit-content;row-gap:calc(var(--scroll-mode-spacing)*7px);touch-action:pan-y}[data-abreast-scroll]:is(.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_){overflow:hidden;touch-action:none}[data-abreast-scroll]:is(.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_) .mangaFlow____hash_base64_5_{align-items:start;column-gap:calc(var(--scroll-mode-spacing)*7px);height:100%}:is([data-abreast-scroll]:is(.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_) .mangaFlow____hash_base64_5_) .img____hash_base64_5_{height:auto;width:100%;will-change:transform}:is(:is([data-abreast-scroll]:is(.root____hash_base64_5_[data-scroll-mode]:not([data-grid-mode]) .mangaBox____hash_base64_5_) .mangaFlow____hash_base64_5_) .img____hash_base64_5_)>picture{position:relative}@keyframes show____hash_base64_5_{0%{opacity:0}90%{opacity:0}to{opacity:1}}.endPageBody____hash_base64_5_,.endPage____hash_base64_5_{align-items:center;display:flex;height:100%;justify-content:center;width:100%;z-index:10}.endPage____hash_base64_5_{background-color:#333d;color:#fff;left:0;opacity:0;pointer-events:none;position:absolute;top:0;transition:opacity .5s}.endPage____hash_base64_5_[data-show]{opacity:1;pointer-events:all}.endPage____hash_base64_5_[data-type=start] .tip____hash_base64_5_{transform:translateY(-10em)}.endPage____hash_base64_5_[data-type=end] .tip____hash_base64_5_{transform:translateY(10em)}.endPage____hash_base64_5_ .endPageBody____hash_base64_5_{transform:translateY(var(--drag-y,0));transition:transform .2s}:is(.endPage____hash_base64_5_ .endPageBody____hash_base64_5_) button{animation:jello____hash_base64_5_ .3s forwards;background-color:initial;color:inherit;cursor:pointer;font-size:1.2em;transform-origin:center}[data-is-end]:is(:is(.endPage____hash_base64_5_ .endPageBody____hash_base64_5_) button){font-size:3em;margin:2em}:is(.endPage____hash_base64_5_ .endPageBody____hash_base64_5_) .tip____hash_base64_5_{margin:auto;position:absolute}.endPage____hash_base64_5_[data-drag] .endPageBody____hash_base64_5_{transition:transform 0s}.root____hash_base64_5_[data-mobile] .endPage____hash_base64_5_>button{width:1em}.comments____hash_base64_5_{align-items:flex-end;display:flex;flex-direction:column;max-height:80%;opacity:.3;overflow:auto;padding-right:.5em;position:absolute;right:1em;width:20em}.comments____hash_base64_5_>p{background-color:#333b;border-radius:.5em;margin:.5em .1em;padding:.2em .5em}.comments____hash_base64_5_:hover{opacity:1}.root____hash_base64_5_[data-mobile] .comments____hash_base64_5_{bottom:0;max-height:15em;opacity:.8}@keyframes jello____hash_base64_5_{0%,11.1%,to{transform:translateZ(0)}22.2%{transform:skewX(-12.5deg) skewY(-12.5deg)}33.3%{transform:skewX(6.25deg) skewY(6.25deg)}44.4%{transform:skewX(-3.125deg) skewY(-3.125deg)}55.5%{transform:skewX(1.5625deg) skewY(1.5625deg)}66.6%{transform:skewX(-.7812deg) skewY(-.7812deg)}77.7%{transform:skewX(.3906deg) skewY(.3906deg)}88.8%{transform:skewX(-.1953deg) skewY(-.1953deg)}}.toolbar____hash_base64_5_{align-items:center;display:flex;height:100%;justify-content:flex-start;position:fixed;top:0;z-index:9}.toolbarPanel____hash_base64_5_{display:flex;flex-direction:column;padding:.5em;position:relative;transform:translateX(-100%);transition:transform .2s}.toolbarPanel____hash_base64_5_>hr{border:none;height:1em;margin:0;visibility:hidden}:is(.toolbar____hash_base64_5_[data-show],.toolbar____hash_base64_5_:hover) .toolbarPanel____hash_base64_5_{transform:none}.toolbar____hash_base64_5_[data-close] .toolbarPanel____hash_base64_5_{transform:translateX(-100%);visibility:hidden}.toolbarBg____hash_base64_5_{background-color:var(--page-bg);border-bottom-right-radius:1em;border-top-right-radius:1em;filter:opacity(.8);height:100%;position:absolute;right:0;top:0;width:100%}.root____hash_base64_5_[data-mobile] .toolbar____hash_base64_5_{font-size:1.3em}.root____hash_base64_5_[data-mobile] .toolbar____hash_base64_5_:not([data-show]){pointer-events:none}.root____hash_base64_5_[data-mobile] .toolbarBg____hash_base64_5_{filter:opacity(.8)}.SettingPanelPopper____hash_base64_5_{height:0!important;padding:0!important;pointer-events:unset!important;transform:none!important}.SettingPanel____hash_base64_5_{background-color:var(--page-bg);border-radius:.3em;bottom:0;box-shadow:0 3px 1px -2px #0003,0 2px 2px 0 #00000024,0 1px 5px 0 #0000001f;color:var(--text);font-size:1.2em;height:fit-content;margin:auto;max-height:95%;max-width:calc(100% - 5em);overflow:auto;position:fixed;top:0;-webkit-user-select:text;user-select:text;z-index:1}.SettingPanel____hash_base64_5_ hr{color:#fff;margin:.5em 0}.SettingPanel____hash_base64_5_>hr{margin:0}.SettingBlock____hash_base64_5_{display:grid;grid-template-rows:max-content 1fr;transition:grid-template-rows .2s ease-out}.SettingBlock____hash_base64_5_ .SettingBlockBody____hash_base64_5_{overflow:hidden;padding:0 .5em 1em;z-index:0}:is(.SettingBlock____hash_base64_5_ .SettingBlockBody____hash_base64_5_)>div+:is(.SettingBlock____hash_base64_5_ .SettingBlockBody____hash_base64_5_)>div{margin-top:1em}:is(.SettingBlock____hash_base64_5_ .SettingBlockBody____hash_base64_5_) input,:is(.SettingBlock____hash_base64_5_ .SettingBlockBody____hash_base64_5_) textarea{margin-top:.3em;width:97%}.SettingBlock____hash_base64_5_[data-show=false]{grid-template-rows:max-content 0fr;padding-bottom:unset}.SettingBlock____hash_base64_5_[data-show=false] .SettingBlockBody____hash_base64_5_{padding:unset}.SettingBlockSubtitle____hash_base64_5_{background-color:var(--page-bg);color:var(--text-secondary);cursor:pointer;font-size:.7em;height:3em;line-height:3em;margin-bottom:.1em;position:sticky;text-align:center;top:0;z-index:1}.SettingBlockBody____hash_base64_5_ .SettingBlockSubtitle____hash_base64_5_{height:1em;line-height:1em;position:unset}.SettingsItem____hash_base64_5_{align-items:center;display:flex;justify-content:space-between}:is(.SettingsItem____hash_base64_5_,.SettingsShowItem____hash_base64_5_)+.SettingsItem____hash_base64_5_{margin-top:1em}.SettingsItem____hash_base64_5_ button[disabled]{cursor:not-allowed;opacity:.5}.SettingsItemName____hash_base64_5_{font-size:.9em;max-width:calc(100% - 4em);overflow-wrap:anywhere;text-align:start;white-space:pre-wrap}.SettingsItemSwitch____hash_base64_5_{align-items:center;background-color:var(--switch-bg);border:0;border-radius:1em;cursor:pointer;display:inline-flex;height:.8em;margin:.3em;padding:0;width:2.3em}.SettingsItemSwitchRound____hash_base64_5_{background:var(--switch);border-radius:100%;box-shadow:0 2px 1px -1px #0003,0 1px 1px 0 #00000024,0 1px 3px 0 #0000001f;height:1.15em;transform:translateX(-10%);transition:transform .1s;width:1.15em}.SettingsItemSwitch____hash_base64_5_[data-checked=true]{background:var(--secondary-bg)}.SettingsItemSwitch____hash_base64_5_[data-checked=true] .SettingsItemSwitchRound____hash_base64_5_{background:var(--secondary);transform:translateX(110%)}.SettingsItemIconButton____hash_base64_5_{background-color:initial;border:none;color:var(--text);cursor:pointer;font-size:1.7em;height:1em;margin:0 .2em 0 0;padding:0}.SettingsItemSelect____hash_base64_5_{background-color:var(--hover-bg-color);border:none;border-radius:5px;cursor:pointer;font-size:.9em;margin:0;max-width:6.5em;outline:none;padding:.3em}.closeCover____hash_base64_5_{height:100%;left:0;position:fixed;top:0;width:100%}.SettingsShowItem____hash_base64_5_{display:grid;transition:grid-template-rows .2s ease-out}.SettingsShowItem____hash_base64_5_>.SettingsShowItemBody____hash_base64_5_{display:flex;flex-direction:column;overflow:hidden}:is(.SettingsShowItem____hash_base64_5_>.SettingsShowItemBody____hash_base64_5_)>.SettingsItem____hash_base64_5_{margin-top:1em}:is(.SettingsShowItem____hash_base64_5_>.SettingsShowItemBody____hash_base64_5_)>:is(textarea,input){line-height:1.2;margin:.4em .2em 0}[data-only-number]{padding:0 .2em}[data-only-number]+span{margin-left:-.1em}.hotkeys____hash_base64_5_{align-items:center;border-bottom:1px solid var(--secondary-bg);color:var(--text);display:flex;flex-grow:1;flex-wrap:wrap;font-size:.9em;padding:2em .2em .2em;position:relative;z-index:1}.hotkeys____hash_base64_5_+.hotkeys____hash_base64_5_{margin-top:.5em}.hotkeys____hash_base64_5_:last-child{border-bottom:none}.hotkeysItem____hash_base64_5_{align-items:center;border-radius:.3em;box-sizing:initial;cursor:pointer;display:flex;font-family:serif;height:1em;margin:.3em;outline:1px solid;outline-color:var(--secondary-bg);padding:.2em 1.2em}.hotkeysItem____hash_base64_5_>svg{background-color:var(--text);border-radius:1em;color:var(--page-bg);display:none;height:1em;margin-left:.4em;opacity:.5}:is(.hotkeysItem____hash_base64_5_>svg):hover{opacity:.9}.hotkeysItem____hash_base64_5_:hover{padding:.2em .5em}.hotkeysItem____hash_base64_5_:hover>svg{display:unset}.hotkeysItem____hash_base64_5_:focus,.hotkeysItem____hash_base64_5_:focus-visible{outline:var(--text) solid 2px}.hotkeysHeader____hash_base64_5_{align-items:center;box-sizing:border-box;display:flex;left:0;padding:0 .5em;position:absolute;top:0;width:100%}.hotkeysHeader____hash_base64_5_>p{background-color:var(--page-bg);line-height:1em;overflow-wrap:anywhere;text-align:start;white-space:pre-wrap}.hotkeysHeader____hash_base64_5_>div[title]{background-color:var(--page-bg);cursor:pointer;display:flex;transform:scale(0);transition:transform .1s}:is(.hotkeysHeader____hash_base64_5_>div[title])>svg{width:1.6em}.hotkeys____hash_base64_5_:hover div[title]{transform:scale(1)}.scrollbar____hash_base64_5_{--arrow-y:clamp(0.45em,calc(var(--slider-midpoint)),calc(var(--scroll-length) - 0.45em));border-left:max(6vw,1em) solid #0000;display:flex;flex-direction:column;height:98%;position:absolute;right:3px;top:1%;touch-action:none;-webkit-user-select:none;user-select:none;width:5px;z-index:9}.scrollbar____hash_base64_5_>div{align-items:center;display:flex;flex-direction:column;flex-grow:1;justify-content:center;pointer-events:none}.scrollbarPage____hash_base64_5_{background-color:var(--secondary);flex-grow:1;height:100%;transform:scaleY(1);transform-origin:bottom;transition:transform 1s;width:100%}.scrollbarPage____hash_base64_5_[data-type=loaded]{transform:scaleY(0)}.scrollbarPage____hash_base64_5_[data-upscale]{background-color:#b39ddb;transform:scaleY(1)}.scrollbarPage____hash_base64_5_[data-upscale=loading]{background-color:#d1c4e9}.scrollbarPage____hash_base64_5_[data-translation-type]{background-color:initial;transform:scaleY(1);transform-origin:top}.scrollbarPage____hash_base64_5_[data-translation-type=wait]{background-color:#81c784}.scrollbarPage____hash_base64_5_[data-translation-type=show]{background-color:#4caf50}.scrollbarPage____hash_base64_5_[data-translation-type=error]{background-color:#f005}.scrollbarPage____hash_base64_5_[data-type=wait]{opacity:.5}.scrollbarPage____hash_base64_5_[data-null]{background-color:#fbc02d}.scrollbarPage____hash_base64_5_[data-type=error]{background-color:#f005}.scrollbarSlider____hash_base64_5_{background-color:#fff5;border-radius:1em;height:var(--slider-height);justify-content:center;opacity:1;position:absolute;transform:translateY(var(--slider-top));transition:transform .15s,opacity .15s;width:100%;z-index:1}.scrollbarPoper____hash_base64_5_{--poper-top:clamp(0%,calc(var(--slider-midpoint) - 50%),calc(var(--scroll-length) - 100%));background-color:#303030;border-radius:.3em;color:#fff;font-size:.8em;line-height:1.5em;min-height:1.5em;min-width:1em;padding:.2em .5em;position:absolute;right:2em;text-align:center;transform:translateY(var(--poper-top));white-space:pre;width:fit-content}.scrollbar____hash_base64_5_:before{background-color:initial;border:.4em solid #0000;border-left:.5em solid #303030;content:\\"\\";position:absolute;right:2em;transform:translate(140%,calc(var(--arrow-y) - 50%))}.scrollbarPoper____hash_base64_5_,.scrollbar____hash_base64_5_:before{opacity:0;transition:opacity .15s,transform .15s}:is(.scrollbar____hash_base64_5_:hover,.scrollbar____hash_base64_5_[data-force-show]) .scrollbarPoper____hash_base64_5_,:is(.scrollbar____hash_base64_5_:hover,.scrollbar____hash_base64_5_[data-force-show]) .scrollbarSlider____hash_base64_5_,:is(.scrollbar____hash_base64_5_:hover,.scrollbar____hash_base64_5_[data-force-show]):before{opacity:1}.scrollbar____hash_base64_5_[data-drag] .scrollbarPoper____hash_base64_5_,.scrollbar____hash_base64_5_[data-drag] .scrollbarSlider____hash_base64_5_,.scrollbar____hash_base64_5_[data-drag]:before{transition:opacity .15s}.scrollbar____hash_base64_5_[data-auto-hidden]:not([data-force-show]) .scrollbarSlider____hash_base64_5_{opacity:0}.scrollbar____hash_base64_5_[data-auto-hidden]:not([data-force-show]):hover .scrollbarSlider____hash_base64_5_{opacity:1}.scrollbar____hash_base64_5_[data-position=hidden]{display:none}.scrollbar____hash_base64_5_[data-position=top]{border-bottom:max(6vh,1em) solid #0000;top:1px}.scrollbar____hash_base64_5_[data-position=top]:before{border-bottom:.5em solid #303030;right:0;top:1.2em;transform:translate(var(--arrow-x),-120%)}.scrollbar____hash_base64_5_[data-position=top] .scrollbarPoper____hash_base64_5_{top:1.2em}.scrollbar____hash_base64_5_[data-position=bottom]{border-top:max(6vh,1em) solid #0000;bottom:1px;top:unset}.scrollbar____hash_base64_5_[data-position=bottom]:before{border-top:.5em solid #303030;bottom:1.2em;right:0;transform:translate(var(--arrow-x),120%)}.scrollbar____hash_base64_5_[data-position=bottom] .scrollbarPoper____hash_base64_5_{bottom:1.2em}.scrollbar____hash_base64_5_[data-position=bottom],.scrollbar____hash_base64_5_[data-position=top]{--arrow-x:calc(var(--arrow-y)*-1 + 50%);border-left:none;flex-direction:row-reverse;height:5px;right:1%;width:98%}:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]):before{border-left:.4em solid #0000}:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarSlider____hash_base64_5_{height:100%;transform:translateX(calc(var(--slider-top)*-1));width:var(--slider-height)}:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarPoper____hash_base64_5_{padding:.1em .3em;right:unset;transform:translateX(calc(var(--poper-top)*-1))}[data-dir=ltr]:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]){--arrow-x:calc(var(--arrow-y) - 50%);flex-direction:row}[data-dir=ltr]:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]):before{left:0;right:unset}[data-dir=ltr]:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarSlider____hash_base64_5_{transform:translateX(var(--top))}[data-dir=ltr]:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarPoper____hash_base64_5_{transform:translateX(var(--poper-top))}:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarPage____hash_base64_5_{transform:scaleX(1)}[data-type=loaded]:is(:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarPage____hash_base64_5_){transform:scaleX(0)}[data-translation-type]:is(:is(.scrollbar____hash_base64_5_[data-position=top],.scrollbar____hash_base64_5_[data-position=bottom]) .scrollbarPage____hash_base64_5_){transform:scaleX(1)}.scrollbar____hash_base64_5_[data-is-abreast-mode] .scrollbarPoper____hash_base64_5_{line-height:1.5em;text-orientation:upright;writing-mode:vertical-rl}.scrollbar____hash_base64_5_[data-is-abreast-mode][data-dir=ltr] .scrollbarPoper____hash_base64_5_{writing-mode:vertical-lr}.root____hash_base64_5_[data-scroll-mode] .scrollbar____hash_base64_5_:before,.root____hash_base64_5_[data-scroll-mode] :is(.scrollbarSlider____hash_base64_5_,.scrollbarPoper____hash_base64_5_){transition:opacity .15s}:is(.root____hash_base64_5_[data-mobile] .scrollbar____hash_base64_5_:hover) .scrollbarPoper____hash_base64_5_,:is(.root____hash_base64_5_[data-mobile] .scrollbar____hash_base64_5_:hover):before{opacity:0}.touchAreaRoot____hash_base64_5_{color:#fff;display:grid;font-size:3em;grid-template-columns:1fr min(30%,10em) 1fr;grid-template-rows:1fr min(20%,10em) 1fr;height:100%;letter-spacing:.5em;opacity:0;pointer-events:none;position:absolute;top:0;transition:opacity .4s;-webkit-user-select:none;user-select:none;width:100%}.touchAreaRoot____hash_base64_5_[data-show]{opacity:1}.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_{align-items:center;display:flex;justify-content:center;text-align:center}[data-area=PREV]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_),[data-area=prev]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_){background-color:#95e1d3e6}[data-area=MENU]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_),[data-area=menu]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_){background-color:#fce38ae6}[data-area=NEXT]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_),[data-area=next]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_){background-color:#f38181e6}[data-area=PREV]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_):after{content:var(--i18n-touch-area-prev)}[data-area=MENU]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_):after{content:var(--i18n-touch-area-menu)}[data-area=NEXT]:is(.touchAreaRoot____hash_base64_5_ .touchArea____hash_base64_5_):after{content:var(--i18n-touch-area-next)}.touchAreaRoot____hash_base64_5_[data-vert=true]{flex-direction:column!important}.touchAreaRoot____hash_base64_5_:not([data-turn-page]) .touchArea____hash_base64_5_[data-area=NEXT],.touchAreaRoot____hash_base64_5_:not([data-turn-page]) .touchArea____hash_base64_5_[data-area=PREV],.touchAreaRoot____hash_base64_5_:not([data-turn-page]) .touchArea____hash_base64_5_[data-area=next],.touchAreaRoot____hash_base64_5_:not([data-turn-page]) .touchArea____hash_base64_5_[data-area=prev]{visibility:hidden}.touchAreaRoot____hash_base64_5_[data-area=edge]{grid-template-columns:1fr min(30%,10em) 1fr}.root____hash_base64_5_[data-mobile] .touchAreaRoot____hash_base64_5_{flex-direction:column!important;letter-spacing:0}.root____hash_base64_5_[data-mobile] [data-area]:after{font-size:.8em}.root____hash_base64_5_{background-color:var(--bg);font-size:1em;height:100%;outline:0;overflow:hidden;position:relative;width:100%}.root____hash_base64_5_ a{color:var(--text-secondary)}.root____hash_base64_5_[data-mobile]{font-size:.8em}.hidden____hash_base64_5_{display:none!important}.invisible____hash_base64_5_{visibility:hidden!important}.beautifyScrollbar____hash_base64_5_{scrollbar-color:var(--scrollbar-slider) #0000;scrollbar-width:thin}.beautifyScrollbar____hash_base64_5_::-webkit-scrollbar{height:10px;width:5px}.beautifyScrollbar____hash_base64_5_::-webkit-scrollbar-track{background:#0000}.beautifyScrollbar____hash_base64_5_::-webkit-scrollbar-thumb{background:var(--scrollbar-slider)}img,p{margin:0}:where(div,div:focus,div:focus-within,div:focus-visible,button){border:none;outline:none}blockquote{border-left:.25em solid var(--text-secondary,#607d8b);color:var(--text-secondary);font-size:.9em;font-style:italic;line-height:1.2em;margin:.5em 0 0;overflow-wrap:anywhere;padding:0 0 0 1em;text-align:start;white-space:pre-wrap}svg{width:1em}"; var modules_c21c94f2$1 = {"img":"img____hash_base64_5_","mangaFlow":"mangaFlow____hash_base64_5_","mangaBox":"mangaBox____hash_base64_5_","root":"root____hash_base64_5_","gridModeTip":"gridModeTip____hash_base64_5_","endPage":"endPage____hash_base64_5_","endPageBody":"endPageBody____hash_base64_5_","tip":"tip____hash_base64_5_","comments":"comments____hash_base64_5_","toolbar":"toolbar____hash_base64_5_","toolbarPanel":"toolbarPanel____hash_base64_5_","toolbarBg":"toolbarBg____hash_base64_5_","SettingPanelPopper":"SettingPanelPopper____hash_base64_5_","SettingPanel":"SettingPanel____hash_base64_5_","SettingBlock":"SettingBlock____hash_base64_5_","SettingBlockBody":"SettingBlockBody____hash_base64_5_","SettingBlockSubtitle":"SettingBlockSubtitle____hash_base64_5_","SettingsItem":"SettingsItem____hash_base64_5_","SettingsShowItem":"SettingsShowItem____hash_base64_5_","SettingsItemName":"SettingsItemName____hash_base64_5_","SettingsItemSwitch":"SettingsItemSwitch____hash_base64_5_","SettingsItemSwitchRound":"SettingsItemSwitchRound____hash_base64_5_","SettingsItemIconButton":"SettingsItemIconButton____hash_base64_5_","SettingsItemSelect":"SettingsItemSelect____hash_base64_5_","closeCover":"closeCover____hash_base64_5_","SettingsShowItemBody":"SettingsShowItemBody____hash_base64_5_","hotkeys":"hotkeys____hash_base64_5_","hotkeysItem":"hotkeysItem____hash_base64_5_","hotkeysHeader":"hotkeysHeader____hash_base64_5_","scrollbar":"scrollbar____hash_base64_5_","scrollbarPage":"scrollbarPage____hash_base64_5_","scrollbarSlider":"scrollbarSlider____hash_base64_5_","scrollbarPoper":"scrollbarPoper____hash_base64_5_","touchAreaRoot":"touchAreaRoot____hash_base64_5_","touchArea":"touchArea____hash_base64_5_","hidden":"hidden____hash_base64_5_","invisible":"invisible____hash_base64_5_","beautifyScrollbar":"beautifyScrollbar____hash_base64_5_"}; let clickTimeout = null; const useDoubleClick = (click, doubleClick, timeout = 200) => event => { // 如果点击触发时还有上次计时器的记录,说明这次是双击 if (clickTimeout) { clearTimeout(clickTimeout); clickTimeout = null; doubleClick?.(event); return; } // 单击事件延迟触发 clickTimeout = window.setTimeout(() => { click(event); clickTimeout = null; }, timeout); }; /** 将页面移回原位 */ const resetPage = (state, animation = false) => { updateShowRange(state); state.page.offset.x.pct = 0; state.page.offset.y.pct = 0; if (state.option.scrollMode.enabled) { state.page.anima = ''; return; } let i = -1; if (helper.inRange(state.renderRange[0], state.activePageIndex, state.renderRange[1])) i = state.activePageIndex - state.renderRange[0]; if (store.page.vertical) state.page.offset.y.pct = i === -1 ? 0 : -i;else state.page.offset.x.pct = i === -1 ? 0 : i; state.page.anima = animation ? 'page' : ''; }; /** 获取指定图片的提示文本 */ const getImgTip = i => { if (i === -1) return helper.t('other.fill_page'); const img = getImg(i); // 如果图片未加载完毕则在其 index 后增加显示当前加载状态 if (img.loadType !== 'loaded') return \`\${i + 1} (\${helper.t(\`img_status.\${img.loadType}\`)})\`; if (img.translationType && img.translationType !== 'hide' && img.translationMessage) return \`\${i + 1}:\${img.translationMessage}\`; if (isUpscale() && img.upscaleUrl !== undefined) return \`\${i + 1} (\${img.upscaleUrl ? helper.t('upscale.upscaled') : helper.t('upscale.upscaling')})\`; return \`\${i + 1}\`; }; /** 获取指定页面的提示文本 */ const getPageTip = pageIndex => { const page = store.pageList[pageIndex]; if (!page) return 'null'; const pageIndexText = page.map(index => getImgTip(index)); if (pageIndexText.length === 1) return pageIndexText[0]; if (store.option.dir === 'rtl') pageIndexText.reverse(); return pageIndexText.join(' | '); }; helper.createEffectOn(() => store.activePageIndex, () => store.show.endPage && setState('show', 'endPage', undefined), { defer: true }); helper.createEffectOn(activePage, helper.throttle(() => store.isDragMode || setState(resetPage))); // 在关闭工具栏的同时关掉滚动条的强制显示 helper.createEffectOn(() => store.show.toolbar, () => store.show.scrollbar && !store.show.toolbar && setState('show', 'scrollbar', false), { defer: true }); // 在切换网格模式后关掉 滚动条和工具栏 的强制显示 helper.createEffectOn(() => store.gridMode, () => setState(resetUI), { defer: true }); let cache = undefined; const initCache = async () => { cache ||= await helper.useCache({ progress: 'id' }, 'ReadProgress'); }; let lastIndex = -1; /** 保存阅读进度 */ const saveReadProgress = helper.throttle(async () => { await initCache(); const index = activeImgIndex(); if (index === lastIndex) return; lastIndex = index; if ( // 只保存 50 页以上漫画的进度 store.imgList.length < 50 || // 翻到最后几页时不保存 index >= store.imgList.length - 5) return await cache.del('progress', location.pathname); const imgSize = {}; for (const [i, img] of imgList().entries()) if (img.width && img.height) imgSize[i] = [img.width, img.height]; await cache.set('progress', { id: location.pathname, time: Date.now(), index, imgSize, fillEffect: store$1.unwrap(store.fillEffect) }); }, 1000); /** 恢复阅读进度 */ const resumeReadProgress = async state => { await initCache(); const progress = await cache.get('progress', location.pathname); if (!progress) return; // 目前卷轴模式下无法避免因图片加载导致的抖动, // 为了避免在恢复阅读进度时出现问题,只能将图片显示相关的数据也存着用于恢复 let i = state.imgList.length; while (i--) { const imgSize = progress.imgSize[i]; if (imgSize) updateImgSize(state.imgList[i], ...imgSize); } state.fillEffect = progress.fillEffect; updatePageData(state); if (state.option.scrollMode.enabled) setTimeout(scrollViewImg, 500, progress.index);else jumpToImg(progress.index); // 清除过时的进度 const nowTime = Date.now(); cache.each('progress', async (data, cursor) => { if (nowTime - data.time < 1000 * 60 * 60 * 24 * 29) return; await helper.promisifyRequest(cursor.delete()); }); }; const closeScrollLock = helper.debounce(() => setState('scrollLock', false), 100); /** 翻页。返回是否成功改变了当前页数 */ const turnPageFn = (state, dir) => { if (state.gridMode) return false; if (dir === 'prev') { switch (state.show.endPage) { case 'start': if (!state.scrollLock && store.option.scroolEnd === 'auto') state.prop.onPrev?.(); return false; case 'end': state.show.endPage = undefined; return false; default: // 弹出卷首结束页 if (isTop()) { if (!state.prop.onExit) return false; // 没有 onPrev 时不弹出 if (!state.prop.onPrev || store.option.scroolEnd !== 'auto') return false; state.show.endPage = 'start'; state.scrollLock = true; closeScrollLock(); return false; } saveReadProgress(); if (state.option.scrollMode.enabled) return false; state.activePageIndex -= 1; return true; } } else { switch (state.show.endPage) { case 'end': if (state.scrollLock) return false; if (state.prop.onNext && store.option.scroolEnd === 'auto') { state.prop.onNext(); return false; } if (store.option.scroolEnd !== 'none') state.prop.onExit?.(true); return false; case 'start': state.show.endPage = undefined; return false; default: // 弹出卷尾结束页 if (isBottom()) { if (!state.prop.onExit) return false; state.show.endPage = 'end'; state.scrollLock = true; closeScrollLock(); return false; } saveReadProgress(); if (state.option.scrollMode.enabled) return false; state.activePageIndex += 1; return true; } } }; const turnPage = dir => setState(state => turnPageFn(state, dir)); const turnPageAnimation = dir => { setState(state => { // 无法翻页就恢复原位 if (!turnPageFn(state, dir)) { state.page.offset.x.px = 0; state.page.offset.y.px = 0; resetPage(state, true); state.isDragMode = false; return; } state.isDragMode = true; resetPage(state); if (store.page.vertical) state.page.offset.y.pct += dir === 'next' ? 1 : -1;else state.page.offset.x.pct += dir === 'next' ? -1 : 1; setTimeout(() => { setState(draftState => { resetPage(draftState, true); draftState.page.offset.x.px = 0; draftState.page.offset.y.px = 0; draftState.isDragMode = false; }); }, 16); }); }; /** 判断翻页方向 */ const getTurnPageDir = (move, total, startTime) => { let dir; // 处理无关速度不考虑时间单纯根据当前滚动距离来判断的情况 if (!startTime) { if (Math.abs(move) > total / 2) dir = move > 0 ? 'next' : 'prev'; return dir; } // 滑动距离超过总长度三分之一判定翻页 if (Math.abs(move) > total / 3) dir = move > 0 ? 'next' : 'prev'; if (dir) return dir; // 滑动速度超过 0.4 判定翻页 const velocity = move / (performance.now() - startTime); if (velocity < -0.4) dir = 'prev'; if (velocity > 0.4) dir = 'next'; return dir; }; const touches = new Map(); const bound = helper.createMemoMap({ x: () => -store.rootSize.width * (store.option.zoom.ratio / 100 - 1), y: () => -store.rootSize.height * (store.option.zoom.ratio / 100 - 1) }); const checkBound = state => { state.option.zoom.offset.x = helper.clamp(bound().x, state.option.zoom.offset.x, 0); state.option.zoom.offset.y = helper.clamp(bound().y, state.option.zoom.offset.y, 0); }; const zoom = (val, focal, animation = false) => { const newScale = helper.clamp(100, val, 300); if (newScale === store.option.zoom.ratio) return; // 消除放大导致的偏移 const { left, top } = refs.mangaBox.getBoundingClientRect(); const x = (focal?.x ?? store.rootSize.width / 2) - left; const y = (focal?.y ?? store.rootSize.height / 2) - top; // 当前直接放大后的基准点坐标 const newX = x / (store.option.zoom.ratio / 100) * (newScale / 100); const newY = y / (store.option.zoom.ratio / 100) * (newScale / 100); // 放大后基准点的偏移距离 const dx = newX - x; const dy = newY - y; setOption((draftOption, state) => { draftOption.zoom.ratio = newScale; draftOption.zoom.offset.x -= dx; draftOption.zoom.offset.y -= dy; checkBound(state); if (animation) state.page.anima = 'zoom'; }); }; // // 惯性滑动 // /** 摩擦系数 */ const FRICTION_COEFF$1 = 0.91; const mouse = { x: 0, y: 0 }; const last = { x: 0, y: 0 }; const velocity = { x: 0, y: 0 }; let animationId$2 = null; const cancelAnimation = () => { if (!animationId$2) return; cancelAnimationFrame(animationId$2); animationId$2 = null; }; let lastTime$1 = 0; /** 逐帧计算惯性滑动 */ const handleSlideAnima = timestamp => { // 当速率足够小时停止计算动画 if (helper.approx(velocity.x, 0, 1) && helper.approx(velocity.y, 0, 1)) { animationId$2 = null; return; } // 在拖拽后模拟惯性滑动 setOption((draftOption, state) => { draftOption.zoom.offset.x += velocity.x; draftOption.zoom.offset.y += velocity.y; checkBound(state); // 确保每16毫秒才减少一次速率,防止在高刷新率显示器上衰减过快 if (timestamp - lastTime$1 > 16) { velocity.x *= FRICTION_COEFF$1; velocity.y *= FRICTION_COEFF$1; lastTime$1 = timestamp; } }); animationId$2 = requestAnimationFrame(handleSlideAnima); }; /** 逐帧根据鼠标坐标移动元素,并计算速率 */ const handleDragAnima$1 = () => { // 当停着不动时退出循环 if (mouse.x === store.option.zoom.offset.x && mouse.y === store.option.zoom.offset.y) { animationId$2 = null; return; } setOption((draftOption, state) => { last.x = draftOption.zoom.offset.x; last.y = draftOption.zoom.offset.y; draftOption.zoom.offset.x = mouse.x; draftOption.zoom.offset.y = mouse.y; checkBound(state); velocity.x = draftOption.zoom.offset.x - last.x; velocity.y = draftOption.zoom.offset.y - last.y; }); animationId$2 = requestAnimationFrame(handleDragAnima$1); }; /** 一段时间没有移动后应该将速率归零 */ const resetVelocity = helper.debounce(() => { velocity.x = 0; velocity.y = 0; }, 200); /** 是否正在双指捏合缩放中 */ let pinchZoom = false; /** 处理放大后的拖拽移动 */ const handleZoomDrag = ({ type, xy: [x, y], last: [lx, ly] }) => { if (store.option.zoom.ratio === 100) return; switch (type) { case 'down': { mouse.x = store.option.zoom.offset.x; mouse.y = store.option.zoom.offset.y; if (animationId$2) cancelAnimation(); break; } case 'move': { if (animationId$2) cancelAnimation(); mouse.x += x - lx; mouse.y += y - ly; animationId$2 ??= requestAnimationFrame(handleDragAnima$1); resetVelocity(); break; } case 'up': { resetVelocity.clear(); // 当双指捏合结束,一个手指抬起时,将剩余的指针当作刚点击来处理 if (pinchZoom) { pinchZoom = false; mouse.x = store.option.zoom.offset.x; mouse.y = store.option.zoom.offset.y; return; } if (animationId$2) cancelAnimationFrame(animationId$2); animationId$2 = requestAnimationFrame(handleSlideAnima); } } }; // // 双指捏合缩放 // /** 初始双指距离 */ let initDistance = 0; /** 初始缩放比例 */ let initScale = 100; /** 获取两个指针之间的距离 */ const getDistance = (a, b) => Math.hypot(b.xy[0] - a.xy[0], b.xy[1] - a.xy[1]); /** 逐帧计算当前屏幕上两点之间的距离,并换算成缩放比例 */ const handlePinchZoomAnima = () => { if (touches.size < 2) { animationId$2 = null; return; } const [a, b] = [...touches.values()]; const distance = getDistance(a, b); zoom(distance / initDistance * initScale, { x: (a.xy[0] + b.xy[0]) / 2, y: (a.xy[1] + b.xy[1]) / 2 }); animationId$2 = requestAnimationFrame(handlePinchZoomAnima); }; /** 处理双指捏合缩放 */ const handlePinchZoom = ({ type }) => { if (touches.size < 2) return; switch (type) { case 'down': { pinchZoom = true; const [a, b] = [...touches.values()]; initDistance = getDistance(a, b); initScale = store.option.zoom.ratio; break; } case 'up': { const [a, b] = [...touches.values()]; initDistance = getDistance(a, b); break; } case 'move': { animationId$2 ??= requestAnimationFrame(handlePinchZoomAnima); break; } case 'cancel': { const [a, b] = [...touches.values()]; initDistance = getDistance(a, b); break; } } }; /** 根据坐标判断点击的元素 */ const findClickEle = (eleList, { x, y }) => [...eleList].find(e => { const rect = e.getBoundingClientRect(); return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom; }); /** 触发点击区域操作 */ const handlePageClick = e => { const targetArea = findClickEle(refs.touchArea.children, e); if (!targetArea) return; const areaName = targetArea.dataset.area; if (!areaName) return; if (areaName === 'menu' || areaName === 'MENU') return setState(state => { state.show.scrollbar = !state.show.scrollbar; state.show.toolbar = !state.show.toolbar; }); if (!store.option.clickPageTurn.enabled || store.option.zoom.ratio !== 100) return; setState(state => { resetUI(state); turnPageFn(state, areaName.toLowerCase()); }); }; /** 网格模式下点击图片跳到对应页 */ const handleGridClick = e => { const target = findClickEle(refs.root.getElementsByClassName(modules_c21c94f2$1.img), e); if (target) jumpToImg(Number(/_(\\d+)_/.exec(target.id)?.[1])); }; /** 双击放大 */ const doubleClickZoom = e => !store.gridMode && zoom(store.option.zoom.ratio === 100 ? 350 : 100, e, true); const handleClick = useDoubleClick(e => store.gridMode ? handleGridClick(e) : handlePageClick(e), doubleClickZoom); let dx$1 = 0; let dy$1 = 0; let animationId$1 = null; const handleDragAnima = () => { // 当停着不动时退出循环 if (dx$1 === store.page.offset.x.px && dy$1 === store.page.offset.y.px) { animationId$1 = null; return; } setState(state => { if (state.page.vertical) state.page.offset.y.px = dy$1;else state.page.offset.x.px = dx$1; }); animationId$1 = requestAnimationFrame(handleDragAnima); }; const handleDragEnd = startTime => { dx$1 = 0; dy$1 = 0; if (animationId$1) { cancelAnimationFrame(animationId$1); animationId$1 = null; } // 将拖动的页面移回正常位置 const dir = store.page.vertical ? getTurnPageDir(-store.page.offset.y.px, store.rootSize.height, startTime) : getTurnPageDir(store.page.offset.x.px, store.rootSize.width, startTime); if (dir) return turnPageAnimation(dir); setState(state => { state.page.offset.x.px = 0; state.page.offset.y.px = 0; state.page.anima = 'page'; state.isDragMode = false; }); }; handleDragEnd.debounce = helper.debounce(handleDragEnd, 200); const handleMangaFlowDrag = ({ type, xy: [x, y], initial: [ix, iy], startTime }) => { switch (type) { case 'move': { dx$1 = store.option.dir === 'rtl' ? x - ix : ix - x; dy$1 = y - iy; if (store.isDragMode) { animationId$1 ||= requestAnimationFrame(handleDragAnima); return; } // 判断滑动方向 let slideDir; const dxAbs = Math.abs(dx$1); const dyAbs = Math.abs(dy$1); if (dxAbs > 5 && dyAbs < 5) slideDir = 'horizontal'; if (dyAbs > 5 && dxAbs < 5) slideDir = 'vertical'; if (!slideDir) return; setState(state => { // 根据滑动方向自动切换排列模式 state.page.vertical = slideDir === 'vertical'; state.isDragMode = true; resetPage(state); }); return; } case 'up': return handleDragEnd(startTime); } }; let lastDeltaY$1 = 0; let retardStartTime = 0; let lastWheel = 0; const handleTrackpadWheel = e => { let deltaY = Math.floor(-e.deltaY); let absDeltaY = Math.abs(deltaY); if (absDeltaY < 2) return; let time = 0; let now = 0; // 为了避免被触摸板的滚动惯性触发,限定一下滚动距离 if (absDeltaY > 50) { now = performance.now(); time = now - lastWheel; lastWheel = now; } if (store.option.scrollMode.enabled) { if (time > 200 && (isTop() && e.deltaY < 0 || isBottom() && e.deltaY > 0)) turnPage(e.deltaY > 0 ? 'next' : 'prev'); return; } // 加速度小于指定值后逐渐缩小滚动距离,实现减速效果 if (Math.abs(absDeltaY - lastDeltaY$1) <= 6) { retardStartTime ||= Date.now(); deltaY *= 1 - Math.min(1, (Date.now() - retardStartTime) / 10 * 0.002); absDeltaY = Math.abs(deltaY); if (absDeltaY < 2) return; } else retardStartTime = 0; lastDeltaY$1 = absDeltaY; dy$1 += deltaY; setState(state => { // 滚动至漫画头尾尽头时 if (isTop() && dy$1 > 0 || isBottom() && dy$1 < 0) { if (time > 200) turnPageFn(state, dy$1 < 0 ? 'next' : 'prev'); dy$1 = 0; } // 滚动过一页时 if (dy$1 <= -state.rootSize.height) { if (turnPageFn(state, 'next')) dy$1 += state.rootSize.height; } else if (dy$1 >= state.rootSize.height && turnPageFn(state, 'prev')) dy$1 -= state.rootSize.height; state.page.vertical = true; state.isDragMode = true; resetPage(state); }); animationId$1 ||= requestAnimationFrame(handleDragAnima); handleDragEnd.debounce(); }; /** 切换页面填充 */ const switchFillEffect = () => { setState(state => { // 如果当前页不是双页显示的就跳过,避免在显示跨页图的页面切换却没看到效果的疑惑 if (state.pageList[state.activePageIndex].length !== 2) return; state.fillEffect[nowFillIndex()] = Number(!state.fillEffect[nowFillIndex()]); updatePageData(state); }); }; /** 切换卷轴模式 */ const switchScrollMode = () => { const index = activeImgIndex(); zoom(100); setOption((draftOption, state) => { draftOption.scrollMode.enabled = !draftOption.scrollMode.enabled; state.page.offset.x.px = 0; state.page.offset.y.px = 0; }); jumpToImg(index); }; /** 切换单双页模式 */ const switchOnePageMode = () => { const index = activeImgIndex(); setOption((draftOption, state) => { if (draftOption.scrollMode.enabled) { if (draftOption.scrollMode.abreastMode) { draftOption.scrollMode.abreastMode = false; draftOption.scrollMode.doubleMode = true; } else draftOption.scrollMode.doubleMode = !draftOption.scrollMode.doubleMode; } else { const newPageNum = pageNum() === 1 ? 2 : 1; draftOption.pageNum = state.option.autoSwitchPageMode && newPageNum === autoPageNum() ? 0 : newPageNum; } }); jumpToImg(index); }; /** 切换阅读方向 */ const switchDir = () => { setOption(draftOption => { draftOption.dir = draftOption.dir === 'rtl' ? 'ltr' : 'rtl'; }); }; /** 切换网格模式 */ const switchGridMode = () => { zoom(100); setState(state => { state.gridMode = !state.gridMode; if (store.option.zoom.ratio !== 100) zoom(100); state.page.anima = ''; }); // 切换到网格模式后自动定位到当前页 if (store.gridMode) requestAnimationFrame(() => { refs.mangaFlow.children[activeImgIndex()]?.scrollIntoView({ block: 'center', inline: 'center' }); }); }; /** 切换卷轴模式下图片适应宽度 */ const switchFitToWidth = () => { const jump = saveScrollProgress(); setOption(draftOption => { draftOption.scrollMode.fitToWidth = !draftOption.scrollMode.fitToWidth; }); jump(); }; /** 切换全屏 */ const switchFullscreen = () => { if (document.fullscreenElement) document.exitFullscreen();else refs.root.requestFullscreen(); }; /** 切换自动滚动 */ const switchAutoScroll = () => setState('autoScroll', 'play', val => !val); /** 切换图片识别相关功能 */ const switchImgRecognition = (...path) => setOption((draftOption, state) => { const option = draftOption.imgRecognition; if (path.length === 0) path.push('enabled'); for (const key of path) option[key] = !option[key]; if (!option.enabled) return; for (const img of Object.values(state.imgMap)) { if (!img.blobUrl) img.loadType = 'wait'; if (img.loadType !== 'loaded') continue; handleImgRecognition(img.src); } if (path.includes('enabled')) updateImgLoadType(); }); // 特意使用 requestAnimationFrame 和 .click() 是为了能和 Vimium 兼容 // (虽然因为使用了 shadow dom 的缘故实际还是不能兼容,但说不定之后就改了呢 const focus = () => requestAnimationFrame(() => { refs.mangaBox?.click(); refs.mangaBox?.focus(); }); const handleMouseDown = e => { if (e.button !== 1 || store.option.scrollMode.enabled) return; e.stopPropagation(); e.preventDefault(); switchFillEffect(); }; /** 卷轴模式下的页面滚动 */ const scrollModeScrollPage = x => { if (!store.show.endPage) { scrollTo(scrollTop() + x, true); setState('scrollLock', true); } closeScrollLock(); }; /** 根据是否开启了 左右翻页键交换 来切换翻页方向 */ const handleSwapPageTurnKey = nextPage => { const next = store.option.swapPageTurnKey ? !nextPage : nextPage; return next ? 'next' : 'prev'; }; /** 处理快捷键长按的情况 */ const handleHoldKey = new class { holdKeys = new Map(); linsten(code, holdFn, upFn) { if (this.holdKeys.has(code)) return; holdFn(); this.holdKeys.set(code, upFn); } onKeyUp = e => { const code = helper.getKeyboardCode(e); if (!this.holdKeys.has(code)) return; this.holdKeys.get(code)(); this.holdKeys.delete(code); }; }(); /** 处理长按滚动 */ const handleHoldScroll = (code, speed) => { handleHoldKey.linsten(code, () => constantScroll.start(speed), () => constantScroll.cancel()); }; const handleKeyDown = e => { switch (e.target.tagName) { case 'INPUT': case 'TEXTAREA': return; } if (e.target.className === modules_c21c94f2$1.hotkeysItem) return; const code = helper.getKeyboardCode(e); // esc 在触发配置操作前,先用于退出一些界面 if (e.key === 'Escape') { if (store.gridMode) { e.stopPropagation(); e.preventDefault(); return setState('gridMode', false); } if (store.show.endPage) { e.stopPropagation(); e.preventDefault(); return setState('show', 'endPage', undefined); } } // 处理标注了 data-only-number 的元素 if (e.target.dataset.onlyNumber !== undefined) { // 拦截能输入数字外的按键 if (/^(?:Shift \\+ )?[a-zA-Z]$/.test(code)) { e.stopPropagation(); e.preventDefault(); } return; } // 卷轴、网格模式下跳过用于移动的按键 if ((isScrollMode() || store.gridMode) && !store.show.endPage) { switch (e.key) { case 'Home': case 'End': case 'ArrowRight': case 'ArrowLeft': e.stopPropagation(); return; case 'ArrowUp': case 'PageUp': e.stopPropagation(); return store.gridMode || turnPage('prev'); case 'ArrowDown': case 'PageDown': case ' ': e.stopPropagation(); return store.gridMode || turnPage('next'); } } // 拦截已注册的快捷键 if (Reflect.has(hotkeysMap(), code)) { e.stopPropagation(); e.preventDefault(); } else return; const hotkey = hotkeysMap()[code]; // 并排卷轴模式下的快捷键 if (isAbreastMode()) { switch (hotkey) { case 'scroll_up': return setAbreastScrollFill(abreastScrollFill() - 20); case 'scroll_down': return setAbreastScrollFill(abreastScrollFill() + 20); case 'scroll_left': return scrollTo(scrollProgress() - (store.option.dir === 'rtl' ? 20 : -20)); case 'scroll_right': return scrollTo(scrollProgress() + (store.option.dir === 'rtl' ? 20 : -20)); case 'page_up': return scrollTo(scrollProgress() - abreastColumnWidth()); case 'page_down': return scrollTo(scrollProgress() + abreastColumnWidth()); case 'jump_to_home': return scrollTo(0); case 'jump_to_end': return scrollTo(scrollLength()); } } // 普通卷轴模式下的快捷键 if (isScrollMode()) { switch (hotkey) { case 'page_up': return scrollModeScrollPage(-store.rootSize.height * 0.8); case 'page_down': return scrollModeScrollPage(store.rootSize.height * 0.8); case 'scroll_up': if (e.repeat) return handleHoldScroll(code, -1); return scrollModeScrollPage(-40); case 'scroll_down': if (e.repeat) return handleHoldScroll(code, 1); return scrollModeScrollPage(40); } } switch (hotkey) { case 'page_up': case 'scroll_up': return turnPage('prev'); case 'page_down': case 'scroll_down': return turnPage('next'); case 'scroll_left': return turnPage(handleSwapPageTurnKey(store.option.dir === 'rtl')); case 'scroll_right': return turnPage(handleSwapPageTurnKey(store.option.dir !== 'rtl')); case 'jump_to_home': return setState('activePageIndex', 0); case 'jump_to_end': return setState('activePageIndex', Math.max(0, store.pageList.length - 1)); case 'switch_page_fill': return switchFillEffect(); case 'switch_scroll_mode': return switchScrollMode(); case 'switch_single_double_page_mode': return switchOnePageMode(); case 'switch_dir': return switchDir(); case 'switch_grid_mode': return switchGridMode(); case 'translate_current_page': return translateCurrent(); case 'translate_all': return translateAll(); case 'translate_to_end': return translateToEnd(); case 'auto_scroll': return switchAutoScroll(); case 'fullscreen': return switchFullscreen(); case 'switch_auto_enlarge': return setOption(draftOption => { draftOption.disableZoom = !draftOption.disableZoom; }); case 'exit': return store.prop.onExit?.(); // 阅读模式以外的快捷键转发到网页上去处理 default: document.body.dispatchEvent(new KeyboardEvent('keydown', e)); document.body.dispatchEvent(new KeyboardEvent('keyup', e)); } }; /** 判断两个数值是否是整数倍的关系 */ const isMultipleOf = (a, b) => { const decimal = \`\${a < b ? b / a : a / b}\`.split('.')?.[1]; return !decimal || decimal.startsWith('0000') || decimal.startsWith('9999'); }; let lastDeltaY = -1; let timeoutId = 0; let lastPageNum = -1; let wheelType; let equalNum = 0; let diffNum = 0; const handleWheel = e => { if (store.gridMode) return; e.stopPropagation(); if (e.ctrlKey || e.altKey) e.preventDefault(); const isWheelDown = e.deltaY > 0; if (store.show.endPage) return turnPage(isWheelDown ? 'next' : 'prev'); // 卷轴模式下的图片缩放 if ((e.ctrlKey || e.altKey) && store.option.scrollMode.enabled && store.option.zoom.ratio === 100) { e.preventDefault(); if (store.option.scrollMode.fitToWidth) return; return zoomScrollModeImg(isWheelDown ? -0.05 : 0.05); } if (e.ctrlKey || e.altKey) { e.preventDefault(); return zoom(store.option.zoom.ratio + (isWheelDown ? -25 : 25), e); } const nowDeltaY = Math.abs(e.deltaY); // 并排卷轴模式下 if (isAbreastMode() && store.option.zoom.ratio === 100) { e.preventDefault(); // 先触发翻页判断再滚动,防止在滚动到底时立刻触发结束页 turnPage(isWheelDown ? 'next' : 'prev'); scrollTo(scrollTop() + e.deltaY); } // 防止滚动到网页 if (!isScrollMode()) e.preventDefault(); // 通过\`两次滚动距离是否成倍数\`和\`滚动距离是否过小\`来判断是否是触摸板 if (wheelType !== 'trackpad' && (nowDeltaY < 2 || !Number.isInteger(lastDeltaY) && !Number.isInteger(nowDeltaY) && !isMultipleOf(lastDeltaY, nowDeltaY))) { wheelType = 'trackpad'; if (timeoutId) clearTimeout(timeoutId); // 如果是触摸板滚动,且上次成功触发了翻页,就重新翻页回去 if (lastPageNum !== -1) setState('activePageIndex', lastPageNum); } // 为了避免因临时卡顿而误判为触摸板 // 在连续几次滚动量均相同的情况下,将 wheelType 相关变量重置回初始状态 if (diffNum < 10) { if (lastDeltaY === nowDeltaY && nowDeltaY > 5) equalNum += 1;else { diffNum += 1; equalNum = 0; } if (equalNum >= 3) { wheelType = undefined; lastPageNum = -1; } } lastDeltaY = nowDeltaY; switch (wheelType) { case undefined: { if (lastPageNum === -1) { // 第一次触发滚动没法判断类型,就当作滚轮来处理 // 但为了避免触摸板前两次滚动事件间隔大于帧生成时间导致得重新翻页回去的闪烁,加个延迟等待下 lastPageNum = store.activePageIndex; timeoutId = window.setTimeout(() => turnPage(isWheelDown ? 'next' : 'prev'), 16); return; } wheelType = 'mouse'; } // falls through case 'mouse': return turnPage(isWheelDown ? 'next' : 'prev'); case 'trackpad': return handleTrackpadWheel(e); } }; /** 滚动条元素的长度 */ const scrollDomLength = helper.createRootMemo(() => Math.max(store.scrollbarSize.width, store.scrollbarSize.height)); /** 滚动条滑块的中心点高度 */ const sliderMidpoint = helper.createRootMemo(() => scrollDomLength() * (scrollPercentage() + sliderHeight() / 2)); /** 滚动条滑块的位置 */ const sliderTop = helper.createRootMemo(() => \`\${scrollPercentage() * scrollDomLength()}px\`); /** 滚动条位置 */ const scrollPosition = helper.createRootMemo(() => { if (store.option.scrollbar.position === 'auto') { if (store.isMobile) return 'top'; if (isAbreastMode()) return 'bottom'; // 大部分图片都是宽图时,将滚动条移至底部 return store.defaultImgType === 'long' ? 'bottom' : 'right'; } return store.option.scrollbar.position; }); /** 判断点击位置在滚动条上的位置比率 */ const getClickTop = (x, y, e) => { switch (scrollPosition()) { case 'bottom': case 'top': return store.option.dir === 'rtl' ? 1 - x / e.offsetWidth : x / e.offsetWidth; default: return y / e.offsetHeight; } }; /** 计算在滚动条上的拖动距离 */ const getSliderDist = ([x, y], [ix, iy], e) => { switch (scrollPosition()) { case 'bottom': case 'top': return store.option.dir === 'rtl' ? (1 - (x - ix)) / e.offsetWidth : (x - ix) / e.offsetWidth; default: return (y - iy) / e.offsetHeight; } }; const [isDrag, setIsDrag] = solidJs.createSignal(false); const closeDrag = helper.debounce(() => setIsDrag(false), 200); let lastType = 'up'; /** 开始拖拽时的 sliderTop 值 */ let startTop = 0; const handleScrollbarSlider = ({ type, xy, initial }, e) => { const [x, y] = xy; // 检测是否是拖动操作 if (type === 'move' && lastType === type) { setIsDrag(true); closeDrag(); } lastType = type; // 跳过拖拽结束事件(单击时会同时触发开始和结束,就用开始事件来完成单击的效果 if (type === 'up') return saveReadProgress(); if (!refs.mangaFlow) return; const scrollbarDom = e.target; /** 点击位置在滚动条上的位置比率 */ const clickTop = getClickTop(x, y, e.target); if (store.option.scrollMode.enabled) { if (type === 'move') { const top = helper.clamp(0, startTop + getSliderDist(xy, initial, scrollbarDom), 1) * scrollLength(); scrollTo(top); } else { // 确保滚动条的中心会在点击位置 startTop = clickTop - sliderHeight() / 2; const top = startTop * scrollLength(); scrollTo(top, true); } } else { let newPageIndex = Math.floor(clickTop * store.pageList.length); // 处理超出范围的情况 if (newPageIndex < 0) newPageIndex = 0;else if (newPageIndex >= store.pageList.length) newPageIndex = store.pageList.length - 1; if (newPageIndex !== store.activePageIndex) setState('activePageIndex', newPageIndex); } }; /** 摩擦系数 */ const FRICTION_COEFF = 0.96; let lastTop = 0; let dy = 0; let lastLeft = 0; let dx = 0; let animationId = null; let lastTime = 0; /** 逐帧计算速率 */ const calcVelocity = () => { const nowTop = store.option.scrollMode.abreastMode ? abreastScrollFill() : scrollTop(); dy = nowTop - lastTop; lastTop = nowTop; dx = store.page.offset.x.px - lastLeft; lastLeft = store.page.offset.x.px; animationId = requestAnimationFrame(calcVelocity); }; /** 逐帧计算惯性滑动 */ const handleSlide = timestamp => { // 当速率足够小时停止计算动画 if (Math.abs(dx) + Math.abs(dy) < 1) { animationId = null; return; } // 确保每16毫秒才减少一次速率,防止在高刷新率显示器上衰减过快 if (timestamp - lastTime > 16) { dy *= FRICTION_COEFF; dx *= FRICTION_COEFF; lastTime = timestamp; } if (store.option.scrollMode.abreastMode) { scrollTo(scrollTop() + dx); setAbreastScrollFill(abreastScrollFill() + dy); } else scrollTo(scrollTop() + dy); animationId = requestAnimationFrame(handleSlide); }; let initTop = 0; let initLeft = 0; let initAbreastScrollFill = 0; const handleScrollModeDrag = ({ type, xy: [x, y], initial: [ix, iy] }, e) => { if (!store.option.scrollMode.abreastMode && e.pointerType !== 'mouse') return; switch (type) { case 'down': { if (animationId) cancelAnimationFrame(animationId); initTop = refs.mangaBox.scrollTop; initLeft = store.page.offset.x.px * (store.option.dir === 'rtl' ? 1 : -1); initAbreastScrollFill = abreastScrollFill(); requestAnimationFrame(calcVelocity); return; } case 'move': { if (store.option.scrollMode.abreastMode) { const _dx = x - ix; const _dy = y - iy; scrollTo((initLeft + _dx) * (store.option.dir === 'rtl' ? 1 : -1)); setAbreastScrollFill(initAbreastScrollFill + _dy); } else scrollTo(initTop + iy - y); return; } case 'up': { if (animationId) cancelAnimationFrame(animationId); animationId = requestAnimationFrame(handleSlide); saveReadProgress(); } } }; /** 在鼠标静止一段时间后自动隐藏 */ const useHiddenMouse = () => { const [hiddenMouse, setHiddenMouse] = solidJs.createSignal(true); const hidden = helper.debounce(() => setHiddenMouse(true), 1000); return { hiddenMouse, /** 鼠标移动 */ onMouseMove() { setHiddenMouse(false); hidden(); } }; }; const useStyle = css => solidJs.onMount(() => helper.useStyle(css, refs.root)); const useStyleMemo = (selector, styleMapArg) => solidJs.onMount(() => helper.useStyleMemo(selector, styleMapArg, refs.root)); const ComicImg = img => { const showState = () => imgShowState().get(img.index); const src = () => { if (img.loadType === 'wait') return ''; if (img.translationType === 'show') return img.translationUrl; if (store.option.imgRecognition.enabled) { if (store.option.imgRecognition.upscale && img.upscaleUrl) return img.upscaleUrl; return img.blobUrl; } // 有些浏览器不支持显示带有 hash 标识的图片 url if (img.src.startsWith('blob:')) return img.src.replace(/#\\..+/, ''); return img.src; }; /** 并排卷轴模式下需要复制的图片数量 */ const cloneNum = solidJs.createMemo(() => { if (!isAbreastMode()) return 0; const imgPosition = abreastArea().position[img.index]; return imgPosition ? imgPosition.length - 1 : 0; }); /** 是否要渲染复制图片 */ const renderClone = () => !store.gridMode && showState() !== undefined && cloneNum() > 0; const selector = \`.\${modules_c21c94f2$1.img}[id^="_\${img.index}_"]\`; useStyleMemo(selector, { 'grid-area': () => isAbreastMode() && !store.gridMode ? 'none' : \`_\${img.index}\`, 'background-color': () => isEnableBg() ? img.background : undefined }); useStyleMemo(\`\${selector} > picture\`, { 'aspect-ratio': () => \`\${img.size.width} / \${img.size.height}\`, background: () => img.progress && \`linear-gradient( to bottom, var(--secondary-bg) \${img.progress}%, var(--hover-bg-color,#fff3) \${img.progress}% )\` }); const _ComicImg = props => (() => { var _el$ = web.template(\`