// Background service worker for WPlace AutoBOT Extension // Test resource loading on extension starto chrome.runtime.onStartup.addListener(async () => { console.log('š WPlace AutoBOT Extension started'); const resources = await loadExtensionResources(); console.log('š¦ Initial resource test:', resources); }); chrome.runtime.onInstalled.addListener(async () => { console.log('š§ WPlace AutoBOT Extension installed'); const resources = await loadExtensionResources(); console.log('š¦ Installation resource test:', resources); }); // Helper to load and inject a local script file into the page (MAIN world) async function injectPlainScriptFromExtension(tabId, relativePath, friendlyName, color, emoji) { const url = chrome.runtime.getURL(relativePath); try { const response = await fetch(url); if (!response.ok) { console.warn(`ā ļø Could not load ${relativePath}, proceeding without it`); return false; } const code = await response.text(); console.log(`${emoji} ${friendlyName} loaded, injecting...`); await chrome.scripting.executeScript({ target: { tabId: tabId }, world: "MAIN", func: (code, friendlyName, color, emoji) => { console.log(`%c${emoji} Loading ${friendlyName}...`, `color: ${color}; font-weight: bold;`); const script = document.createElement('script'); script.textContent = code; document.head.appendChild(script); script.remove(); console.log(`%cā ${friendlyName} loaded successfully`, `color: ${color}; font-weight: bold;`); }, args: [code, friendlyName, color, emoji] }); console.log(`ā ${friendlyName} injected successfully`); return true; } catch (e) { console.warn(`ā ļø Error injecting ${friendlyName}:`, e); return false; } } async function executeLocalScript(scriptName, tabId) { try { console.log(`Loading script: ${scriptName}`); // Check if we need to inject dependencies first for Auto-Image.js or Art-Extractor.js if (scriptName === 'Auto-Image.js' || scriptName === 'Art-Extractor.js') { console.log(`š ${scriptName} detected, ensuring dependencies are loaded first...`); try { await injectPlainScriptFromExtension(tabId, 'scripts/token-manager.js', 'WPlace Token Manager', '#4ade80', 'š'); await injectPlainScriptFromExtension(tabId, 'scripts/image-processor.js', 'WPlace Image Processor', '#3b82f6', 'š¼ļø'); await injectPlainScriptFromExtension(tabId, 'scripts/overlay-manager.js', 'WPlace Overlay Manager', '#8b5cf6', 'šØ'); await injectPlainScriptFromExtension(tabId, 'scripts/utils-manager.js', 'WPlace Utils Manager', '#f59e0b', 'š ļø'); // Small delay to ensure dependencies are fully initialized await new Promise(resolve => setTimeout(resolve, 200)); } catch (error) { console.warn('ā ļø Error loading dependencies:', error); } } // Determine script path - Script-manager files are in root, others in scripts/ let scriptUrl; if (scriptName === 'Script-manager.js' || scriptName === 'Script-manager-fixed.js') { scriptUrl = chrome.runtime.getURL(scriptName); } else { scriptUrl = chrome.runtime.getURL(`scripts/${scriptName}`); } const response = await fetch(scriptUrl); if (!response.ok) { throw new Error(`Failed to load script: ${response.status} ${response.statusText}`); } const scriptCode = await response.text(); console.log(`Script loaded: ${scriptCode.length} characters`); // Load theme and language resources const resources = await loadExtensionResources(); // Execute in MAIN world context (bypasses CSP) await chrome.scripting.executeScript({ target: { tabId: tabId }, world: "MAIN", // Key: executes in page context, not extension context func: (code, name, themeCSS, languages) => { console.log(`%cš Executing ${name}...`, 'color: #4ade80; font-weight: bold; font-size: 14px;'); // Create detailed resource report console.group(`%cš WPlace AutoBOT Resource Report for ${name}`, 'color: #3b82f6; font-weight: bold; font-size: 16px;'); // Debug: Log what resources we received console.log(`%cš¦ Raw Resources Received:`, 'color: #8b5cf6; font-weight: bold;'); console.log(` - Themes object:`, themeCSS); console.log(` - Languages object:`, languages); console.log(` - Theme count: ${Object.keys(themeCSS || {}).length}`); console.log(` - Language count: ${Object.keys(languages || {}).length}`); // Inject CSS themes if available if (themeCSS && Object.keys(themeCSS).length > 0) { console.group(`%cšØ Theme Processing`, 'color: #8b5cf6; font-weight: bold;'); console.log(`%cš Loading ${Object.keys(themeCSS).length} theme files from extension local storage...`, 'color: #8b5cf6;'); // Create a global themes object window.AUTOBOT_THEMES = themeCSS; // Log detailed theme information Object.entries(themeCSS).forEach(([filename, content]) => { console.log(`%cš Theme File: ${filename}`, 'color: #8b5cf6; font-weight: bold;'); console.log(` š Size: ${content.length.toLocaleString()} characters`); console.log(` š Source: Extension local file (chrome-extension://)`); console.log(` š Full path: themes/${filename}`); // Show first few lines as preview const preview = content.substring(0, 200).split('\\n').slice(0, 3).join('\\n'); console.log(` š Preview: ${preview}${content.length > 200 ? '...' : ''}`); console.log(` ā Status: Loaded successfully`); }); // Inject auto-image-styles.css if available if (themeCSS['auto-image-styles.css']) { const autoImageContent = themeCSS['auto-image-styles.css']; const styleElement = document.createElement('style'); styleElement.id = 'autobot-auto-image-styles'; styleElement.textContent = autoImageContent; document.head.appendChild(styleElement); console.log(`%c⨠AUTO-INJECTED: auto-image-styles.css`, 'color: #10b981; font-weight: bold;'); console.log(` š Injected size: ${autoImageContent.length.toLocaleString()} characters`); console.log(` š Source: Extension local file`); console.log(` šÆ Target:
as