xjs = require('xjs');
function log(str)
{
document.getElementById("log").insertAdjacentHTML('beforeend', escapeHTML(str) + "<br>\n");
}
function range(to) { return Array(to).fill().map((_, i) => i); }
function escapeHTML(str)
{
str = "" + str;
return str.replace(/[&<>'"]/g,
tag => ({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag]));
}
async function go()
{
log("Starting");
await xjs.ready();
log("Ready");
xjs.ExtensionWindow.on('scene-add', (i, uid) =>
{
checkScenes();
});
xjs.ExtensionWindow.on('scene-delete', (i, uid) =>
{
checkScenes();
});
let ws = null;
let sceneNames = [];
async function checkScenes(force = false)
{
const newSceneCount = await xjs.Scene.getSceneCount();
const newScenes = await Promise.all(range(newSceneCount).map(xjs.Scene.getBySceneIndex));
const newSceneNames = await Promise.all(newScenes.map(s => s.getName()));
if (force ||
sceneNames.length != newSceneNames.length ||
sceneNames.some((n, i) => n != newSceneNames[i]))
{
sceneNames = newSceneNames;
if (ws)
{
const data = JSON.stringify({scenes: sceneNames});
log("Sending: " + data);
ws.send(data);
}
}
}
function checkScenesTimer()
{
setTimeout(async () =>
{
await checkScenes();
checkScenesTimer();
}, 3000);
}
checkScenesTimer();
function doWebsocket()
{
ws = new WebSocket('ws://localhost:8080');
ws.onclose = () =>
{
ws.onclose = null;
ws.onmessage = null;
ws.onopen = null;
ws = null;
setTimeout(doWebsocket, 3000);
};
ws.onmessage = msg =>
{
try
{
const data = JSON.parse(msg.data);
log("Received: " + JSON.stringify(data));
if (data.set_scene !== undefined)
{
}
}
catch {}
};
ws.onopen = async () =>
{
log("Socket open");
checkScenes(true);
};
}
doWebsocket();
}
go();