mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 09:33:55 +08:00
优化:拆分test_page.html,把一部分opus和document操作的代码提取出来,减少单个文件大小
This commit is contained in:
@@ -26,3 +26,14 @@ export function getLogContainer (flan) {
|
|||||||
return logContainer;
|
return logContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新Opus库状态显示
|
||||||
|
export function updateScriptStatus(message, type) {
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) {
|
||||||
|
statusElement.textContent = message;
|
||||||
|
statusElement.className = `script-status ${type}`;
|
||||||
|
statusElement.style.display = 'block';
|
||||||
|
statusElement.style.width = 'auto';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { log } from './utils/logger.js';
|
||||||
|
import { updateScriptStatus } from './document.js'
|
||||||
|
|
||||||
|
|
||||||
|
// 检查Opus库是否已加载
|
||||||
|
export function checkOpusLoaded() {
|
||||||
|
try {
|
||||||
|
// 检查Module是否存在(本地库导出的全局变量)
|
||||||
|
if (typeof Module === 'undefined') {
|
||||||
|
throw new Error('Opus库未加载,Module对象不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试先使用Module.instance(libopus.js最后一行导出方式)
|
||||||
|
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
||||||
|
// 使用Module.instance对象替换全局Module对象
|
||||||
|
window.ModuleInstance = Module.instance;
|
||||||
|
log('Opus库加载成功(使用Module.instance)', 'success');
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有Module.instance,检查全局Module函数
|
||||||
|
if (typeof Module._opus_decoder_get_size === 'function') {
|
||||||
|
window.ModuleInstance = Module;
|
||||||
|
log('Opus库加载成功(使用全局Module)', 'success');
|
||||||
|
updateScriptStatus('Opus库加载成功', 'success');
|
||||||
|
|
||||||
|
// 3秒后隐藏状态
|
||||||
|
const statusElement = document.getElementById('scriptStatus');
|
||||||
|
if (statusElement) statusElement.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
||||||
|
} catch (err) {
|
||||||
|
log(`Opus库加载失败,请检查libopus.js文件是否存在且正确: ${err.message}`, 'error');
|
||||||
|
updateScriptStatus('Opus库加载失败,请检查libopus.js文件是否存在且正确', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -102,6 +102,7 @@
|
|||||||
<script type="module">
|
<script type="module">
|
||||||
import { log } from './js/utils/logger.js';
|
import { log } from './js/utils/logger.js';
|
||||||
import { webSocketConnect } from './js/xiaoZhiConnect.js';
|
import { webSocketConnect } from './js/xiaoZhiConnect.js';
|
||||||
|
import { checkOpusLoaded } from './js/opus.js';
|
||||||
// 需要加载的脚本列表 - 移除Opus依赖
|
// 需要加载的脚本列表 - 移除Opus依赖
|
||||||
const scriptFiles = [];
|
const scriptFiles = [];
|
||||||
|
|
||||||
@@ -113,57 +114,6 @@
|
|||||||
total: scriptFiles.length
|
total: scriptFiles.length
|
||||||
};
|
};
|
||||||
|
|
||||||
// 检查Opus库是否已加载
|
|
||||||
function checkOpusLoaded() {
|
|
||||||
try {
|
|
||||||
// 检查Module是否存在(本地库导出的全局变量)
|
|
||||||
if (typeof Module === 'undefined') {
|
|
||||||
throw new Error('Opus库未加载,Module对象不存在');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试先使用Module.instance(libopus.js最后一行导出方式)
|
|
||||||
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
|
||||||
// 使用Module.instance对象替换全局Module对象
|
|
||||||
window.ModuleInstance = Module.instance;
|
|
||||||
log('Opus库加载成功(使用Module.instance)', 'success');
|
|
||||||
updateScriptStatus('Opus库加载成功', 'success');
|
|
||||||
|
|
||||||
// 3秒后隐藏状态
|
|
||||||
const statusElement = document.getElementById('scriptStatus');
|
|
||||||
if (statusElement) statusElement.style.display = 'none';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果没有Module.instance,检查全局Module函数
|
|
||||||
if (typeof Module._opus_decoder_get_size === 'function') {
|
|
||||||
window.ModuleInstance = Module;
|
|
||||||
log('Opus库加载成功(使用全局Module)', 'success');
|
|
||||||
updateScriptStatus('Opus库加载成功', 'success');
|
|
||||||
|
|
||||||
// 3秒后隐藏状态
|
|
||||||
const statusElement = document.getElementById('scriptStatus');
|
|
||||||
if (statusElement) statusElement.style.display = 'none';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
|
||||||
} catch (err) {
|
|
||||||
log(`Opus库加载失败,请检查libopus.js文件是否存在且正确: ${err.message}`, 'error');
|
|
||||||
updateScriptStatus('Opus库加载失败,请检查libopus.js文件是否存在且正确', 'error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新脚本状态显示
|
|
||||||
function updateScriptStatus(message, type) {
|
|
||||||
const statusElement = document.getElementById('scriptStatus');
|
|
||||||
if (statusElement) {
|
|
||||||
statusElement.textContent = message;
|
|
||||||
statusElement.className = `script-status ${type}`;
|
|
||||||
statusElement.style.display = 'block';
|
|
||||||
statusElement.style.width = 'auto';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 全局变量
|
// 全局变量
|
||||||
let websocket = null;
|
let websocket = null;
|
||||||
let mediaRecorder = null;
|
let mediaRecorder = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user