music works across????

This commit is contained in:
thom2305 2025-07-04 13:34:40 -04:00
parent 3801d12e24
commit d33ae05a22
No known key found for this signature in database
GPG Key ID: 0171038FEE1BEF12
6 changed files with 146 additions and 29 deletions

View File

@ -1,4 +1,7 @@
const _injectedWiiShopSoundUrls = window._wiiShopSoundUrls_ || [];
const _injectedWiiShopBgmUrl = window._wiiShopBgmUrl_ || '';
const _bgmInitiallyEnabled = window._wiiShopBgmInitiallyEnabled_ === true;
const _bgmAlreadyPlaying = window._bgmAlreadyPlaying || false;
var ecsUrl = "";
var iasUrl = "";
@ -322,22 +325,20 @@ class wiiShop {
}
class wiiSound {
// === THIS IS THE CRUCIAL CHANGE ===
// Use a static property to store the sound URLs, accessible by all instances
static audioUrls = _injectedWiiShopSoundUrls;
static currentBgmAudio = null;
constructor() {
trace("wiiSound initialized"); // Use the trace function
trace("wiiSound initialized");
}
playSE(snd) {
const soundIndex = parseInt(snd, 10);
// Access the URLs via the static property
if (isNaN(soundIndex) || soundIndex < 1 || soundIndex >= wiiSound.audioUrls.length) {
console.warn("Invalid sound index for wiiSound.playSE:", snd);
return;
}
// Access the URLs via the static property
const audioUrl = wiiSound.audioUrls[soundIndex];
if (!audioUrl) {
console.warn("No audio URL found for sound index:", soundIndex);
@ -345,17 +346,60 @@ class wiiSound {
}
const audio = new Audio(audioUrl);
audio.style.display = 'none'; // Keep it hidden
audio.style.display = 'none';
audio.play()
.then(() => {
console.log('Wii Shop Sound played:', soundIndex, audioUrl);
})
.catch(error => {
console.error('Error playing Wii Shop Sound:', soundIndex, audioUrl, error);
// Common error: Autoplay policy blocked.
// Consider a fallback, like playing on user interaction.
});
}
playBGM() {
if (window._bgmAlreadyPlaying === true) {
return "nah mate, i aint playin for ya, im already playin";
} else {
window._bgmAlreadyPlaying = true;
if (wiiSound.currentBgmAudio && !wiiSound.currentBgmAudio.paused) {
wiiSound.currentBgmAudio.pause();
wiiSound.currentBgmAudio.currentTime = 0;
console.log('Stopped previous BGM.');
}
if (!_injectedWiiShopBgmUrl) {
console.warn("No BGM URL available.");
return;
}
const audio = new Audio(_injectedWiiShopBgmUrl);
audio.loop = true;
audio.style.display = 'none';
audio.play()
.then(() => {
wiiSound.currentBgmAudio = audio;
console.log('Wii Shop BGM started and looping:', _injectedWiiShopBgmUrl);
})
.catch(error => {
console.error('Error playing Wii Shop BGM:', _injectedWiiShopBgmUrl, error);
// Autoplay policy issues are common here.
});
}
}
stopBGM() {
if (window._bgmAlreadyPlaying === true){
if (wiiSound.currentBgmAudio) {
wiiSound.currentBgmAudio.pause();
wiiSound.currentBgmAudio.currentTime = 0;
wiiSound.currentBgmAudio = null;
console.log('Wii Shop BGM stopped.');
}
} else {
return "wtf do you want me to stop eh?";
}
}
}
class wiiKeyboard {
@ -458,3 +502,14 @@ window.wiiKeyboard = wiiKeyboard;
window.wiiShop = wiiShop;
window.wiiSound = wiiSound;
window.wiiMii = wiiMii;
if (_bgmInitiallyEnabled) {
// Use a short delay or an event listener to ensure all page elements are ready
// and to potentially work around immediate autoplay blocks (though not guaranteed).
document.addEventListener('DOMContentLoaded', () => {
if (window.wiiSound) {
const soundInstance = new window.wiiSound();
soundInstance.playBGM();
}
});
}

View File

@ -109,6 +109,7 @@ function checkFontState() {
});
}
// Define the relative paths to your sound files within the extension
const soundFilePaths = [
'', // Index 0 (unused)
'audio/1.wav',
@ -120,16 +121,11 @@ const soundFilePaths = [
'audio/7.wav'
];
// Generate the full, web-accessible URLs
const generatedSoundUrls = [];
for (let i = 0; i < soundFilePaths.length; i++) {
if (soundFilePaths[i]) {
// Use browser.runtime.getURL for Firefox
generatedSoundUrls[i] = browser.runtime.getURL(soundFilePaths[i]);
} else {
generatedSoundUrls[i] = '';
}
}
// Define the path for the BGM file
const bgmFilePath = 'audio/bgm.wav'; // <--- NEW BGM PATH
const generatedSoundUrls = soundFilePaths.map(path => path ? browser.runtime.getURL(path) : '');
const generatedBgmUrl = browser.runtime.getURL(bgmFilePath);
// Function to load class definitions based on the browser
const loadClassDefinitions = async () => {
@ -152,23 +148,31 @@ const loadClassDefinitions = async () => {
}
const classDefinitionsText = await response.text();
// Get the current BGM enabled state from storage
// Use browser.storage.local for Firefox compatibility
const storageResult = await browser.storage.local.get(['bgmEnabled']);
const bgmInitiallyEnabled = storageResult.bgmEnabled === true; // Default to false if not set
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
// PREPEND the sound URLs as a global variable *before* the classDefinitions.js content
// PREPEND the sound URLs, BGM URL, and BGM enabled state as global variables
script.textContent = `
(function() {
// Define a global variable that classDefinitions.js can access
window._wiiShopSoundUrls_ = ${JSON.stringify(generatedSoundUrls)};
window._wiiShopBgmUrl_ = ${JSON.stringify(generatedBgmUrl)};
window._wiiShopBgmInitiallyEnabled_ = ${JSON.stringify(bgmInitiallyEnabled)}; // <--- PASS BGM STATE
})();
` + classDefinitionsText; // Append the actual classDefinitions.js content
` + classDefinitionsText;
document.documentElement.insertBefore(script, document.documentElement.firstChild);
script.onload = function() {
this.remove();
// Clean up the global variable after classDefinitions.js has likely used it
// Clean up the global variables after classDefinitions.js has used them
delete window._wiiShopSoundUrls_;
delete window._wiiShopBgmUrl_;
delete window._wiiShopBgmInitiallyEnabled_; // <--- CLEAN UP BGM STATE
};
} catch (error) {
@ -180,4 +184,34 @@ const loadClassDefinitions = async () => {
loadClassDefinitions();
// Load the existing trace and points from localStorage
const result = checkFontState();
console.log(result);
console.log(result);
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleBGM") {
// Here, you'd typically want to call a function on the page
// to control the BGM. Since wiiSound is defined in classDefinitions.js,
// which runs in the page's context, you need to use a custom event
// or directly call window.wiiSound if it's already available.
// For simplicity, let's assume classDefinitions.js has exposed a way to call it.
// Or, we could just re-inject a small script to call it.
const controlScript = document.createElement('script');
controlScript.textContent = `
(function() {
if (window.wiiSound && typeof window.wiiSound.currentBgmAudio !== 'undefined') {
if (window.wiiSound.currentBgmAudio) {
window.wiiSound.currentBgmAudio.pause();
window.wiiSound.currentBgmAudio.currentTime = 0;
}
if (${request.enabled}) {
// Re-instantiate wiiSound to get the latest instance,
// then call playBGM(). This ensures the method exists.
new window.wiiSound().playBGM();
}
}
})();
`;
document.documentElement.appendChild(controlScript);
controlScript.onload = function() { this.remove(); };
}
});

View File

@ -34,6 +34,7 @@
"web_accessible_resources": [
{
"resources": [
"fonts/*",
"images/*",
"audio/*",
"classDefinitions.js"

View File

@ -7,7 +7,8 @@
"activeTab",
"storage",
"webRequest",
"tabs"
"tabs",
"webRequestBlocking"
],
"host_permissions": [
"<all_urls>"
@ -34,6 +35,7 @@
"web_accessible_resources": [
{
"resources": [
"fonts/*",
"images/*",
"audio/*",
"classDefinitions.js"

View File

@ -1,4 +1,3 @@
<!-- popup.html -->
<!DOCTYPE html>
<html lang="en">
<head>
@ -28,6 +27,8 @@
<label>
<input type="checkbox" id="toggleFont"> Enable Custom Font
</label>
<br> <label>
<input type="checkbox" id="toggleBGM"> Enable Persistent BGM </label>
<script src="popup.js"></script>
</body>
</html>

View File

@ -1,5 +1,6 @@
// popup.js
const STORAGE_ENDPOINT = '/_extension_storage';
const STORAGE_ENDPOINT = '/_extension_storage'; // Your existing storage endpoint
document.getElementById('clearTrace').addEventListener('click', function() {
fetch(`${STORAGE_ENDPOINT}?clogs=true`)
.catch(() => {});
@ -16,10 +17,33 @@ document.getElementById('clearPoints').addEventListener('click', function() {
document.getElementById('toggleFont').addEventListener('change', function() {
const isChecked = this.checked;
// Set the font state in the extension's storage
chrome.storage.local.set({ fontDisabled: !isChecked });
// Using chrome.storage.local for consistency with browser extension APIs
chrome.storage.local.set({ fontEnabled: isChecked }); // Renamed key to 'fontEnabled' for clarity
});
// Check the current font state when the popup is opened
chrome.storage.local.get(['fontDisabled'], function(result) {
document.getElementById('toggleFont').checked = !result.fontDisabled; // Set checkbox based on stored value
chrome.storage.local.get(['fontEnabled'], function(result) {
// Default to true if not set (font enabled by default)
document.getElementById('toggleFont').checked = (result.fontEnabled !== false);
});
// --- NEW BGM TOGGLE LOGIC ---
document.getElementById('toggleBGM').addEventListener('change', function() {
const isChecked = this.checked;
chrome.storage.local.set({ bgmEnabled: isChecked }, function() {
// Optionally, send a message to content scripts to update immediately
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "toggleBGM",
enabled: isChecked
});
});
});
});
// Check the current BGM state when the popup is opened
chrome.storage.local.get(['bgmEnabled'], function(result) {
// Default to false if not set (BGM disabled by default on startup)
document.getElementById('toggleBGM').checked = (result.bgmEnabled === true);
});