Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ let hasMicPermission = false;
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const synth = window.speechSynthesis;

const directoryUrl = window.__UNITY_VOICE_DIRECTORY_URL__
?? (() => {
const href = window.location.href;
const pathname = window.location.pathname || '';
const lastSegment = pathname.substring(pathname.lastIndexOf('/') + 1);

if (href.endsWith('/')) {
return href;
}

if (lastSegment && lastSegment.includes('.')) {
return href.substring(0, href.lastIndexOf('/') + 1);
}

return `${href}/`;
})();

function resolveAssetPath(relativePath) {
try {
return new URL(relativePath, directoryUrl).toString();
} catch (error) {
console.error('Failed to resolve asset path:', error);
return relativePath;
}
}

window.addEventListener('load', async () => {
await loadSystemPrompt();
setupSpeechRecognition();
Expand All @@ -37,7 +63,7 @@ function setCircleState(circle, { speaking = false, listening = false, error = f

async function loadSystemPrompt() {
try {
const response = await fetch('ai-instruct.txt');
const response = await fetch(resolveAssetPath('ai-instruct.txt'));
systemPrompt = await response.text();
} catch (error) {
console.error('Error fetching system prompt:', error);
Expand Down
37 changes: 34 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unity Voice Chat</title>
<link rel="stylesheet" href="style.css">
<script>
(() => {
const href = window.location.href;
const pathname = window.location.pathname || '';
const lastSegment = pathname.substring(pathname.lastIndexOf('/') + 1);

const directoryUrl = href.endsWith('/')
? href
: lastSegment && lastSegment.includes('.')
? href.substring(0, href.lastIndexOf('/') + 1)
: `${href}/`;

window.__UNITY_VOICE_DIRECTORY_URL__ = directoryUrl;

const resolveAsset = (relativePath) => {
try {
return new URL(relativePath, directoryUrl).toString();
} catch (error) {
console.error('Failed to resolve asset path:', error);
return relativePath;
}
};

const styleLink = document.createElement('link');
styleLink.rel = 'stylesheet';
styleLink.href = resolveAsset('style.css');
document.head.appendChild(styleLink);

const script = document.createElement('script');
script.defer = true;
script.src = resolveAsset('app.js');
document.head.appendChild(script);
})();
</script>
</head>
<body>
<div id="background" aria-hidden="true"></div>
Expand All @@ -23,7 +56,5 @@
<button id="mute-indicator" class="mute-indicator" data-state="muted" type="button">
<span class="indicator-text">Tap or click anywhere to unmute</span>
</button>

<script src="app.js"></script>
</body>
</html>