mirror of
https://wiilab.wiimart.org/wiimart/wiimart-extension
synced 2025-09-03 20:11:04 +02:00
browser chrome adaptation
This commit is contained in:
parent
51dd4aeab1
commit
39d23d5445
213
content.js
213
content.js
@ -3,12 +3,15 @@
|
|||||||
// Log that the content script is running
|
// Log that the content script is running
|
||||||
console.log("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;
|
let fontLoaded = false;
|
||||||
|
|
||||||
function loadFont() {
|
function loadFont() {
|
||||||
return "nuh uh";
|
const fontUrl = runtimeAPI.runtime.getURL("fonts/fot_rodin_pro_m.ttf");
|
||||||
const fontUrl = chrome.runtime.getURL("fonts/fot_rodin_pro_m.ttf");
|
|
||||||
const style = document.createElement('style');
|
const style = document.createElement('style');
|
||||||
style.textContent = `
|
style.textContent = `
|
||||||
@font-face {
|
@font-face {
|
||||||
@ -21,30 +24,13 @@ function loadFont() {
|
|||||||
* {
|
* {
|
||||||
font-family: 'Wii NTLG PGothic JPN Regular', sans-serif !important;
|
font-family: 'Wii NTLG PGothic JPN Regular', sans-serif !important;
|
||||||
}
|
}
|
||||||
#text03-01 {
|
#text03-01, #text05-01, #text06-01 {
|
||||||
display: flex; /* Enable flexbox */
|
display: flex; /* Enable flexbox */
|
||||||
justify-content: center; /* Center horizontally */
|
justify-content: center; /* Center horizontally */
|
||||||
align-items: center; /* Center vertically */
|
align-items: center; /* Center vertically */
|
||||||
height: 100vh; /* Full height of the viewport */
|
height: 100vh; /* Full height of the viewport */
|
||||||
width: 100%; /* Full width */
|
width: 100%; /* Full width */
|
||||||
}
|
}
|
||||||
|
|
||||||
#text05-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 */
|
|
||||||
}
|
|
||||||
|
|
||||||
#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 {
|
.buttonTextBlackM {
|
||||||
font-size: 24px; /* Adjust font size as needed */
|
font-size: 24px; /* Adjust font size as needed */
|
||||||
color: black; /* Set text color */
|
color: black; /* Set text color */
|
||||||
@ -52,96 +38,84 @@ function loadFont() {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Try to append to the head first
|
|
||||||
const head = document.head || document.getElementsByTagName('head')[0];
|
const head = document.head || document.getElementsByTagName('head')[0];
|
||||||
if (head) {
|
if (head) {
|
||||||
head.appendChild(style);
|
head.appendChild(style);
|
||||||
console.log("Font loaded into <head>.");
|
console.log("Font loaded into <head>.");
|
||||||
return "Font loaded into <head>"; // Return success message
|
return "Font loaded into <head>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// If head is not available, use MutationObserver to wait for it
|
|
||||||
const observer = new MutationObserver(() => {
|
const observer = new MutationObserver(() => {
|
||||||
const head = document.head || document.getElementsByTagName('head')[0];
|
const head = document.head || document.getElementsByTagName('head')[0];
|
||||||
if (head) {
|
if (head) {
|
||||||
head.appendChild(style);
|
head.appendChild(style);
|
||||||
observer.disconnect(); // Stop observing once the style is added
|
observer.disconnect();
|
||||||
console.log("Font loaded into <head> via MutationObserver.");
|
console.log("Font loaded into <head> via MutationObserver.");
|
||||||
return "Font loaded into <head> via MutationObserver"; // Return success message
|
return "Font loaded into <head> via MutationObserver";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start observing the document for changes
|
|
||||||
observer.observe(document, { childList: true, subtree: true });
|
observer.observe(document, { childList: true, subtree: true });
|
||||||
|
|
||||||
// Fallback: If the head is still not available, append to the body
|
|
||||||
const fallbackTimeout = setTimeout(() => {
|
const fallbackTimeout = setTimeout(() => {
|
||||||
if (!document.head || !document.head.contains(style)) {
|
if (!document.head || !document.head.contains(style)) {
|
||||||
document.body.appendChild(style);
|
document.body.appendChild(style);
|
||||||
console.log("Font loaded into <body> as a fallback.");
|
console.log("Font loaded into <body> as a fallback.");
|
||||||
observer.disconnect(); // Stop observing if we fall back
|
observer.disconnect();
|
||||||
return "Font loaded into <body> as a fallback"; // Return success message
|
return "Font loaded into <body> as a fallback";
|
||||||
}
|
}
|
||||||
}, 1000); // Wait for 1 second before falling back
|
}, 1000);
|
||||||
|
|
||||||
// Check if the observer is still active after 2 seconds
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// If the font is still not loaded, log an error
|
|
||||||
if (!document.head || !document.body.contains(style)) {
|
if (!document.head || !document.body.contains(style)) {
|
||||||
console.error("Failed to load font: <head> and <body> are not available.");
|
console.error("Failed to load font: <head> and <body> are not available.");
|
||||||
return "Failed to load font: <head> and <body> are not available."; // Return error message
|
return "Failed to load font: <head> and <body> are not available.";
|
||||||
}
|
}
|
||||||
observer.disconnect(); // Clear the observer
|
observer.disconnect();
|
||||||
clearTimeout(fallbackTimeout); // Clear the timeout
|
clearTimeout(fallbackTimeout);
|
||||||
}, 2000); // Check again after 2 seconds
|
}, 2000);
|
||||||
|
|
||||||
return "Font loading initiated"; // Return initial status
|
return "Font loading initiated";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the font state on page load
|
// Check the font state on page load
|
||||||
|
|
||||||
function checkFontState() {
|
function checkFontState() {
|
||||||
chrome.storage.local.get(['fontDisabled'], function(result) {
|
storageAPI.local.get(['fontDisabled']).then(function(result) {
|
||||||
if (!result.fontDisabled) {
|
if (!result.fontDisabled) {
|
||||||
return "disabled";
|
loadFont(); // Call loadFont, no need to return its string status here
|
||||||
return loadFont();
|
|
||||||
} else {
|
} else {
|
||||||
return "disabled";
|
console.log("Font loading disabled by preference.");
|
||||||
}
|
}
|
||||||
});
|
}).catch(error => console.error("Error checking font state:", error));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the relative paths to your sound files within the extension
|
|
||||||
const soundFilePaths = [
|
const soundFilePaths = [
|
||||||
'', // Index 0 (unused)
|
'', // Index 0 (unused)
|
||||||
'audio/1.wav',
|
'audio/1.wav',
|
||||||
'audio/2.wav',
|
'audio/2.wav',
|
||||||
'audio/3.wav',
|
'audio/3.wav',
|
||||||
'audio/4.wav',
|
'audio/4.wav',
|
||||||
'audio/5.wav',
|
'audio/5.wav',
|
||||||
'audio/6.wav',
|
'audio/6.wav',
|
||||||
'audio/7.wav'
|
'audio/7.wav'
|
||||||
];
|
];
|
||||||
|
const bgmFilePath = 'audio/bgm.wav'; // Define BGM file path
|
||||||
|
|
||||||
// Define the path for the BGM file
|
// Generate the full, web-accessible URLs
|
||||||
const bgmFilePath = 'audio/bgm.wav'; // <--- NEW BGM PATH
|
const generatedSoundUrls = [];
|
||||||
|
for (let i = 0; i < soundFilePaths.length; i++) {
|
||||||
const generatedSoundUrls = soundFilePaths.map(path => path ? browser.runtime.getURL(path) : '');
|
if (soundFilePaths[i]) {
|
||||||
const generatedBgmUrl = browser.runtime.getURL(bgmFilePath);
|
generatedSoundUrls[i] = runtimeAPI.runtime.getURL(soundFilePaths[i]);
|
||||||
|
|
||||||
// Function to load class definitions based on the browser
|
|
||||||
const loadClassDefinitions = async () => {
|
|
||||||
let classDefinitionsUrl;
|
|
||||||
const userAgent = navigator.userAgent.toLowerCase();
|
|
||||||
|
|
||||||
if (userAgent.includes("chrome")) {
|
|
||||||
classDefinitionsUrl = chrome.runtime.getURL('classDefinitions.js');
|
|
||||||
} else if (userAgent.includes("firefox")) {
|
|
||||||
classDefinitionsUrl = browser.runtime.getURL('classDefinitions.js');
|
|
||||||
} else {
|
} else {
|
||||||
console.warn("Unsupported browser. Class definitions will not be loaded.");
|
generatedSoundUrls[i] = '';
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
const generatedBgmUrl = runtimeAPI.runtime.getURL(bgmFilePath); // Generate BGM URL
|
||||||
|
|
||||||
|
|
||||||
|
// Function to load class definitions and inject globals
|
||||||
|
const loadClassDefinitions = async () => {
|
||||||
|
let classDefinitionsUrl = runtimeAPI.runtime.getURL('classDefinitions.js');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(classDefinitionsUrl);
|
const response = await fetch(classDefinitionsUrl);
|
||||||
@ -151,31 +125,33 @@ const loadClassDefinitions = async () => {
|
|||||||
const classDefinitionsText = await response.text();
|
const classDefinitionsText = await response.text();
|
||||||
|
|
||||||
// Get the current BGM enabled state from storage
|
// Get the current BGM enabled state from storage
|
||||||
// Use browser.storage.local for Firefox compatibility
|
const storageResult = await storageAPI.local.get(['bgmEnabled']);
|
||||||
const storageResult = await browser.storage.local.get(['bgmEnabled']);
|
|
||||||
const bgmInitiallyEnabled = storageResult.bgmEnabled === true; // Default to false if not set
|
const bgmInitiallyEnabled = storageResult.bgmEnabled === true; // Default to false if not set
|
||||||
|
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
script.setAttribute('type', 'text/javascript');
|
script.setAttribute('type', 'text/javascript');
|
||||||
|
|
||||||
// PREPEND the sound URLs, BGM URL, and BGM enabled state as global variables
|
// PREPEND the sound and BGM URLs as global variables *before* the classDefinitions.js content
|
||||||
script.textContent = `
|
script.textContent = `
|
||||||
(function() {
|
(function() {
|
||||||
|
// Define global variables that classDefinitions.js can access
|
||||||
window._wiiShopSoundUrls_ = ${JSON.stringify(generatedSoundUrls)};
|
window._wiiShopSoundUrls_ = ${JSON.stringify(generatedSoundUrls)};
|
||||||
window._wiiShopBgmUrl_ = ${JSON.stringify(generatedBgmUrl)};
|
window._wiiShopBgmUrl_ = ${JSON.stringify(generatedBgmUrl)};
|
||||||
window._wiiShopBgmInitiallyEnabled_ = ${JSON.stringify(bgmInitiallyEnabled)}; // <--- PASS BGM STATE
|
window._wiiShopBgmInitiallyEnabled_ = ${JSON.stringify(bgmInitiallyEnabled)}; // Pass BGM state
|
||||||
})();
|
})();
|
||||||
` + classDefinitionsText;
|
` + classDefinitionsText; // Append the actual classDefinitions.js content
|
||||||
|
|
||||||
document.documentElement.insertBefore(script, document.documentElement.firstChild);
|
document.documentElement.insertBefore(script, document.documentElement.firstChild);
|
||||||
|
|
||||||
script.onload = function() {
|
script.onload = function() {
|
||||||
this.remove();
|
this.remove();
|
||||||
// Clean up the global variables after classDefinitions.js has used them
|
// Clean up the global variables after classDefinitions.js has likely used them
|
||||||
delete window._wiiShopSoundUrls_;
|
// These are no longer needed as they are referenced immediately by the injected script.
|
||||||
delete window._wiiShopBgmUrl_;
|
// delete window._wiiShopSoundUrls_;
|
||||||
delete window._wiiShopBgmInitiallyEnabled_; // <--- CLEAN UP BGM STATE
|
// delete window._wiiShopBgmUrl_;
|
||||||
|
// delete window._wiiShopBgmInitiallyEnabled_; // Clean up BGM state
|
||||||
};
|
};
|
||||||
|
console.log("classDefinitions.js and global URLs injected into page context via script element.");
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to load class definitions:", error);
|
console.error("Failed to load class definitions:", error);
|
||||||
@ -185,35 +161,58 @@ const loadClassDefinitions = async () => {
|
|||||||
// Load the class definitions
|
// Load the class definitions
|
||||||
loadClassDefinitions();
|
loadClassDefinitions();
|
||||||
// Load the existing trace and points from localStorage
|
// Load the existing trace and points from localStorage
|
||||||
const result = checkFontState();
|
checkFontState(); // Call checkFontState to initiate font loading
|
||||||
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');
|
// --- Initial BGM Playback on DOMContentLoaded ---
|
||||||
controlScript.textContent = `
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
(function() {
|
// Give a slight delay to ensure classDefinitions.js and window.extensionStorage are fully ready
|
||||||
if (window.wiiSound && typeof window.wiiSound.currentBgmAudio !== 'undefined') {
|
await new Promise(resolve => setTimeout(resolve, 50));
|
||||||
if (window.wiiSound.currentBgmAudio) {
|
|
||||||
window.wiiSound.currentBgmAudio.pause();
|
// Request the initial BGM enabled state from the background script via extensionStorage
|
||||||
window.wiiSound.currentBgmAudio.currentTime = 0;
|
// We expect extensionStorage to be available after classDefinitions.js is injected
|
||||||
}
|
if (typeof window.extensionStorage !== 'undefined' && typeof window.extensionStorage.get === 'function') {
|
||||||
if (${request.enabled}) {
|
try {
|
||||||
// Re-instantiate wiiSound to get the latest instance,
|
const bgmEnabled = await window.extensionStorage.get('bgmEnabled');
|
||||||
// then call playBGM(). This ensures the method exists.
|
console.log("Initial BGM enabled state from storage:", bgmEnabled);
|
||||||
new window.wiiSound().playBGM();
|
|
||||||
}
|
if (bgmEnabled === true) { // Explicitly check for true
|
||||||
}
|
console.log("Attempting to play initial BGM from content script.");
|
||||||
})();
|
// Use the new sendMessageToBackground proxy for BGM control
|
||||||
`;
|
// This will send a request to /_extension_storage?action=playBGM&bgmUrl=...
|
||||||
document.documentElement.appendChild(controlScript);
|
window.extensionStorage.sendMessageToBackground({
|
||||||
controlScript.onload = function() { this.remove(); };
|
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.");
|
||||||
|
// Send a message to background via extensionStorage proxy for consistency
|
||||||
|
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.");
|
||||||
|
// Send a message to background via extensionStorage proxy for consistency
|
||||||
|
if (typeof window.extensionStorage !== 'undefined' && typeof window.extensionStorage.sendMessageToBackground === 'function') {
|
||||||
|
window.extensionStorage.sendMessageToBackground({
|
||||||
|
action: "stopBGM"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user