|
1472 | 1472 | loadGoogleFont(urlParams.get("googlefont")); |
1473 | 1473 | } |
1474 | 1474 |
|
1475 | | - var timeoutDelay = 0; |
1476 | | - if (urlParams.has("showtime")) { |
1477 | | - timeoutDelay = parseInt(urlParams.get("showtime")); |
1478 | | - if (isNaN(timeoutDelay)) { timeoutDelay = 20000; } |
1479 | | - } |
| 1475 | + var timeoutDelay = 0; |
| 1476 | + if (urlParams.has("showtime")) { |
| 1477 | + timeoutDelay = parseInt(urlParams.get("showtime")); |
| 1478 | + if (isNaN(timeoutDelay)) { timeoutDelay = 20000; } |
| 1479 | + } |
| 1480 | + |
| 1481 | + var hideAfterTTS = urlParams.has("hideaftertts"); |
| 1482 | + var autoHideByLength = urlParams.has("autohide") || urlParams.has("autotime"); |
| 1483 | + var minimumAutoHideDelay = parseInt(urlParams.get("mintime"), 10); |
| 1484 | + if (isNaN(minimumAutoHideDelay) || minimumAutoHideDelay < 0) { minimumAutoHideDelay = 4000; } |
| 1485 | + var maximumAutoHideDelay = parseInt(urlParams.get("maxtime"), 10); |
| 1486 | + if (isNaN(maximumAutoHideDelay) || maximumAutoHideDelay < minimumAutoHideDelay) { maximumAutoHideDelay = 30000; } |
| 1487 | + var hideAfterTTSDelay = parseInt(urlParams.get("hidedelay"), 10); |
| 1488 | + if (isNaN(hideAfterTTSDelay) || hideAfterTTSDelay < 0) { hideAfterTTSDelay = 500; } |
| 1489 | + var hideAfterTTSFallback = parseInt(urlParams.get("ttstimeout"), 10); |
| 1490 | + if (isNaN(hideAfterTTSFallback) || hideAfterTTSFallback < 1000) { hideAfterTTSFallback = 120000; } |
1480 | 1491 |
|
1481 | 1492 | var borderRadius = 0; |
1482 | 1493 | if (urlParams.has("rounded")) { |
|
1947 | 1958 | } |
1948 | 1959 | } |
1949 | 1960 |
|
1950 | | - var iframeTimeout = null; |
1951 | | - var lastMessageId = null |
1952 | | - |
1953 | | - function processData(data) { |
| 1961 | + var iframeTimeout = null; |
| 1962 | + var lastMessageId = null; |
| 1963 | + var botDisplayToken = 0; |
| 1964 | + var ttsCompletionTimer = null; |
| 1965 | + |
| 1966 | + function getBotMessageText(contents) { |
| 1967 | + var container = document.createElement("div"); |
| 1968 | + container.innerHTML = contents && contents.chatmessage ? String(contents.chatmessage) : ""; |
| 1969 | + return (container.textContent || container.innerText || "").replace(/\s+/g, " ").trim(); |
| 1970 | + } |
| 1971 | + |
| 1972 | + function getBotAutoHideDelay(contents) { |
| 1973 | + var text = getBotMessageText(contents); |
| 1974 | + var wordCount = text ? text.split(/\s+/).length : 0; |
| 1975 | + var estimatedDelay = 1500 + (wordCount * 350); |
| 1976 | + return Math.max(minimumAutoHideDelay, Math.min(maximumAutoHideDelay, estimatedDelay)); |
| 1977 | + } |
| 1978 | + |
| 1979 | + function botTTSIsActive() { |
| 1980 | + try { |
| 1981 | + if (window.speechSynthesis && (window.speechSynthesis.pending || window.speechSynthesis.speaking)) return true; |
| 1982 | + if (typeof TTS !== "undefined" && TTS.premiumQueueActive) return true; |
| 1983 | + if (typeof TTS !== "undefined" && TTS.audio && !TTS.audio.paused && !TTS.audio.ended) return true; |
| 1984 | + } catch (e) {} |
| 1985 | + return false; |
| 1986 | + } |
| 1987 | + |
| 1988 | + function clearBotOverlay(options) { |
| 1989 | + options = options || {}; |
| 1990 | + botDisplayToken += 1; |
| 1991 | + lastMessageId = null; |
| 1992 | + if (timeoutTimer) { |
| 1993 | + timeoutTimer.clear(); |
| 1994 | + timeoutTimer = null; |
| 1995 | + } |
| 1996 | + if (ttsCompletionTimer) { |
| 1997 | + ttsCompletionTimer.clear(); |
| 1998 | + ttsCompletionTimer = null; |
| 1999 | + } |
| 2000 | + if (nextComment) { |
| 2001 | + nextComment.clear(); |
| 2002 | + nextComment = null; |
| 2003 | + } |
| 2004 | + if (options.clearQueue) { |
| 2005 | + queue = []; |
| 2006 | + if (queueTimer) { |
| 2007 | + queueTimer.clear(); |
| 2008 | + queueTimer = null; |
| 2009 | + } |
| 2010 | + } |
| 2011 | + document.getElementById("output").classList.add(transitionType); |
| 2012 | + if (options.advanceQueue) checkqueue(true); |
| 2013 | + |
| 2014 | + if (isIFrame) { |
| 2015 | + clearTimeout(iframeTimeout); |
| 2016 | + iframeTimeout = createReliableTimer(function () { |
| 2017 | + parent.postMessage({ resizeWindow: { height: "0px" } }, "*"); |
| 2018 | + }, 300); |
| 2019 | + } |
| 2020 | + sendToDisk(false); |
| 2021 | + return true; |
| 2022 | + } |
| 2023 | + |
| 2024 | + function hideBotOverlayForToken(displayToken, advanceQueue) { |
| 2025 | + if (displayToken !== botDisplayToken) return false; |
| 2026 | + clearBotOverlay({ advanceQueue: !!advanceQueue }); |
| 2027 | + return true; |
| 2028 | + } |
| 2029 | + |
| 2030 | + function waitForBotTTSToFinish(displayToken, contents) { |
| 2031 | + var startedAt = Date.now(); |
| 2032 | + var sawPlayback = botTTSIsActive(); |
| 2033 | + var startupGrace = 2500; |
| 2034 | + |
| 2035 | + function checkPlayback() { |
| 2036 | + if (displayToken !== botDisplayToken) return; |
| 2037 | + var active = botTTSIsActive(); |
| 2038 | + if (active) sawPlayback = true; |
| 2039 | + if (sawPlayback && !active) { |
| 2040 | + timeoutTimer = createReliableTimer(function () { |
| 2041 | + hideBotOverlayForToken(displayToken, true); |
| 2042 | + }, hideAfterTTSDelay); |
| 2043 | + return; |
| 2044 | + } |
| 2045 | + if (!sawPlayback && Date.now() - startedAt >= startupGrace) { |
| 2046 | + timeoutTimer = createReliableTimer(function () { |
| 2047 | + hideBotOverlayForToken(displayToken, true); |
| 2048 | + }, getBotAutoHideDelay(contents)); |
| 2049 | + return; |
| 2050 | + } |
| 2051 | + if (Date.now() - startedAt >= hideAfterTTSFallback) { |
| 2052 | + hideBotOverlayForToken(displayToken, true); |
| 2053 | + return; |
| 2054 | + } |
| 2055 | + ttsCompletionTimer = createReliableTimer(checkPlayback, 100); |
| 2056 | + } |
| 2057 | + |
| 2058 | + ttsCompletionTimer = createReliableTimer(checkPlayback, 100); |
| 2059 | + } |
| 2060 | + |
| 2061 | + function processData(data) { |
1954 | 2062 | if (pseudodock) { |
1955 | 2063 | if (data && !data.contents) { |
1956 | 2064 | var tmp = {}; |
|
2033 | 2141 | } |
2034 | 2142 |
|
2035 | 2143 | return true; |
2036 | | - } else if (data && ("contents" in data)) { // clear |
2037 | | - lastMessageId = null; |
2038 | | - if (timeoutTimer) { |
2039 | | - timeoutTimer.clear(); |
2040 | | - timeoutTimer = null; |
2041 | | - } |
2042 | | - document.getElementById("output").classList.add(transitionType); |
2043 | | - if (nextComment) { |
2044 | | - nextComment.clear(); |
2045 | | - nextComment = null; |
2046 | | - } |
2047 | | - |
2048 | | - checkqueue(true); |
2049 | | - |
2050 | | - if (isIFrame) { |
2051 | | - clearTimeout(iframeTimeout); |
2052 | | - iframeTimeout = createReliableTimer(function () { |
2053 | | - parent.postMessage({ resizeWindow: { height: "0px" } }, "*"); |
2054 | | - }, 300); |
2055 | | - } |
2056 | | - |
2057 | | - sendToDisk(data.contents); |
2058 | | - return true; |
2059 | | - } else if (data.action) { |
2060 | | - console.log(data); |
2061 | | - if (data.action === "toggleTTS" || data.action === "tts") { |
| 2144 | + } else if (data && ("contents" in data)) { // clear |
| 2145 | + return clearBotOverlay({ advanceQueue: true }); |
| 2146 | + } else if (data.action) { |
| 2147 | + console.log(data); |
| 2148 | + if (data.action === "clearBotOverlay") { |
| 2149 | + return clearBotOverlay({ clearQueue: true }); |
| 2150 | + } else if (data.action === "toggleTTS" || data.action === "tts") { |
2062 | 2151 | if ("value" in data) { |
2063 | 2152 | if (data.value == "toggle") { |
2064 | 2153 | TTS.toggle(); |
|
2106 | 2195 | } |
2107 | 2196 | } |
2108 | 2197 |
|
2109 | | - function showmessage(contents) { |
2110 | | - try { |
2111 | | - // Clear any existing timers first to prevent memory leaks |
2112 | | - if (timeoutTimer) { |
2113 | | - timeoutTimer.clear(); |
2114 | | - timeoutTimer = null; |
2115 | | - } |
| 2198 | + function showmessage(contents) { |
| 2199 | + try { |
| 2200 | + var displayToken = ++botDisplayToken; |
| 2201 | + // Clear any existing timers first to prevent memory leaks |
| 2202 | + if (timeoutTimer) { |
| 2203 | + timeoutTimer.clear(); |
| 2204 | + timeoutTimer = null; |
| 2205 | + } |
| 2206 | + if (ttsCompletionTimer) { |
| 2207 | + ttsCompletionTimer.clear(); |
| 2208 | + ttsCompletionTimer = null; |
| 2209 | + } |
2116 | 2210 |
|
2117 | 2211 | if (nextComment) { |
2118 | 2212 | nextComment.clear(); |
|
2462 | 2556 | console.log("TTS setting changed to:", TTS.speech); |
2463 | 2557 | } |
2464 | 2558 |
|
2465 | | - if (TTS.speech) { |
2466 | | - TTS.initAudioContext(); |
2467 | | - console.log("TTS speaking:", data.chatname, data.chatmessage); |
2468 | | - TTS.speechMeta(data, true); // Add true as the second parameter to force speech |
2469 | | - } |
2470 | | - |
2471 | | - if (timeoutDelay) { |
2472 | | - if (timeoutTimer) { |
2473 | | - timeoutTimer.clear(); |
2474 | | - timeoutTimer = null; |
2475 | | - } |
2476 | | - timeoutTimer = createReliableTimer(function () { |
2477 | | - lastMessageId = null; |
2478 | | - document.getElementById("output").classList.add(transitionType); |
2479 | | - if (nextComment) { |
2480 | | - nextComment.clear(); |
2481 | | - nextComment = null; |
2482 | | - } |
2483 | | - checkqueue(true); |
2484 | | - }, timeoutDelay); |
2485 | | - } |
| 2559 | + if (TTS.speech) { |
| 2560 | + TTS.initAudioContext(); |
| 2561 | + console.log("TTS speaking:", data.chatname, data.chatmessage); |
| 2562 | + TTS.speechMeta(data, true); // Add true as the second parameter to force speech |
| 2563 | + } |
| 2564 | + |
| 2565 | + if (hideAfterTTS && TTS.speech) { |
| 2566 | + waitForBotTTSToFinish(displayToken, data); |
| 2567 | + } else if (autoHideByLength || timeoutDelay) { |
| 2568 | + if (timeoutTimer) { |
| 2569 | + timeoutTimer.clear(); |
| 2570 | + timeoutTimer = null; |
| 2571 | + } |
| 2572 | + timeoutTimer = createReliableTimer(function () { |
| 2573 | + hideBotOverlayForToken(displayToken, true); |
| 2574 | + }, autoHideByLength ? getBotAutoHideDelay(data) : timeoutDelay); |
| 2575 | + } |
2486 | 2576 | }, |
2487 | 2577 | 500, // give time for the previous featured message to go fade/drop away |
2488 | 2578 | contents |
|
0 commit comments