UNPKG

r2-navigator-js

Version:

Readium 2 'navigator' for NodeJS (TypeScript)

450 lines 17.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TTSStateEnum = void 0; exports.checkTtsState = checkTtsState; exports.playTtsOnReadingLocation = playTtsOnReadingLocation; exports.ttsHandleIpcMessage = ttsHandleIpcMessage; exports.ttsListen = ttsListen; exports.ttsPlay = ttsPlay; exports.ttsPause = ttsPause; exports.ttsStop = ttsStop; exports.ttsResume = ttsResume; exports.ttsPrevious = ttsPrevious; exports.ttsNext = ttsNext; exports.ttsOverlayEnable = ttsOverlayEnable; exports.ttsClickEnable = ttsClickEnable; exports.ttsVoices = ttsVoices; exports.ttsVoice = ttsVoice; exports.ttsPlaybackRate = ttsPlaybackRate; exports.ttsAndMediaOverlaysManualPlayNext = ttsAndMediaOverlaysManualPlayNext; exports.ttsSkippabilityEnable = ttsSkippabilityEnable; exports.ttsSentenceDetectionEnable = ttsSentenceDetectionEnable; exports.ttsHighlightStyle = ttsHighlightStyle; const tslib_1 = require("tslib"); const debounce = require("debounce"); const events_1 = require("../common/events"); const location_1 = require("./location"); const win = global.window; let _lastTTSWebView; let _lastTTSWebViewHref; let _ttsAutoPlayTimeout; function checkTtsState(wv) { let wasStopped = false; if (_lastTTSWebView && _lastTTSWebViewHref) { if (win.READIUM2.ttsClickEnabled || !win.READIUM2.getActiveWebViews().includes(_lastTTSWebView) || !win.READIUM2.getActiveWebViews().find((webview) => { var _a; return ((_a = webview.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) === _lastTTSWebViewHref; })) { wasStopped = true; setTimeout(() => { win.speechSynthesis.cancel(); }, 0); _lastTTSWebView = undefined; _lastTTSWebViewHref = undefined; if (_ttsListener) { _ttsListener(TTSStateEnum.STOPPED); } } } checkTtsStateDebounced(wasStopped, wv); } const checkTtsStateDebounced = debounce(checkTtsStateRaw, 400); function checkTtsStateRaw(wasStopped, wv) { var _a; if (wasStopped || win.READIUM2.ttsClickEnabled) { if ((_a = wv.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) { if (_ttsAutoPlayTimeout !== undefined) { win.clearTimeout(_ttsAutoPlayTimeout); _ttsAutoPlayTimeout = undefined; } _ttsAutoPlayTimeout = win.setTimeout(() => { var _a, _b; _ttsAutoPlayTimeout = undefined; if (((_a = wv.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) && (!_lastTTSWebView || (wasStopped && _lastTTSWebViewHref === ((_b = wv.READIUM2.link) === null || _b === void 0 ? void 0 : _b.Href)))) { _lastTTSWebView = wv; _lastTTSWebViewHref = wv.READIUM2.link.Href; playTtsOnReadingLocation(wv.READIUM2.link.Href); } }, 100); } } } function playTtsOnReadingLocation(href) { const activeWebView = win.READIUM2.getActiveWebViews().find((webview) => { var _a; return ((_a = webview.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) === href; }); if (activeWebView) { let done = false; const cb = (event) => { var _a; if (event.channel === events_1.R2_EVENT_READING_LOCATION) { const webview = event.currentTarget; if (webview !== activeWebView) { console.log("Wrong navigator webview?!"); return; } done = true; activeWebView.removeEventListener("ipc-message", cb); if (((_a = activeWebView.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) === href) { ttsPlay(win.READIUM2.ttsPlaybackRate, undefined); } } }; setTimeout(() => { if (done) { return; } try { activeWebView.removeEventListener("ipc-message", cb); } catch (err) { console.log(err); } const activeWebView_ = win.READIUM2.getActiveWebViews().find((webview) => { var _a; return ((_a = webview.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href) === href; }); if (activeWebView_) { ttsPlay(win.READIUM2.ttsPlaybackRate, undefined); } }, 1000); activeWebView.addEventListener("ipc-message", cb); } } function ttsHandleIpcMessage(eventChannel, _eventArgs, eventCurrentTarget) { var _a, _b; if (eventChannel === events_1.R2_EVENT_TTS_IS_PAUSED) { _lastTTSWebView = eventCurrentTarget; _lastTTSWebViewHref = (_a = eventCurrentTarget.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href; if (_ttsListener) { _ttsListener(TTSStateEnum.PAUSED); } } else if (eventChannel === events_1.R2_EVENT_TTS_IS_STOPPED) { _lastTTSWebView = undefined; _lastTTSWebViewHref = undefined; if (_ttsListener) { _ttsListener(TTSStateEnum.STOPPED); } } else if (eventChannel === events_1.R2_EVENT_TTS_IS_PLAYING) { _lastTTSWebView = eventCurrentTarget; _lastTTSWebViewHref = (_b = eventCurrentTarget.READIUM2.link) === null || _b === void 0 ? void 0 : _b.Href; if (_ttsListener) { _ttsListener(TTSStateEnum.PLAYING); } } else if (eventChannel === events_1.R2_EVENT_TTS_DOC_END) { (0, location_1.navPreviousOrNext)(false, true, true); } else if (eventChannel === events_1.R2_EVENT_TTS_DOC_BACK) { (0, location_1.navPreviousOrNext)(true, true, true); } else { return false; } return true; } var TTSStateEnum; (function (TTSStateEnum) { TTSStateEnum["PAUSED"] = "PAUSED"; TTSStateEnum["PLAYING"] = "PLAYING"; TTSStateEnum["STOPPED"] = "STOPPED"; })(TTSStateEnum || (exports.TTSStateEnum = TTSStateEnum = {})); let _ttsListener; function ttsListen(ttsListener) { _ttsListener = ttsListener; } function ttsPlay(speed, voice) { var _a, _b; if (!win.READIUM2) { return; } win.READIUM2.ttsPlaybackRate = speed; if (typeof voice === "undefined") { } else if (voice && Array.isArray(voice)) { ttsVoices(voice); } else { ttsVoice(voice); } let startElementCSSSelector; let startElementRangeInfo; const loc = (0, location_1.getCurrentReadingLocation)(); let activeWebView = win.READIUM2.getActiveWebViews().find((webview) => { var _a; return loc && loc.locator.href && loc.locator.href === ((_a = webview.READIUM2.link) === null || _a === void 0 ? void 0 : _a.Href); }); if (loc && activeWebView) { startElementCSSSelector = loc.locator.locations.cssSelector; } if (loc && activeWebView) { startElementRangeInfo = (_a = loc.locator.locations.caretInfo) === null || _a === void 0 ? void 0 : _a.rangeInfo; } if (!activeWebView) { activeWebView = win.READIUM2.getFirstWebView(); } _lastTTSWebView = activeWebView; _lastTTSWebViewHref = undefined; if (!activeWebView) { return; } _lastTTSWebViewHref = (_b = activeWebView.READIUM2.link) === null || _b === void 0 ? void 0 : _b.Href; const payload = { rootElement: "html > body", speed, startElement: startElementCSSSelector, rangeInfo: startElementRangeInfo, voices: win.READIUM2.ttsVoices, }; setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if (activeWebView) { if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_PLAY, payload); } } }), 0); } function ttsPause() { const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { if (_lastTTSWebView && _lastTTSWebView !== activeWebView) { continue; } setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_PAUSE); } }), 0); } } function ttsStop() { const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { if (_lastTTSWebView && _lastTTSWebView !== activeWebView) { continue; } _lastTTSWebView = undefined; _lastTTSWebViewHref = undefined; setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_STOP); } }), 0); } } function ttsResume() { const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { if (_lastTTSWebView && _lastTTSWebView !== activeWebView) { continue; } setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_RESUME); } }), 0); } } function ttsPrevious(skipSentences, escape = false) { const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { if (_lastTTSWebView && _lastTTSWebView !== activeWebView) { continue; } setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { skipSentences, escape, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_PREVIOUS, payload); } }), 0); } } function ttsNext(skipSentences, escape = false) { const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { if (_lastTTSWebView && _lastTTSWebView !== activeWebView) { continue; } setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { skipSentences, escape, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_DO_NEXT, payload); } }), 0); } } function ttsOverlayEnable(doEnable) { if (win.READIUM2) { win.READIUM2.ttsOverlayEnabled = doEnable; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { doEnable, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_OVERLAY_ENABLE, payload); } }), 0); } } function ttsClickEnable(doEnable) { if (win.READIUM2) { win.READIUM2.ttsClickEnabled = doEnable; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { doEnable, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_CLICK_ENABLE, payload); } }), 0); } } function ttsVoices(voices) { if (win.READIUM2) { win.READIUM2.ttsVoices = voices; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { const payload = { voices, }; setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_VOICE, payload); } }), 0); } } function ttsVoice(voice) { if (win.READIUM2) { if (voice === null) { win.READIUM2.ttsVoices = null; ttsVoices(win.READIUM2.ttsVoices); } else if (voice) { if (!win.READIUM2.ttsVoices) { win.READIUM2.ttsVoices = []; } win.READIUM2.ttsVoices = win.READIUM2.ttsVoices.filter((v) => v.lang !== voice.lang); win.READIUM2.ttsVoices.push(voice); ttsVoices(win.READIUM2.ttsVoices); } } } function ttsPlaybackRate(speed) { if (win.READIUM2) { win.READIUM2.ttsPlaybackRate = speed; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { const payload = { speed, }; setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_PLAYBACK_RATE, payload); } }), 0); } } function ttsAndMediaOverlaysManualPlayNext(doEnable) { if (win.READIUM2) { win.READIUM2.ttsAndMediaOverlaysManualPlayNext = doEnable; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { doEnable, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_MEDIAOVERLAYS_MANUAL_PLAY_NEXT, payload); } }), 0); } } function ttsSkippabilityEnable(doEnable) { if (win.READIUM2) { win.READIUM2.ttsSkippabilityEnabled = doEnable; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { doEnable, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_SKIP_ENABLE, payload); } }), 0); } } function ttsSentenceDetectionEnable(doEnable) { if (win.READIUM2) { win.READIUM2.ttsSentenceDetectionEnabled = doEnable; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { doEnable, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_SENTENCE_DETECT_ENABLE, payload); } }), 0); } } function ttsHighlightStyle(ttsHighlightStyle, ttsHighlightColor, ttsHighlightStyle_WORD, ttsHighlightColor_WORD) { if (win.READIUM2) { win.READIUM2.ttsHighlightStyle = ttsHighlightStyle; win.READIUM2.ttsHighlightColor = ttsHighlightColor; win.READIUM2.ttsHighlightStyle_WORD = ttsHighlightStyle_WORD; win.READIUM2.ttsHighlightColor_WORD = ttsHighlightColor_WORD; } const activeWebViews = win.READIUM2.getActiveWebViews(); for (const activeWebView of activeWebViews) { setTimeout(() => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const payload = { ttsHighlightStyle, ttsHighlightColor, ttsHighlightStyle_WORD, ttsHighlightColor_WORD, }; if ((_a = activeWebView.READIUM2) === null || _a === void 0 ? void 0 : _a.DOMisReady) { yield activeWebView.send(events_1.R2_EVENT_TTS_HIGHLIGHT_STYLE, payload); } }), 0); } } //# sourceMappingURL=readaloud.js.map