From 7a56afd222bf04701cfaa04641a05ddbf7a56069 Mon Sep 17 00:00:00 2001 From: Himanshu <144930708+Himanshu4812@users.noreply.github.com> Date: Sun, 25 Jan 2026 16:47:45 +0530 Subject: [PATCH] Fix Translate plugin crash in preview mode --- src/plugins/translate/plugin.translate.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/plugins/translate/plugin.translate.js b/src/plugins/translate/plugin.translate.js index b68eec3ff..1fa27d67a 100644 --- a/src/plugins/translate/plugin.translate.js +++ b/src/plugins/translate/plugin.translate.js @@ -152,6 +152,10 @@ export class TranslatePlugin extends BookReaderPlugin { } /** @type {HTMLElement} textLayerElement */ const textLayerElement = page.querySelector('.BRtextLayer'); + if (!textLayerElement) { + console.warn("Translate unavailable: no text layer (preview mode?)"); + return; + } // Should use native DOM element.style method instead of $().css method, specific issue with rendering / style calculation in Chrome $(pageTranslationLayer).css({ "width": textLayerElement.style.width, @@ -307,6 +311,12 @@ export class TranslatePlugin extends BookReaderPlugin { } handleToggleTranslation = async () => { + const hasTextLayer = document.querySelector('.BRtextLayer'); + if (!hasTextLayer) { + alert("Please borrow the book to use translation."); + return; + } + this.userToggleTranslate = !this.userToggleTranslate; this.translationManager.active = this.userToggleTranslate;