This commit is contained in:
eric
2026-03-22 23:29:04 -05:00
parent a4b7edd153
commit cc552d49f0
2 changed files with 151 additions and 42 deletions

View File

@@ -84,16 +84,36 @@ button.loading::after{content:" ⏳";animation:spin 1s linear infinite}
<pre id="pm2Log">正在加载状态与日志...</pre>
</div>
<script>
/**
* 监控目标:本分支用 *2.py / web2 / obs2 等与「无 2」分支区分常与 Ubuntu+PM2 配套。
* 与 web2.py 中 PROCESS_MONITOR / VALID_PROCESSES 一致PM2 应用名script 为入口脚本文件名。
* 若改了 PM2 进程名,请同步改 pm2 字段与 web2.py。
*/
const PROCESS_MONITOR = [
{ pm2: 'tiktok', script: 'tiktok2.py', label: 'TikTok' },
{ pm2: 'youtube', script: 'youtube2.py', label: 'YouTube' },
{ pm2: 'obs', script: 'obs2.sh', label: 'OBS' },
];
/** 进程列表仅来自 GET /process_monitorweb2.py 启动时扫描项目根目录自动识别PM2 名 = 脚本主文件名(无扩展名)。 */
let PROCESS_MONITOR = [];
function pm2NameFromScript(script) {
const base = String(script).split(/[/\\]/).pop() || '';
const i = base.lastIndexOf('.');
return i > 0 ? base.slice(0, i) : base;
}
function isYoutubeScript(script) {
return /\.py$/i.test(script) && pm2NameFromScript(script).startsWith('youtube');
}
function isTiktokScript(script) {
return /\.py$/i.test(script) && pm2NameFromScript(script).startsWith('tiktok');
}
function isObsScript(script) {
return pm2NameFromScript(script).startsWith('obs');
}
async function loadProcessMonitor() {
const res = await fetch('/process_monitor');
if (!res.ok) throw new Error(String(res.status));
const data = await res.json();
const entries = data.entries || [];
PROCESS_MONITOR = entries.map((e) => ({
script: e.script,
label: e.label || e.script,
pm2: e.pm2 || pm2NameFromScript(e.script),
}));
}
function getEntryByPm2(name) {
return PROCESS_MONITOR.find((e) => e.pm2 === name) || null;
@@ -112,8 +132,7 @@ function fillProcessSelect() {
const logEl = document.getElementById('pm2Log');
const statusIndicator = document.getElementById('statusIndicator');
const processSelect = document.getElementById('processSelect');
fillProcessSelect();
let currentProcess = PROCESS_MONITOR[0].pm2;
let currentProcess = '';
// 各专属区域
const youtubeOnlySection = document.getElementById('youtubeOnlySection');
@@ -147,9 +166,10 @@ processSelect.addEventListener('change', () => {
function updateSectionsVisibility() {
const ent = getEntryByPm2(currentProcess);
const isYoutube = ent && ent.script === 'youtube2.py';
const isTiktok = ent && ent.script === 'tiktok2.py';
const isObs = ent && ent.script === 'obs2.sh';
const s = ent ? ent.script : '';
const isYoutube = ent && isYoutubeScript(s);
const isTiktok = ent && isTiktokScript(s);
const isObs = ent && isObsScript(s);
youtubeOnlySection.style.display = isYoutube ? 'block' : 'none';
urlConfigSection.style.display = (isYoutube || isTiktok) ? 'block' : 'none';
@@ -291,11 +311,26 @@ document.getElementById('pm2Buttons').addEventListener('click', e=>{
if(action && action !== 'status') pm2Action(action);
});
// 自动刷新 + 初始
setInterval(fetchStatus, 1000);
refreshProcessScriptNote();
fetchStatus();
updateSectionsVisibility();
(async function boot() {
try {
await loadProcessMonitor();
} catch (e) {
logEl.textContent = '无法加载 /process_monitor请用本服务打开的页面访问或检查 web2 是否已启动)\n' + (e && e.message ? e.message : '');
statusIndicator.textContent = '❌ 未连接';
return;
}
if (!PROCESS_MONITOR.length) {
logEl.textContent = '未发现任何入口脚本(项目根应有 tiktok*.py / youtube*.py / obs*.sh且 Web 为当前 web*.py';
return;
}
fillProcessSelect();
currentProcess = PROCESS_MONITOR[0].pm2;
processSelect.value = currentProcess;
refreshProcessScriptNote();
setInterval(fetchStatus, 1000);
fetchStatus();
updateSectionsVisibility();
})();
</script>
</body>
</html>