From 7fe4d38c5184f36013becfc66cda5fffc22ff1a3 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Sun, 22 Jun 2025 17:22:39 +0800 Subject: [PATCH] fix: allow inline scripts on Tauri platforms (#1451) --- .../app/reader/components/FoliateViewer.tsx | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx index b8300c2c..6bc41c8e 100644 --- a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx +++ b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx @@ -30,12 +30,19 @@ import { import { getMaxInlineSize } from '@/utils/config'; import { getDirFromUILanguage } from '@/utils/rtl'; import { isCJKLang } from '@/utils/lang'; +import { isTauriAppPlatform } from '@/services/environment'; import { transformContent } from '@/services/transformService'; import { lockScreenOrientation } from '@/utils/bridge'; import { useTextTranslation } from '../hooks/useTextTranslation'; import { manageSyntaxHighlighting } from '@/utils/highlightjs'; import { getViewInsets } from '@/utils/insets'; +declare global { + interface Window { + eval(script: string): void; + } +} + const FoliateViewer: React.FC<{ bookKey: string; bookDoc: BookDoc; @@ -122,6 +129,11 @@ const FoliateViewer: React.FC<{ mountAdditionalFonts(detail.doc, isCJKLang(bookData.book?.primaryLanguage)); + // Inline scripts in tauri platforms are not executed by default + if (viewSettings.allowScript && isTauriAppPlatform()) { + evalInlineScripts(detail.doc); + } + // only call on load if we have highlighting turned on. if (viewSettings.codeHighlighting) { manageSyntaxHighlighting(detail.doc, viewSettings); @@ -144,6 +156,22 @@ const FoliateViewer: React.FC<{ } }; + const evalInlineScripts = (doc: Document) => { + if (doc.defaultView && doc.defaultView.frameElement) { + const iframe = doc.defaultView.frameElement as HTMLIFrameElement; + const scripts = doc.querySelectorAll('script:not([src])'); + scripts.forEach((script, index) => { + const scriptContent = script.textContent || script.innerHTML; + try { + console.warn('Evaluating inline scripts in iframe'); + iframe.contentWindow?.eval(scriptContent); + } catch (error) { + console.error(`Error executing iframe script ${index + 1}:`, error); + } + }); + } + }; + const docRelocateHandler = (event: Event) => { const detail = (event as CustomEvent).detail; if (detail.reason !== 'scroll' && detail.reason !== 'page') return;