Spaces:
Running
Running
File size: 54,748 Bytes
b39fb97 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
import '@tailwindcss/browser';
//Gemini 95 was fully vibe-coded by @ammaar and @olacombe, while we don't endorse code quality, we thought it was a fun demonstration of what's possible with the model when a Designer and PM jam.
//An homage to an OS that inspired so many of us!
// Define the dosInstances object to fix type errors
const dosInstances: Record<string, { initialized: boolean }> = {};
// --- DOM Element References ---
const desktop = document.getElementById('desktop') as HTMLDivElement;
const windows = document.querySelectorAll('.window') as NodeListOf<HTMLDivElement>;
const icons = document.querySelectorAll('.icon') as NodeListOf<HTMLDivElement>; // This is a NodeList
const startMenu = document.getElementById('start-menu') as HTMLDivElement;
const startButton = document.getElementById('start-button') as HTMLButtonElement;
const taskbarAppsContainer = document.getElementById('taskbar-apps') as HTMLDivElement;
const paintAssistant = document.getElementById('paint-assistant') as HTMLDivElement;
const assistantBubble = paintAssistant?.querySelector('.assistant-bubble') as HTMLDivElement;
// --- State Variables ---
let activeWindow: HTMLDivElement | null = null;
let highestZIndex: number = 20; // Start z-index for active windows
const openApps = new Map<string, { windowEl: HTMLDivElement; taskbarButton: HTMLDivElement }>(); // Store open apps and their elements
let geminiInstance: any | null = null; // Store the initialized Gemini AI instance
let paintCritiqueIntervalId: number | null = null; // Timer for paint critiques
// Store ResizeObservers to disconnect them later
const paintResizeObserverMap = new Map<Element, ResizeObserver>();
// --- Minesweeper Game State Variables ---
let minesweeperTimerInterval: number | null = null;
let minesweeperTimeElapsed: number = 0;
let minesweeperFlagsPlaced: number = 0;
let minesweeperGameOver: boolean = false;
let minesweeperMineCount: number = 10; // Default for 9x9
let minesweeperGridSize: { rows: number, cols: number } = { rows: 9, cols: 9 }; // Default 9x9
let minesweeperFirstClick: boolean = true; // To ensure first click is never a mine
// --- YouTube Player State ---
// @ts-ignore: YT will be defined by the YouTube API script
const youtubePlayers: Record<string, YT.Player | null> = {};
let ytApiLoaded = false;
let ytApiLoadingPromise: Promise<void> | null = null;
const DEFAULT_YOUTUBE_VIDEO_ID = 'WXuK6gekU1Y'; // Default video for GemPlayer ("Never Gonna Give You Up")
// --- Core Functions ---
/** Brings a window to the front and sets it as active */
function bringToFront(windowElement: HTMLDivElement): void {
if (activeWindow === windowElement) return; // Already active
if (activeWindow) {
activeWindow.classList.remove('active');
const appName = activeWindow.id;
if (openApps.has(appName)) {
openApps.get(appName)?.taskbarButton.classList.remove('active');
}
}
highestZIndex++;
windowElement.style.zIndex = highestZIndex.toString();
windowElement.classList.add('active');
activeWindow = windowElement;
const appNameRef = windowElement.id;
if (openApps.has(appNameRef)) {
openApps.get(appNameRef)?.taskbarButton.classList.add('active');
}
if ((appNameRef === 'doom' || appNameRef === 'wolf3d') && dosInstances[appNameRef]) {
const container = document.getElementById(`${appNameRef}-container`); // This ID might need checking
const canvas = container?.querySelector('canvas');
canvas?.focus();
}
}
/** Opens an application window */
async function openApp(appName: string): Promise<void> {
const windowElement = document.getElementById(appName) as HTMLDivElement | null;
if (!windowElement) {
console.error(`Window element not found for app: ${appName}`);
return;
}
if (openApps.has(appName)) {
bringToFront(windowElement);
windowElement.style.display = 'flex';
windowElement.classList.add('active');
return;
}
windowElement.style.display = 'flex';
windowElement.classList.add('active');
bringToFront(windowElement);
const taskbarButton = document.createElement('div');
taskbarButton.classList.add('taskbar-app');
taskbarButton.dataset.appName = appName;
let iconSrc = '';
let title = appName;
const iconElement = findIconElement(appName);
if (iconElement) {
const img = iconElement.querySelector('img');
const span = iconElement.querySelector('span');
if(img) iconSrc = img.src;
if(span) title = span.textContent || appName;
} else { // Fallback for apps opened via start menu but maybe no desktop icon
switch(appName) {
case 'myComputer': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/mycomputer.png'; title = 'My Gemtop'; break;
case 'chrome': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/chrome-icon-2.png'; title = 'Chrome'; break;
case 'notepad': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/GemNotes.png'; title = 'GemNotes'; break;
case 'paint': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/gempaint.png'; title = 'GemPaint'; break;
case 'doom': iconSrc = 'https://64.media.tumblr.com/1d89dfa76381e5c14210a2149c83790d/7a15f84c681c1cf9-c1/s540x810/86985984be99d5591e0cbc0dea6f05ffa3136dac.png'; title = 'Doom II'; break;
case 'gemini': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/GeminiChatRetro.png'; title = 'Gemini App'; break;
case 'minesweeper': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/gemsweeper.png'; title = 'GemSweeper'; break;
case 'imageViewer': iconSrc = 'https://win98icons.alexmeub.com/icons/png/display_properties-4.png'; title = 'Image Viewer'; break;
case 'mediaPlayer': iconSrc = 'https://storage.googleapis.com/gemini-95-icons/ytmediaplayer.png'; title = 'GemPlayer'; break;
}
}
if (iconSrc) {
const img = document.createElement('img');
img.src = iconSrc;
img.alt = title;
taskbarButton.appendChild(img);
}
taskbarButton.appendChild(document.createTextNode(title));
taskbarButton.addEventListener('click', () => {
if (windowElement === activeWindow && windowElement.style.display !== 'none') {
minimizeApp(appName);
} else {
windowElement.style.display = 'flex';
bringToFront(windowElement);
}
});
taskbarAppsContainer.appendChild(taskbarButton);
openApps.set(appName, { windowEl: windowElement, taskbarButton: taskbarButton });
taskbarButton.classList.add('active');
// Initialize specific applications
if (appName === 'chrome') {
initAiBrowser(windowElement);
}
else if (appName === 'notepad') {
await initNotepadStory(windowElement);
}
else if (appName === 'paint') {
initSimplePaintApp(windowElement);
if (paintAssistant) paintAssistant.classList.add('visible');
if (assistantBubble) assistantBubble.textContent = 'Warming up my judging circuits...';
if (paintCritiqueIntervalId) clearInterval(paintCritiqueIntervalId);
paintCritiqueIntervalId = window.setInterval(critiquePaintDrawing, 15000);
}
else if (appName === 'doom' && !dosInstances['doom']) {
const doomContainer = document.getElementById('doom-content') as HTMLDivElement;
if (doomContainer) {
doomContainer.innerHTML = '<iframe src="https://js-dos.com/games/doom.exe.html" width="100%" height="100%" frameborder="0" scrolling="no" allowfullscreen></iframe>';
dosInstances['doom'] = { initialized: true };
}
} else if (appName === 'gemini') {
await initGeminiChat(windowElement);
}
else if (appName === 'minesweeper') {
initMinesweeperGame(windowElement);
}
else if (appName === 'myComputer') {
initMyComputer(windowElement);
}
else if (appName === 'mediaPlayer') {
await initMediaPlayer(windowElement);
}
}
/** Closes an application window */
function closeApp(appName: string): void {
const appData = openApps.get(appName);
if (!appData) return;
const { windowEl, taskbarButton } = appData;
windowEl.style.display = 'none';
windowEl.classList.remove('active');
taskbarButton.remove();
openApps.delete(appName);
if (dosInstances[appName]) {
console.log(`Cleaning up ${appName} instance (iframe approach)`);
const container = document.getElementById(`${appName}-content`);
if (container) container.innerHTML = '';
delete dosInstances[appName];
}
if (appName === 'paint') {
if (paintCritiqueIntervalId) {
clearInterval(paintCritiqueIntervalId);
paintCritiqueIntervalId = null;
if (paintAssistant) paintAssistant.classList.remove('visible');
}
const paintContent = appData.windowEl.querySelector('.window-content') as HTMLDivElement | null;
if (paintContent && paintResizeObserverMap.has(paintContent)) {
paintResizeObserverMap.get(paintContent)?.disconnect();
paintResizeObserverMap.delete(paintContent);
}
}
if (appName === 'minesweeper') {
if (minesweeperTimerInterval) {
clearInterval(minesweeperTimerInterval);
minesweeperTimerInterval = null;
}
}
if (appName === 'mediaPlayer') {
const player = youtubePlayers[appName];
if (player) {
try {
if (typeof player.stopVideo === 'function') player.stopVideo();
if (typeof player.destroy === 'function') player.destroy();
} catch (e) {
console.warn("Error stopping/destroying media player:", e);
}
delete youtubePlayers[appName];
console.log("Destroyed YouTube player for mediaPlayer.");
}
// Reset the player area with a message
const playerDivId = `youtube-player-${appName}`;
const playerDiv = document.getElementById(playerDivId) as HTMLDivElement | null;
if (playerDiv) {
playerDiv.innerHTML = `<p class="media-player-status-message">Player closed. Enter a YouTube URL to load.</p>`;
}
// Reset control buttons state (optional, but good practice)
const mediaPlayerWindow = document.getElementById('mediaPlayer');
if (mediaPlayerWindow) {
const playBtn = mediaPlayerWindow.querySelector('#media-player-play') as HTMLButtonElement;
const pauseBtn = mediaPlayerWindow.querySelector('#media-player-pause') as HTMLButtonElement;
const stopBtn = mediaPlayerWindow.querySelector('#media-player-stop') as HTMLButtonElement;
if (playBtn) playBtn.disabled = true;
if (pauseBtn) pauseBtn.disabled = true;
if (stopBtn) stopBtn.disabled = true;
}
}
if (activeWindow === windowEl) {
activeWindow = null;
let nextAppToActivate: HTMLDivElement | null = null;
let maxZ = -1;
openApps.forEach((data) => {
const z = parseInt(data.windowEl.style.zIndex || '0', 10);
if (z > maxZ) {
maxZ = z;
nextAppToActivate = data.windowEl;
}
});
if (nextAppToActivate) {
bringToFront(nextAppToActivate);
}
}
}
/** Minimizes an application window */
function minimizeApp(appName: string): void {
const appData = openApps.get(appName);
if (!appData) return;
const { windowEl, taskbarButton } = appData;
windowEl.style.display = 'none';
windowEl.classList.remove('active');
taskbarButton.classList.remove('active');
if (activeWindow === windowEl) {
activeWindow = null;
let nextAppToActivate: string | null = null;
let maxZ = 0;
openApps.forEach((data, name) => {
if (data.windowEl.style.display !== 'none') {
const z = parseInt(data.windowEl.style.zIndex || '0', 10);
if (z > maxZ) {
maxZ = z;
nextAppToActivate = name;
}
}
});
if (nextAppToActivate) {
bringToFront(openApps.get(nextAppToActivate)!.windowEl);
}
}
}
// --- Gemini Chat Specific Functions ---
async function initGeminiChat(windowElement: HTMLDivElement): Promise<void> {
const historyDiv = windowElement.querySelector('.gemini-chat-history') as HTMLDivElement;
const inputEl = windowElement.querySelector('.gemini-chat-input') as HTMLInputElement;
const sendButton = windowElement.querySelector('.gemini-chat-send') as HTMLButtonElement;
if (!historyDiv || !inputEl || !sendButton) {
console.error("Gemini chat elements not found in window:", windowElement.id);
return;
}
function addChatMessage(container: HTMLDivElement, text: string, className: string = '') {
const p = document.createElement('p');
if (className) p.classList.add(className);
p.textContent = text;
container.appendChild(p);
container.scrollTop = container.scrollHeight;
}
addChatMessage(historyDiv, "Initializing AI...", "system-message");
const sendMessage = async () => {
if (!geminiInstance) {
const initSuccess = await initializeGeminiIfNeeded('initGeminiChat');
if (!initSuccess) {
addChatMessage(historyDiv, "Error: Failed to initialize AI.", "error-message");
return;
}
const initMsg = Array.from(historyDiv.children).find(el => el.textContent?.includes("Initializing AI..."));
if (initMsg) initMsg.remove();
addChatMessage(historyDiv, "AI Ready.", "system-message");
}
const message = inputEl.value.trim();
if (!message) return;
addChatMessage(historyDiv, `You: ${message}`, "user-message");
inputEl.value = '';
inputEl.disabled = true;
sendButton.disabled = true;
try {
// @ts-ignore
const chat = geminiInstance.chats.create({ model: 'gemini-2.5-flash', history: [] });
// @ts-ignore
const result = await chat.sendMessageStream({message: message});
let fullResponse = "";
addChatMessage(historyDiv, "Gemini: ", "gemini-message");
const lastMessageElement = historyDiv.lastElementChild as HTMLParagraphElement | null;
for await (const chunk of result) {
const chunkText = chunk.text || "";
fullResponse += chunkText;
if (lastMessageElement) {
lastMessageElement.textContent += chunkText;
historyDiv.scrollTop = historyDiv.scrollHeight;
}
}
} catch (error: any) {
addChatMessage(historyDiv, `Error: ${error.message || 'Failed to get response.'}`, "error-message");
} finally {
inputEl.disabled = false; sendButton.disabled = false; inputEl.focus();
}
};
sendButton.onclick = sendMessage;
inputEl.onkeydown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } };
inputEl.disabled = false; sendButton.disabled = false; inputEl.focus();
}
/** Handles Notepad story generation */
async function initNotepadStory(windowElement: HTMLDivElement): Promise<void> {
const textarea = windowElement.querySelector('.notepad-textarea') as HTMLTextAreaElement;
const storyButton = windowElement.querySelector('.notepad-story-button') as HTMLButtonElement;
if (!textarea || !storyButton) return;
storyButton.addEventListener('click', async () => {
const currentText = textarea.value;
textarea.value = currentText + "\n\nGenerating story... Please wait...\n\n";
textarea.scrollTop = textarea.scrollHeight;
storyButton.disabled = true; storyButton.textContent = "Working...";
try {
if (!geminiInstance) {
if (!await initializeGeminiIfNeeded('initNotepadStory')) throw new Error("Failed to initialize Gemini API.");
}
const prompt = "Write me a short creative story (250-300 words) with an unexpected twist ending. Make it engaging and suitable for all ages.";
// @ts-ignore
const result = await geminiInstance.models.generateContentStream({ model: 'gemini-2.5-flash', contents: prompt });
textarea.value = currentText + "\n\n";
for await (const chunk of result) {
textarea.value += chunk.text || "";
textarea.scrollTop = textarea.scrollHeight;
}
textarea.value += "\n\n";
} catch (error: any) {
textarea.value = currentText + "\n\nError: " + (error.message || "Failed to generate story.") + "\n\n";
} finally {
storyButton.disabled = false; storyButton.textContent = "Generate Story";
textarea.scrollTop = textarea.scrollHeight;
}
});
}
/** Initializes the AI Browser functionality with image generation */
function initAiBrowser(windowElement: HTMLDivElement): void {
const addressBar = windowElement.querySelector('.browser-address-bar') as HTMLInputElement;
const goButton = windowElement.querySelector('.browser-go-button') as HTMLButtonElement;
const iframe = windowElement.querySelector('#browser-frame') as HTMLIFrameElement;
const loadingEl = windowElement.querySelector('.browser-loading') as HTMLDivElement;
const DIAL_UP_SOUND_URL = 'https://www.soundjay.com/communication/dial-up-modem-01.mp3';
let dialUpAudio: HTMLAudioElement | null = null;
if (!addressBar || !goButton || !iframe || !loadingEl) return;
async function navigateToUrl(url: string): Promise<void> {
if (!url.startsWith('http://') && !url.startsWith('https://')) url = 'https://' + url;
try {
const urlObj = new URL(url);
const domain = urlObj.hostname;
// --- RESTORED DIALUP ANIMATION ---
loadingEl.innerHTML = `
<style>
.dialup-animation .dot {
animation: dialup-blink 1.4s infinite both;
}
.dialup-animation .dot:nth-child(2) {
animation-delay: 0.2s;
}
.dialup-animation .dot:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes dialup-blink {
0%, 80%, 100% { opacity: 0; }
40% { opacity: 1; }
}
.browser-loading p { margin: 5px 0; }
.browser-loading .small-text { font-size: 0.8em; color: #aaa; }
</style>
<img src="https://d112y698adiu2z.cloudfront.net/photos/production/software_photos/000/948/341/datas/original.gif"/>
<p>Connecting to ${domain}<span class="dialup-animation"><span class="dot">.</span><span class="dot">.</span><span class="dot">.</span></span></p>
<!-- Sound will play via JS -->
`;
loadingEl.style.display = 'flex';
// --- END RESTORED DIALUP ANIMATION ---
try {
if (!dialUpAudio) {
dialUpAudio = new Audio(DIAL_UP_SOUND_URL); dialUpAudio.loop = true;
}
await dialUpAudio.play();
} catch (audioError) { console.error("Dial-up sound error:", audioError); }
try {
if (!geminiInstance) {
if (!await initializeGeminiIfNeeded('initAiBrowser')) {
iframe.src = 'data:text/plain;charset=utf-8,AI Init Error';
loadingEl.style.display = 'none'; return;
}
}
const websitePrompt = `
Create a complete 90s-style website for the domain "${domain}".
MUST include: 1 relevant image, garish 90s styling (neon, comic sans, tables), content specific to "${domain}", scrolling marquee, retro emoji/ascii, blinking text, visitor counter (9000+), "Under Construction" signs. Fun, humorous, 1996 feel. Image MUST match theme. No modern design.
`;
// @ts-ignore
const result = await geminiInstance.models.generateContent({
model: 'gemini-2.0-flash-preview-image-generation',
contents: [{role: 'user', parts: [{text: websitePrompt}]}],
config: { temperature: 0.9, responseModalities: ['TEXT', 'IMAGE'] }
});
let htmlContent = ""; const images: string[] = [];
if (result.candidates?.[0]?.content) {
for (const part of result.candidates[0].content.parts) {
if (part.text) htmlContent += part.text.replace(/```html|```/g, '').trim();
else if (part.inlineData?.data) images.push(`data:${part.inlineData.mimeType || 'image/png'};base64,${part.inlineData.data}`);
}
}
if (!htmlContent.includes("<html")) {
htmlContent = `<!DOCTYPE html><html><head><title>${domain}</title><style>body{font-family:"Comic Sans MS";background:lime;color:blue;}marquee{background:yellow;color:red;}img{max-width:80%; display:block; margin:10px auto; border: 3px ridge gray;}</style></head><body><marquee>Welcome to ${domain}!</marquee><h1>${domain}</h1><div>${htmlContent}</div></body></html>`;
}
if (images.length > 0) {
if (!htmlContent.includes('<img src="data:')) {
htmlContent = htmlContent.replace(/(<\/h1>)/i, `$1\n<img src="${images[0]}" alt="Site Image">`);
}
}
iframe.src = 'data:text/html;charset=utf-8,' + encodeURIComponent(htmlContent);
addressBar.value = url;
} catch (e: any) {
iframe.src = 'data:text/html;charset=utf-8,' + encodeURIComponent(`<html><body>Error generating site: ${e.message}</body></html>`);
} finally {
loadingEl.style.display = 'none';
if (dialUpAudio) { dialUpAudio.pause(); dialUpAudio.currentTime = 0; }
}
} catch (e) { alert("Invalid URL"); loadingEl.style.display = 'none'; }
}
goButton.addEventListener('click', () => navigateToUrl(addressBar.value));
addressBar.addEventListener('keydown', (e) => { if (e.key === 'Enter') navigateToUrl(addressBar.value); });
addressBar.addEventListener('click', () => addressBar.select());
}
// --- Event Listeners Setup ---
icons.forEach(icon => {
icon.addEventListener('click', () => {
const appName = icon.getAttribute('data-app');
if (appName) {
openApp(appName);
startMenu.classList.remove('active');
}
});
});
document.querySelectorAll('.start-menu-item').forEach(item => {
item.addEventListener('click', () => {
const appName = (item as HTMLElement).getAttribute('data-app');
if (appName) openApp(appName);
startMenu.classList.remove('active');
});
});
startButton.addEventListener('click', (e) => {
e.stopPropagation();
startMenu.classList.toggle('active');
if (startMenu.classList.contains('active')) {
highestZIndex++;
startMenu.style.zIndex = highestZIndex.toString();
}
});
windows.forEach(windowElement => {
const titleBar = windowElement.querySelector('.window-titlebar') as HTMLDivElement | null;
const closeButton = windowElement.querySelector('.window-close') as HTMLDivElement | null;
const minimizeButton = windowElement.querySelector('.window-minimize') as HTMLDivElement | null;
windowElement.addEventListener('mousedown', () => bringToFront(windowElement), true);
if (closeButton) {
closeButton.addEventListener('click', (e) => { e.stopPropagation(); closeApp(windowElement.id); });
}
if (minimizeButton) {
minimizeButton.addEventListener('click', (e) => { e.stopPropagation(); minimizeApp(windowElement.id); });
}
if (titleBar) {
let isDragging = false;
let dragOffsetX: number, dragOffsetY: number;
const startDragging = (e: MouseEvent) => {
if (!(e.target === titleBar || titleBar.contains(e.target as Node)) || (e.target as Element).closest('.window-control-button')) {
isDragging = false; return;
}
isDragging = true; bringToFront(windowElement);
const rect = windowElement.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left; dragOffsetY = e.clientY - rect.top;
titleBar.style.cursor = 'grabbing';
document.addEventListener('mousemove', dragWindow);
document.addEventListener('mouseup', stopDragging, { once: true });
};
const dragWindow = (e: MouseEvent) => {
if (!isDragging) return;
let x = e.clientX - dragOffsetX; let y = e.clientY - dragOffsetY;
const taskbarHeight = taskbarAppsContainer.parentElement?.offsetHeight ?? 36;
const maxX = window.innerWidth - windowElement.offsetWidth;
const maxY = window.innerHeight - windowElement.offsetHeight - taskbarHeight;
const minX = -(windowElement.offsetWidth - 40);
const maxXAdjusted = window.innerWidth - 40;
x = Math.max(minX, Math.min(x, maxXAdjusted));
y = Math.max(0, Math.min(y, maxY));
windowElement.style.left = `${x}px`; windowElement.style.top = `${y}px`;
};
const stopDragging = () => {
if (!isDragging) return;
isDragging = false; titleBar.style.cursor = 'grab';
document.removeEventListener('mousemove', dragWindow);
};
titleBar.addEventListener('mousedown', startDragging);
}
if (!openApps.has(windowElement.id)) { // Only apply random for newly opened, not for bringToFront
const randomTop = Math.random() * (window.innerHeight / 4) + 20;
const randomLeft = Math.random() * (window.innerWidth / 3) + 20;
windowElement.style.top = `${randomTop}px`;
windowElement.style.left = `${randomLeft}px`;
}
});
document.addEventListener('click', (e) => {
if (startMenu.classList.contains('active') && !startMenu.contains(e.target as Node) && !startButton.contains(e.target as Node)) {
startMenu.classList.remove('active');
}
});
function findIconElement(appName: string): HTMLDivElement | undefined {
return Array.from(icons).find(icon => icon.dataset.app === appName);
}
console.log("Gemini 95 Simulator Initialized (TS)");
async function critiquePaintDrawing(): Promise<void> {
const paintWindow = document.getElementById('paint') as HTMLDivElement | null;
if (!paintWindow || paintWindow.style.display === 'none') return;
const canvas = paintWindow.querySelector('#paint-canvas') as HTMLCanvasElement | null;
if (!canvas) { if (assistantBubble) assistantBubble.textContent = 'Error: Canvas not found!'; return; }
if (!geminiInstance) {
if (!await initializeGeminiIfNeeded('critiquePaintDrawing')) {
if (assistantBubble) assistantBubble.textContent = 'Error: AI init failed!'; return;
}
}
try {
if (assistantBubble) assistantBubble.textContent = 'Analyzing...';
const imageDataUrl = canvas.toDataURL('image/jpeg', 0.8);
const base64Data = imageDataUrl.split(',')[1];
if (!base64Data) throw new Error("Failed to get base64 data.");
const prompt = "Critique this drawing with witty sarcasm (1-2 sentences).";
const imagePart = { inlineData: { data: base64Data, mimeType: "image/jpeg" } };
// @ts-ignore
const result = await geminiInstance.models.generateContent({ model: "gemini-2.5-pro-exp-03-25", contents: [{ role: "user", parts: [ { text: prompt }, imagePart] }] });
const critique = result?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || "Is this art?";
if (assistantBubble) assistantBubble.textContent = critique;
} catch (error: any) {
if (assistantBubble) assistantBubble.textContent = `Critique Error: ${error.message}`;
}
}
function initSimplePaintApp(windowElement: HTMLDivElement): void {
const canvas = windowElement.querySelector('#paint-canvas') as HTMLCanvasElement;
const toolbar = windowElement.querySelector('.paint-toolbar') as HTMLDivElement;
const contentArea = windowElement.querySelector('.window-content') as HTMLDivElement; // This is the direct parent managing canvas size
const colorSwatches = windowElement.querySelectorAll('.paint-color-swatch') as NodeListOf<HTMLButtonElement>;
const sizeButtons = windowElement.querySelectorAll('.paint-size-button') as NodeListOf<HTMLButtonElement>;
const clearButton = windowElement.querySelector('.paint-clear-button') as HTMLButtonElement;
if (!canvas || !toolbar || !contentArea || !clearButton) { return; }
const ctx = canvas.getContext('2d');
if (!ctx) { return; }
let isDrawing = false; let lastX = 0; let lastY = 0;
ctx.strokeStyle = 'black'; ctx.lineWidth = 2; ctx.lineJoin = 'round'; ctx.lineCap = 'round';
let currentStrokeStyle = ctx.strokeStyle; let currentLineWidth = ctx.lineWidth;
function resizeCanvas() {
const rect = contentArea.getBoundingClientRect();
const toolbarHeight = toolbar.offsetHeight;
const newWidth = Math.floor(rect.width); // Canvas width is content area width
const newHeight = Math.floor(rect.height - toolbarHeight); // Canvas height is content area height minus toolbar
if (canvas.width === newWidth && canvas.height === newHeight && newWidth > 0 && newHeight > 0) return;
canvas.width = newWidth > 0 ? newWidth : 1;
canvas.height = newHeight > 0 ? newHeight : 1;
ctx.fillStyle = 'white'; ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = currentStrokeStyle; ctx.lineWidth = currentLineWidth;
ctx.lineJoin = 'round'; ctx.lineCap = 'round';
}
const resizeObserver = new ResizeObserver(resizeCanvas);
resizeObserver.observe(contentArea);
paintResizeObserverMap.set(contentArea, resizeObserver);
resizeCanvas();
function getMousePos(canvasDom: HTMLCanvasElement, event: MouseEvent | TouchEvent): { x: number, y: number } {
const rect = canvasDom.getBoundingClientRect();
let clientX, clientY;
if (event instanceof MouseEvent) { clientX = event.clientX; clientY = event.clientY; }
else { clientX = event.touches[0].clientX; clientY = event.touches[0].clientY; }
return { x: clientX - rect.left, y: clientY - rect.top };
}
function startDrawing(e: MouseEvent | TouchEvent) {
isDrawing = true; const pos = getMousePos(canvas, e);
[lastX, lastY] = [pos.x, pos.y]; ctx.beginPath(); ctx.moveTo(lastX, lastY);
}
function draw(e: MouseEvent | TouchEvent) {
if (!isDrawing) return; e.preventDefault();
const pos = getMousePos(canvas, e);
ctx.lineTo(pos.x, pos.y); ctx.stroke();
[lastX, lastY] = [pos.x, pos.y];
}
function stopDrawing() { if (isDrawing) isDrawing = false; }
canvas.addEventListener('mousedown', startDrawing); canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing); canvas.addEventListener('mouseleave', stopDrawing);
canvas.addEventListener('touchstart', startDrawing, { passive: false });
canvas.addEventListener('touchmove', draw, { passive: false });
canvas.addEventListener('touchend', stopDrawing); canvas.addEventListener('touchcancel', stopDrawing);
colorSwatches.forEach(swatch => {
swatch.addEventListener('click', () => {
ctx.strokeStyle = swatch.dataset.color || 'black'; currentStrokeStyle = ctx.strokeStyle;
colorSwatches.forEach(s => s.classList.remove('active')); swatch.classList.add('active');
if (swatch.dataset.color === 'white') {
const largeSizeButton = Array.from(sizeButtons).find(b => b.dataset.size === '10');
if (largeSizeButton) {
ctx.lineWidth = parseInt(largeSizeButton.dataset.size || '10', 10); currentLineWidth = ctx.lineWidth;
sizeButtons.forEach(s => s.classList.remove('active')); largeSizeButton.classList.add('active');
}
} else {
const activeSizeButton = Array.from(sizeButtons).find(b => b.classList.contains('active'));
if (activeSizeButton) { ctx.lineWidth = parseInt(activeSizeButton.dataset.size || '2', 10); currentLineWidth = ctx.lineWidth; }
}
});
});
sizeButtons.forEach(button => {
button.addEventListener('click', () => {
ctx.lineWidth = parseInt(button.dataset.size || '2', 10); currentLineWidth = ctx.lineWidth;
sizeButtons.forEach(s => s.classList.remove('active')); button.classList.add('active');
const eraser = Array.from(colorSwatches).find(s => s.dataset.color === 'white');
if (!eraser?.classList.contains('active')) {
if (!Array.from(colorSwatches).some(s => s.classList.contains('active'))) {
const blackSwatch = Array.from(colorSwatches).find(s => s.dataset.color === 'black');
blackSwatch?.classList.add('active'); ctx.strokeStyle = 'black'; currentStrokeStyle = ctx.strokeStyle;
}
}
});
});
clearButton.addEventListener('click', () => {
ctx.fillStyle = 'white'; ctx.fillRect(0, 0, canvas.width, canvas.height);
});
(windowElement.querySelector('.paint-color-swatch[data-color="black"]') as HTMLButtonElement)?.classList.add('active');
(windowElement.querySelector('.paint-size-button[data-size="2"]') as HTMLButtonElement)?.classList.add('active');
}
type MinesweeperCell = { isMine: boolean; isRevealed: boolean; isFlagged: boolean; adjacentMines: number; element: HTMLDivElement; row: number; col: number; };
function initMinesweeperGame(windowElement: HTMLDivElement): void {
const boardElement = windowElement.querySelector('#minesweeper-board') as HTMLDivElement;
const flagCountElement = windowElement.querySelector('.minesweeper-flag-count') as HTMLDivElement;
const timerElement = windowElement.querySelector('.minesweeper-timer') as HTMLDivElement;
const resetButton = windowElement.querySelector('.minesweeper-reset-button') as HTMLButtonElement;
const hintButton = windowElement.querySelector('.minesweeper-hint-button') as HTMLButtonElement;
const commentaryElement = windowElement.querySelector('.minesweeper-commentary') as HTMLDivElement;
if (!boardElement || !flagCountElement || !timerElement || !resetButton || !hintButton || !commentaryElement) return;
let grid: MinesweeperCell[][] = [];
function resetGame() {
if (minesweeperTimerInterval) clearInterval(minesweeperTimerInterval);
minesweeperTimerInterval = null; minesweeperTimeElapsed = 0; minesweeperFlagsPlaced = 0;
minesweeperGameOver = false; minesweeperFirstClick = true; minesweeperMineCount = 10;
minesweeperGridSize = { rows: 9, cols: 9 };
timerElement.textContent = `β±οΈ 0`; flagCountElement.textContent = `π© ${minesweeperMineCount}`;
resetButton.textContent = 'π'; commentaryElement.textContent = "Let's play! Click a square.";
createGrid();
}
function createGrid() {
boardElement.innerHTML = ''; grid = [];
boardElement.style.gridTemplateColumns = `repeat(${minesweeperGridSize.cols}, 20px)`;
boardElement.style.gridTemplateRows = `repeat(${minesweeperGridSize.rows}, 20px)`;
for (let r = 0; r < minesweeperGridSize.rows; r++) {
const row: MinesweeperCell[] = [];
for (let c = 0; c < minesweeperGridSize.cols; c++) {
const cellElement = document.createElement('div'); cellElement.classList.add('minesweeper-cell');
const cellData: MinesweeperCell = { isMine: false, isRevealed: false, isFlagged: false, adjacentMines: 0, element: cellElement, row: r, col: c };
cellElement.addEventListener('click', () => handleCellClick(cellData));
cellElement.addEventListener('contextmenu', (e) => { e.preventDefault(); handleCellRightClick(cellData); });
row.push(cellData); boardElement.appendChild(cellElement);
}
grid.push(row);
}
}
function placeMines(firstClickRow: number, firstClickCol: number) {
let minesPlaced = 0;
while (minesPlaced < minesweeperMineCount) {
const r = Math.floor(Math.random() * minesweeperGridSize.rows);
const c = Math.floor(Math.random() * minesweeperGridSize.cols);
if ((r === firstClickRow && c === firstClickCol) || grid[r][c].isMine) continue;
grid[r][c].isMine = true; minesPlaced++;
}
for (let r = 0; r < minesweeperGridSize.rows; r++) {
for (let c = 0; c < minesweeperGridSize.cols; c++) {
if (!grid[r][c].isMine) grid[r][c].adjacentMines = countAdjacentMines(r, c);
}
}
}
function countAdjacentMines(row: number, col: number): number {
let count = 0;
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
if (dr === 0 && dc === 0) continue;
const nr = row + dr; const nc = col + dc;
if (nr >= 0 && nr < minesweeperGridSize.rows && nc >= 0 && nc < minesweeperGridSize.cols && grid[nr][nc].isMine) count++;
}
}
return count;
}
function handleCellClick(cell: MinesweeperCell) {
if (minesweeperGameOver || cell.isRevealed || cell.isFlagged) return;
if (minesweeperFirstClick && !minesweeperTimerInterval) {
placeMines(cell.row, cell.col); minesweeperFirstClick = false; startTimer();
}
if (cell.isMine) gameOver(cell);
else { revealCell(cell); checkWinCondition(); }
}
function handleCellRightClick(cell: MinesweeperCell) {
if (minesweeperGameOver || cell.isRevealed || (minesweeperFirstClick && !minesweeperTimerInterval)) return;
cell.isFlagged = !cell.isFlagged; cell.element.textContent = cell.isFlagged ? 'π©' : '';
minesweeperFlagsPlaced += cell.isFlagged ? 1 : -1;
updateFlagCount(); checkWinCondition();
}
function revealCell(cell: MinesweeperCell) {
if (cell.isRevealed || cell.isFlagged || cell.isMine) return;
cell.isRevealed = true; cell.element.classList.add('revealed'); cell.element.textContent = '';
if (cell.adjacentMines > 0) {
cell.element.textContent = cell.adjacentMines.toString();
cell.element.dataset.number = cell.adjacentMines.toString();
} else {
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
if (dr === 0 && dc === 0) continue;
const nr = cell.row + dr; const nc = cell.col + dc;
if (nr >= 0 && nr < minesweeperGridSize.rows && nc >= 0 && nc < minesweeperGridSize.cols) {
const neighbor = grid[nr][nc];
if (!neighbor.isRevealed && !neighbor.isFlagged) revealCell(neighbor);
}
}
}
}
}
function startTimer() {
if (minesweeperTimerInterval) return;
minesweeperTimeElapsed = 0; timerElement.textContent = `β±οΈ 0`;
minesweeperTimerInterval = window.setInterval(() => {
minesweeperTimeElapsed++; timerElement.textContent = `β±οΈ ${minesweeperTimeElapsed}`;
}, 1000);
}
function updateFlagCount() {
flagCountElement.textContent = `π© ${minesweeperMineCount - minesweeperFlagsPlaced}`;
}
function gameOver(clickedMine: MinesweeperCell) {
minesweeperGameOver = true;
if (minesweeperTimerInterval) clearInterval(minesweeperTimerInterval);
minesweeperTimerInterval = null; resetButton.textContent = 'π΅';
grid.forEach(row => row.forEach(cell => {
if (cell.isMine) {
cell.element.classList.add('mine', 'revealed'); cell.element.textContent = 'π£';
}
if (!cell.isMine && cell.isFlagged) cell.element.textContent = 'β';
}));
clickedMine.element.classList.add('exploded'); clickedMine.element.textContent = 'π₯';
}
function checkWinCondition() {
if (minesweeperGameOver) return;
let revealedCount = 0; let correctlyFlaggedMines = 0;
grid.forEach(row => row.forEach(cell => {
if (cell.isRevealed && !cell.isMine) revealedCount++;
if (cell.isFlagged && cell.isMine) correctlyFlaggedMines++;
}));
const totalNonMineCells = (minesweeperGridSize.rows * minesweeperGridSize.cols) - minesweeperMineCount;
if (revealedCount === totalNonMineCells || (correctlyFlaggedMines === minesweeperMineCount && minesweeperFlagsPlaced === minesweeperMineCount)) {
minesweeperGameOver = true;
if (minesweeperTimerInterval) clearInterval(minesweeperTimerInterval);
minesweeperTimerInterval = null; resetButton.textContent = 'π';
if (revealedCount === totalNonMineCells) {
grid.forEach(row => row.forEach(cell => {
if (cell.isMine && !cell.isFlagged) { cell.isFlagged = true; cell.element.textContent = 'π©'; minesweeperFlagsPlaced++; }
})); updateFlagCount();
}
}
}
function getBoardStateAsText(): string {
let boardString = `Flags: ${minesweeperMineCount - minesweeperFlagsPlaced}, Time: ${minesweeperTimeElapsed}s\nGrid (H=Hidden,F=Flag,Num=Mines):\n`;
grid.forEach(row => {
row.forEach(cell => {
if (cell.isFlagged) boardString += " F ";
else if (!cell.isRevealed) boardString += " H ";
else if (cell.adjacentMines > 0) boardString += ` ${cell.adjacentMines} `;
else boardString += " _ ";
});
boardString += "\n";
});
return boardString;
}
async function getAiHint() {
if (minesweeperGameOver || minesweeperFirstClick) { commentaryElement.textContent = "Click a square first!"; return; }
hintButton.disabled = true; hintButton.textContent = 'π€'; commentaryElement.textContent = 'Thinking...';
if (!geminiInstance) {
if (!await initializeGeminiIfNeeded('getAiHint')) {
commentaryElement.textContent = 'AI Init Error.'; hintButton.disabled = false; hintButton.textContent = 'π‘ Hint'; return;
}
}
try {
const boardState = getBoardStateAsText();
const prompt = `Minesweeper state:\n${boardState}\nShort, witty hint (1-2 sentences) for a safe move or dangerous area. Don't reveal exact mines unless certain. Hint:`;
// @ts-ignore
const result = await geminiInstance.models.generateContent({ model: "gemini-2.5-flash", contents: [{role:"user", parts:[{text:prompt}]}], config: {temperature: 0.7}});
const hintText = result?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || "Try clicking somewhere?";
commentaryElement.textContent = hintText;
} catch (error: any) { commentaryElement.textContent = `Hint Error: ${error.message}`;
} finally { hintButton.disabled = false; hintButton.textContent = 'π‘ Hint'; }
}
resetButton.addEventListener('click', resetGame);
hintButton.addEventListener('click', getAiHint);
resetGame();
}
function initMyComputer(windowElement: HTMLDivElement): void {
const cDriveIcon = windowElement.querySelector('#c-drive-icon') as HTMLDivElement;
const cDriveContent = windowElement.querySelector('#c-drive-content') as HTMLDivElement;
const secretImageIcon = windowElement.querySelector('#secret-image-icon') as HTMLDivElement;
if (!cDriveIcon || !cDriveContent || !secretImageIcon) return;
cDriveIcon.addEventListener('click', () => {
cDriveIcon.style.display = 'none'; cDriveContent.style.display = 'block';
});
secretImageIcon.addEventListener('click', () => {
const imageViewerWindow = document.getElementById('imageViewer') as HTMLDivElement | null;
const imageViewerImg = document.getElementById('image-viewer-img') as HTMLImageElement | null;
const imageViewerTitle = document.getElementById('image-viewer-title') as HTMLSpanElement | null;
if (!imageViewerWindow || !imageViewerImg || !imageViewerTitle) { alert("Image Viewer corrupted!"); return; }
imageViewerImg.src = 'https://storage.googleapis.com/gemini-95-icons/%40ammaar%2B%40olacombe.png';
imageViewerImg.alt = 'dontshowthistoanyone.jpg';
imageViewerTitle.textContent = 'dontshowthistoanyone.jpg - Image Viewer';
openApp('imageViewer');
});
cDriveIcon.style.display = 'inline-flex'; cDriveContent.style.display = 'none';
}
// --- YouTube Player (GemPlayer) Logic ---
function loadYouTubeApi(): Promise<void> {
if (ytApiLoaded) return Promise.resolve();
if (ytApiLoadingPromise) return ytApiLoadingPromise;
ytApiLoadingPromise = new Promise((resolve, reject) => {
// @ts-ignore
if (window.YT && window.YT.Player) {
ytApiLoaded = true; resolve(); return;
}
const tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
tag.onerror = (err) => {
console.error("Failed to load YouTube API script:", err);
ytApiLoadingPromise = null;
reject(new Error("YouTube API script load failed"));
};
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode!.insertBefore(tag, firstScriptTag);
// @ts-ignore
window.onYouTubeIframeAPIReady = () => {
ytApiLoaded = true; ytApiLoadingPromise = null; resolve();
};
setTimeout(() => {
if (!ytApiLoaded) {
// @ts-ignore
if (window.onYouTubeIframeAPIReady) window.onYouTubeIframeAPIReady = null;
ytApiLoadingPromise = null;
reject(new Error("YouTube API load timeout"));
}
}, 10000);
});
return ytApiLoadingPromise;
}
function getYouTubeVideoId(urlOrId: string): string | null {
if (!urlOrId) return null;
if (/^[a-zA-Z0-9_-]{11}$/.test(urlOrId)) return urlOrId;
const regExp = /^.*(?:youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]{11}).*/;
const match = urlOrId.match(regExp);
return (match && match[1]) ? match[1] : null;
}
async function initMediaPlayer(windowElement: HTMLDivElement): Promise<void> {
const appName = windowElement.id; // 'mediaPlayer'
const urlInput = windowElement.querySelector('.media-player-input') as HTMLInputElement;
const loadButton = windowElement.querySelector('.media-player-load-button') as HTMLButtonElement;
const playerContainerDivId = `youtube-player-${appName}`;
const playerDiv = windowElement.querySelector(`#${playerContainerDivId}`) as HTMLDivElement;
const playButton = windowElement.querySelector('#media-player-play') as HTMLButtonElement;
const pauseButton = windowElement.querySelector('#media-player-pause') as HTMLButtonElement;
const stopButton = windowElement.querySelector('#media-player-stop') as HTMLButtonElement;
if (!urlInput || !loadButton || !playerDiv || !playButton || !pauseButton || !stopButton) {
console.error("Media Player elements not found for", appName);
if (playerDiv) playerDiv.innerHTML = `<p class="media-player-status-message" style="color:red;">Error: Player UI missing.</p>`;
return;
}
const updateButtonStates = (playerState?: number) => {
// @ts-ignore
const YTPlayerState = window.YT?.PlayerState;
if (!YTPlayerState) {
playButton.disabled = true; pauseButton.disabled = true; stopButton.disabled = true;
return;
}
const state = playerState !== undefined ? playerState
// @ts-ignore
: (youtubePlayers[appName] && typeof youtubePlayers[appName].getPlayerState === 'function' ? youtubePlayers[appName].getPlayerState() : YTPlayerState.UNSTARTED);
playButton.disabled = state === YTPlayerState.PLAYING || state === YTPlayerState.BUFFERING;
pauseButton.disabled = state !== YTPlayerState.PLAYING && state !== YTPlayerState.BUFFERING; // Can pause if buffering too
stopButton.disabled = state === YTPlayerState.ENDED || state === YTPlayerState.UNSTARTED || state === -1 /* UNSTARTED also seen as -1 */;
};
updateButtonStates(-1); // Initial state (unstarted)
const showPlayerMessage = (message: string, isError: boolean = false) => {
const player = youtubePlayers[appName];
if (player) {
try { if (typeof player.destroy === 'function') player.destroy(); }
catch(e) { console.warn("Minor error destroying player:", e); }
delete youtubePlayers[appName];
}
playerDiv.innerHTML = `<p class="media-player-status-message" style="color:${isError ? 'red' : '#ccc'};">${message}</p>`;
updateButtonStates(-1);
};
const initialStatusMessageEl = playerDiv.querySelector('.media-player-status-message');
if (initialStatusMessageEl) initialStatusMessageEl.textContent = 'Connecting to YouTube...';
try {
await loadYouTubeApi();
if (initialStatusMessageEl) initialStatusMessageEl.textContent = 'YouTube API Ready. Loading default video...';
} catch (error: any) {
showPlayerMessage(`Error: Could not load YouTube Player API. ${error.message}`, true);
return;
}
const createPlayer = (videoId: string) => {
const existingPlayer = youtubePlayers[appName];
if (existingPlayer) {
try { if (typeof existingPlayer.destroy === 'function') existingPlayer.destroy(); }
catch(e) { console.warn("Minor error destroying previous player:", e); }
}
playerDiv.innerHTML = ''; // Clear previous content/message
try {
// @ts-ignore
youtubePlayers[appName] = new YT.Player(playerContainerDivId, {
height: '100%', width: '100%', videoId: videoId,
playerVars: { 'playsinline': 1, 'autoplay': 1, 'controls': 0, 'modestbranding': 1, 'rel': 0, 'fs': 0, 'origin': window.location.origin },
events: {
'onReady': (event: any) => { /* Autoplay handles start */ updateButtonStates(event.target.getPlayerState()); },
'onError': (event: any) => {
const errorMessages: { [key: number]: string } = { 2: "Invalid video ID.", 5: "HTML5 Player error.", 100: "Video not found/private.", 101: "Playback disallowed.", 150: "Playback disallowed."};
showPlayerMessage(errorMessages[event.data] || `Playback Error (Code: ${event.data})`, true);
},
'onStateChange': (event: any) => { updateButtonStates(event.data); }
}
});
} catch (error: any) {
showPlayerMessage(`Failed to create video player: ${error.message}`, true);
}
};
loadButton.addEventListener('click', () => {
const videoUrlOrId = urlInput.value.trim();
const videoId = getYouTubeVideoId(videoUrlOrId);
if (videoId) {
showPlayerMessage("Loading video..."); // Show loading message immediately
createPlayer(videoId);
} else {
showPlayerMessage("Invalid YouTube URL or Video ID.", true);
}
});
playButton.addEventListener('click', () => {
const player = youtubePlayers[appName];
// @ts-ignore
if (player && typeof player.playVideo === 'function') player.playVideo();
});
pauseButton.addEventListener('click', () => {
const player = youtubePlayers[appName];
// @ts-ignore
if (player && typeof player.pauseVideo === 'function') player.pauseVideo();
});
stopButton.addEventListener('click', () => {
const player = youtubePlayers[appName];
// @ts-ignore
if (player && typeof player.stopVideo === 'function') {
player.stopVideo();
// @ts-ignore - Manually set to ended for button state update
updateButtonStates(window.YT?.PlayerState?.ENDED);
}
});
if (DEFAULT_YOUTUBE_VIDEO_ID) {
if (initialStatusMessageEl) initialStatusMessageEl.textContent = `Loading default video...`; // Update message
createPlayer(DEFAULT_YOUTUBE_VIDEO_ID);
} else {
// Message already set by HTML if no default video.
// showPlayerMessage("Enter a YouTube URL or Video ID and click 'Load'.");
}
}
// --- END YouTube Player Logic ---
async function initializeGeminiIfNeeded(context: string): Promise<boolean> {
if (geminiInstance) return true;
try {
const module = await import('@google/genai');
// @ts-ignore
const GoogleAIClass = module.GoogleGenAI;
if (typeof GoogleAIClass !== 'function') throw new Error("GoogleGenAI constructor not found.");
// @ts-ignore
const apiKey = process.env.GEMINI_API_KEY || "";
if (!apiKey) {
alert("CRITICAL ERROR: Gemini API Key missing.");
throw new Error("API Key is missing.");
}
// @ts-ignore
geminiInstance = new GoogleAIClass({apiKey: apiKey});
return true;
} catch (error: any) {
console.error(`Failed Gemini initialization in ${context}:`, error);
alert(`CRITICAL ERROR: Gemini AI failed to initialize. ${error.message}`);
return false;
}
}
|