// content.js // Log that the content script is running console.log("Content script is running."); // --- Feature Detection for Cross-Browser Compatibility --- const runtimeAPI = typeof browser !== 'undefined' ? browser : chrome; const storageAPI = runtimeAPI.storage; // This will be browser.storage or chrome.storage // --- Original Font Loading Logic --- let fontLoaded = false; function loadFont() { return "disabled"; const fontUrl = runtimeAPI.runtime.getURL("fonts/fot_rodin_pro_m.ttf"); const style = document.createElement('style'); style.textContent = ` @font-face { font-family: 'Wii NTLG PGothic JPN Regular'; src: url('${fontUrl}') format('truetype'); } body { font-family: 'Wii NTLG PGothic JPN Regular', sans-serif !important; } * { font-family: 'Wii NTLG PGothic JPN Regular', sans-serif !important; } #text03-01, #text05-01, #text06-01 { display: flex; /* Enable flexbox */ justify-content: center; /* Center horizontally */ align-items: center; /* Center vertically */ height: 100vh; /* Full height of the viewport */ width: 100%; /* Full width */ } .buttonTextBlackM { font-size: 24px; /* Adjust font size as needed */ color: black; /* Set text color */ text-align: center; /* Center text within the div */ } `; const head = document.head || document.getElementsByTagName('head')[0]; if (head) { head.appendChild(style); console.log("Font loaded into ."); return "Font loaded into "; } const observer = new MutationObserver(() => { const head = document.head || document.getElementsByTagName('head')[0]; if (head) { head.appendChild(style); observer.disconnect(); console.log("Font loaded into via MutationObserver."); return "Font loaded into via MutationObserver"; } }); observer.observe(document, { childList: true, subtree: true }); const fallbackTimeout = setTimeout(() => { if (!document.head || !document.head.contains(style)) { document.body.appendChild(style); console.log("Font loaded into as a fallback."); observer.disconnect(); return "Font loaded into as a fallback"; } }, 1000); setTimeout(() => { if (!document.head || !document.body.contains(style)) { console.error("Failed to load font: and are not available."); return "Failed to load font: and are not available."; } observer.disconnect(); clearTimeout(fallbackTimeout); }, 2000); return "Font loading initiated"; } // Check the font state on page load function checkFontState() { storageAPI.local.get(['fontDisabled']).then(function(result) { if (!result.fontDisabled) { //loadFont(); } else { console.log("Font loading disabled by preference."); } }).catch(error => console.error("Error checking font state:", error)); } const soundFilePaths = [ '', // Index 0 (unused) 'audio/1.wav', 'audio/2.wav', 'audio/3.wav', 'audio/4.wav', 'audio/5.wav', 'audio/6.wav', 'audio/7.wav', 'audio/8.wav', 'audio/9.wav', 'audio/10.wav', 'audio/11.wav', 'audio/12.wav', 'audio/13.wav', 'audio/14.wav', 'audio/15.wav', 'audio/16.wav' ]; const bgmFilePath = 'audio/bgm.wav'; // Define BGM file path // Generate the full, web-accessible URLs const generatedSoundUrls = []; for (let i = 0; i < soundFilePaths.length; i++) { if (soundFilePaths[i]) { generatedSoundUrls[i] = runtimeAPI.runtime.getURL(soundFilePaths[i]); } else { generatedSoundUrls[i] = ''; } } const generatedBgmUrl = runtimeAPI.runtime.getURL(bgmFilePath); // Generate BGM URL // Function to load class definitions and inject globals const loadClassDefinitions = () => { const injectScript = () => { if (document.documentElement.querySelector('script[data-injected-id="wii-shop-cdjs"]')) { // Already injected, prevent double injection return; } const script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); script.setAttribute('src', runtimeAPI.runtime.getURL('classDefinitions.js')); // Set src to the external file script.setAttribute('data-injected-id', 'wii-shop-cdjs'); // Add an ID to prevent duplicates // Inject into the document.documentElement ( tag) as the very first child if (document.documentElement) { document.documentElement.insertBefore(script, document.documentElement.firstChild); console.log("classDefinitions.js injected into page context via script element (early)."); // --- Post message to the injected script AFTER it's loaded --- // This ensures classDefinitions.js has a chance to set up its message listener. script.onload = () => { window.postMessage({ type: "WII_SHOP_EXTENSION_INIT", soundUrls: generatedSoundUrls, bgmUrl: generatedBgmUrl }, window.location.origin); // Specify origin for security console.log("WII_SHOP_EXTENSION_INIT message sent to page context."); }; script.onerror = (e) => { console.error("Error loading classDefinitions.js:", e); }; } else { console.error("Failed to inject script: document.documentElement not found."); } }; // Check if document.documentElement is already available if (document.documentElement) { injectScript(); } else { // If not, use a MutationObserver to wait for the element to be available const observer = new MutationObserver((mutations, obs) => { if (document.documentElement) { injectScript(); obs.disconnect(); // Stop observing once injected } }); // Observe the document for changes in its child nodes (to catch being added) observer.observe(document, { childList: true, subtree: true }); } }; // Load the class definitions loadClassDefinitions(); checkFontState(); // --- Initial BGM Playback on DOMContentLoaded --- document.addEventListener('DOMContentLoaded', async () => { // Give a slight delay to ensure classDefinitions.js and window.extensionStorage are fully ready await new Promise(resolve => setTimeout(resolve, 50)); // Request the initial BGM enabled state from the background script via extensionStorage if (typeof window.extensionStorage !== 'undefined' && typeof window.extensionStorage.get === 'function') { try { const bgmEnabled = await window.extensionStorage.get('bgmEnabled'); console.log("Initial BGM enabled state from storage:", bgmEnabled); if (bgmEnabled === true) { console.log("Attempting to play initial BGM from content script."); window.extensionStorage.sendMessageToBackground({ action: "playBGM", bgmUrl: generatedBgmUrl }); } } catch (error) { console.error("Error retrieving initial BGM state or sending play request:", error); } } else { console.warn("window.extensionStorage not available for initial BGM check."); } }); // Listener for messages from the popup (if any, specifically for BGM toggle) runtimeAPI.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === "toggleBGMImmediate") { if (request.enabled) { console.log("Received toggleBGMImmediate from popup: Play BGM."); if (typeof window.extensionStorage !== 'undefined' && typeof window.extensionStorage.sendMessageToBackground === 'function') { window.extensionStorage.sendMessageToBackground({ action: "playBGM", bgmUrl: generatedBgmUrl }); } } else { console.log("Received toggleBGMImmediate from popup: Stop BGM."); if (typeof window.extensionStorage !== 'undefined' && typeof window.extensionStorage.sendMessageToBackground === 'function') { window.extensionStorage.sendMessageToBackground({ action: "stopBGM" }); } } } });