mirror of
https://github.com/tiennm99/WPlace-AutoBOT.git
synced 2026-09-02 16:18:46 +00:00
background.js
This commit is contained in:
+383
-58
@@ -48,7 +48,7 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
} else {
|
||||
scriptUrl = chrome.runtime.getURL(`scripts/${scriptName}`);
|
||||
}
|
||||
|
||||
|
||||
const response = await fetch(scriptUrl);
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -67,38 +67,38 @@ async function executeLocalScript(scriptName, 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'];
|
||||
@@ -106,7 +106,7 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
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`);
|
||||
@@ -122,14 +122,14 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.log(` 📋 Expected files: auto-image-styles.css, acrylic.css, classic.css, etc.`);
|
||||
window.AUTOBOT_THEMES = {};
|
||||
}
|
||||
|
||||
|
||||
// Inject language data if available
|
||||
if (languages && Object.keys(languages).length > 0) {
|
||||
console.group(`%c🌍 Language Processing`, 'color: #06b6d4; font-weight: bold;');
|
||||
console.log(`%c📁 Loading ${Object.keys(languages).length} language files from extension local storage...`, 'color: #06b6d4;');
|
||||
|
||||
|
||||
window.AUTOBOT_LANGUAGES = languages;
|
||||
|
||||
|
||||
// Log detailed language information
|
||||
Object.entries(languages).forEach(([filename, content]) => {
|
||||
console.log(`%c📄 Language File: ${filename}`, 'color: #06b6d4; font-weight: bold;');
|
||||
@@ -137,18 +137,18 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.log(` 📏 Keys count: ${Object.keys(content).length.toLocaleString()}`);
|
||||
console.log(` 📍 Source: Extension local file (chrome-extension://)`);
|
||||
console.log(` 🔗 Full path: lang/${filename}`);
|
||||
|
||||
|
||||
// Show some sample keys
|
||||
const sampleKeys = Object.keys(content).slice(0, 5);
|
||||
console.log(` 🔑 Sample keys: ${sampleKeys.join(', ')}${Object.keys(content).length > 5 ? '...' : ''}`);
|
||||
console.log(` ✅ Status: Loaded successfully`);
|
||||
});
|
||||
|
||||
|
||||
// Helper function to get language data with detailed logging
|
||||
window.getLanguage = function(lang = 'en') {
|
||||
window.getLanguage = function (lang = 'en') {
|
||||
const langFile = lang + '.json';
|
||||
const result = window.AUTOBOT_LANGUAGES[langFile] || window.AUTOBOT_LANGUAGES['en.json'] || {};
|
||||
|
||||
|
||||
console.group(`%c🔤 Language Access: ${lang.toUpperCase()}`, 'color: #06b6d4; font-weight: bold;');
|
||||
console.log(` 📋 Requested: ${lang}`);
|
||||
console.log(` 📄 File: ${langFile}`);
|
||||
@@ -157,10 +157,10 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.log(` ✅ Success: ${window.AUTOBOT_LANGUAGES[langFile] ? 'Found exact match' : 'Fallback to English'}`);
|
||||
console.log(` 📝 Data preview:`, result);
|
||||
console.groupEnd();
|
||||
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
console.log(`%c🔤 Available languages: ${Object.keys(languages).map(f => f.replace('.json', '')).join(', ')}`, 'color: #06b6d4;');
|
||||
console.groupEnd();
|
||||
} else {
|
||||
@@ -168,16 +168,16 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.log(` 📁 Expected source: Extension local files`);
|
||||
console.log(` 📋 Expected files: en.json, de.json, fr.json, etc.`);
|
||||
window.AUTOBOT_LANGUAGES = {};
|
||||
window.getLanguage = function() {
|
||||
window.getLanguage = function () {
|
||||
console.warn(`%c⚠️ getLanguage() called but no languages available`, 'color: #f59e0b;');
|
||||
return {};
|
||||
return {};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Helper function to apply theme with detailed logging
|
||||
window.applyTheme = function(themeName) {
|
||||
window.applyTheme = function (themeName) {
|
||||
console.group(`%c🎨 Theme Application: ${themeName}`, 'color: #8b5cf6; font-weight: bold;');
|
||||
|
||||
|
||||
if (!window.AUTOBOT_THEMES || Object.keys(window.AUTOBOT_THEMES).length === 0) {
|
||||
console.error(`%c❌ No themes available in extension`, 'color: #ef4444; font-weight: bold;');
|
||||
console.log(` 📁 Expected source: Extension local files`);
|
||||
@@ -185,38 +185,38 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.groupEnd();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const themeFile = themeName + '.css';
|
||||
console.log(` 📋 Requested theme: ${themeName}`);
|
||||
console.log(` 📄 Looking for file: ${themeFile}`);
|
||||
console.log(` 📁 Available themes: ${Object.keys(window.AUTOBOT_THEMES).join(', ')}`);
|
||||
|
||||
|
||||
if (window.AUTOBOT_THEMES[themeFile]) {
|
||||
const themeContent = window.AUTOBOT_THEMES[themeFile];
|
||||
|
||||
|
||||
// Remove existing theme
|
||||
const existing = document.getElementById('autobot-theme');
|
||||
if (existing) {
|
||||
console.log(` 🗑️ Removing previous theme element`);
|
||||
existing.remove();
|
||||
}
|
||||
|
||||
|
||||
// Apply new theme
|
||||
const styleElement = document.createElement('style');
|
||||
styleElement.id = 'autobot-theme';
|
||||
styleElement.textContent = themeContent;
|
||||
document.head.appendChild(styleElement);
|
||||
|
||||
|
||||
console.log(`%c✅ Theme applied successfully: ${themeName}`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(` 📏 Content size: ${themeContent.length.toLocaleString()} characters`);
|
||||
console.log(` 📍 Source: Extension local file`);
|
||||
console.log(` 🎯 Target: <head> as <style> element`);
|
||||
console.log(` 🆔 Element ID: autobot-theme`);
|
||||
|
||||
|
||||
// Show preview of applied CSS
|
||||
const preview = themeContent.substring(0, 150).split('\\n').slice(0, 2).join('\\n');
|
||||
console.log(` 👀 CSS Preview: ${preview}...`);
|
||||
|
||||
|
||||
console.groupEnd();
|
||||
return true;
|
||||
} else {
|
||||
@@ -227,7 +227,7 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Final resource summary
|
||||
console.group(`%c📋 Resource Summary`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(`%c🎨 Themes loaded: ${Object.keys(window.AUTOBOT_THEMES || {}).length}`, 'color: #8b5cf6;');
|
||||
@@ -237,15 +237,15 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
console.log(` - getLanguage(lang) - Get language translations`);
|
||||
console.log(`%c📍 All resources loaded from: Extension local files`, 'color: #10b981;');
|
||||
console.groupEnd();
|
||||
|
||||
|
||||
console.groupEnd(); // End main resource report
|
||||
|
||||
|
||||
// Create script element to execute the code
|
||||
const script = document.createElement('script');
|
||||
script.textContent = code;
|
||||
document.head.appendChild(script);
|
||||
script.remove(); // Clean up after execution
|
||||
|
||||
|
||||
console.log(`%c✅ ${name} executed successfully with full resource access`, 'color: #4ade80; font-weight: bold;');
|
||||
},
|
||||
args: [scriptCode, scriptName, resources.themes, resources.languages]
|
||||
@@ -262,7 +262,7 @@ async function executeLocalScript(scriptName, tabId) {
|
||||
async function loadExtensionResources() {
|
||||
console.group('%c🔧 WPlace AutoBOT Resource Loading System', 'color: #3b82f6; font-weight: bold; font-size: 16px;');
|
||||
const startTime = performance.now();
|
||||
|
||||
|
||||
const resources = {
|
||||
themes: {},
|
||||
languages: {}
|
||||
@@ -270,13 +270,13 @@ async function loadExtensionResources() {
|
||||
|
||||
try {
|
||||
console.log('%c� Starting resource loading from extension directory...', 'color: #3b82f6; font-weight: bold;');
|
||||
|
||||
|
||||
// Load theme files
|
||||
console.group('%c🎨 Theme Files Loading', 'color: #8b5cf6; font-weight: bold;');
|
||||
const themeFiles = [
|
||||
'auto-image-styles.css',
|
||||
'themes/acrylic.css',
|
||||
'themes/classic-light.css',
|
||||
'themes/classic-light.css',
|
||||
'themes/classic.css',
|
||||
'themes/neon.css',
|
||||
'themes/neon-cyan.css',
|
||||
@@ -288,34 +288,34 @@ async function loadExtensionResources() {
|
||||
console.log(`%c� Loading theme: ${themeFile}`, 'color: #8b5cf6;');
|
||||
console.log(` 📍 Source path: ${themeFile}`);
|
||||
console.log(` 🔗 Full URL: chrome-extension://${chrome.runtime.id}/${themeFile}`);
|
||||
|
||||
|
||||
const themeUrl = chrome.runtime.getURL(themeFile);
|
||||
console.log(` 🌐 Resolved URL: ${themeUrl}`);
|
||||
|
||||
|
||||
const response = await fetch(themeUrl);
|
||||
console.log(` 📡 Fetch response status: ${response.status} ${response.statusText}`);
|
||||
console.log(` 📋 Response headers:`, Object.fromEntries(response.headers.entries()));
|
||||
|
||||
|
||||
if (response.ok) {
|
||||
const content = await response.text();
|
||||
const fileName = themeFile.split('/').pop();
|
||||
resources.themes[fileName] = content;
|
||||
|
||||
|
||||
console.log(`%c✅ ${fileName} loaded successfully`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(` 📏 File size: ${content.length.toLocaleString()} characters`);
|
||||
console.log(` 📊 File size: ${(content.length / 1024).toFixed(2)} KB`);
|
||||
console.log(` 🔍 Content type: CSS stylesheet`);
|
||||
|
||||
|
||||
// Show content preview
|
||||
const firstLine = content.split('\n')[0];
|
||||
const lastLine = content.split('\n').slice(-1)[0];
|
||||
console.log(` 👀 First line: ${firstLine.substring(0, 100)}${firstLine.length > 100 ? '...' : ''}`);
|
||||
console.log(` 👀 Last line: ${lastLine.substring(0, 100)}${lastLine.length > 100 ? '...' : ''}`);
|
||||
|
||||
|
||||
// Count CSS rules
|
||||
const ruleCount = (content.match(/\{[^}]*\}/g) || []).length;
|
||||
console.log(` 📝 Estimated CSS rules: ${ruleCount.toLocaleString()}`);
|
||||
|
||||
|
||||
} else {
|
||||
console.error(`%c❌ Failed to load ${themeFile}`, 'color: #ef4444; font-weight: bold;');
|
||||
console.error(` 📡 Status: ${response.status} ${response.statusText}`);
|
||||
@@ -369,35 +369,35 @@ async function loadExtensionResources() {
|
||||
console.log(` 🌐 Language: ${langFile.replace('lang/', '').replace('.json', '').toUpperCase()}`);
|
||||
console.log(` 📍 Source path: ${langFile}`);
|
||||
console.log(` 🔗 Full URL: chrome-extension://${chrome.runtime.id}/${langFile}`);
|
||||
|
||||
|
||||
const langUrl = chrome.runtime.getURL(langFile);
|
||||
console.log(` 🌐 Resolved URL: ${langUrl}`);
|
||||
|
||||
|
||||
const response = await fetch(langUrl);
|
||||
console.log(` 📡 Fetch response status: ${response.status} ${response.statusText}`);
|
||||
|
||||
|
||||
if (response.ok) {
|
||||
const text = await response.text();
|
||||
console.log(` 📏 Raw text size: ${text.length.toLocaleString()} characters`);
|
||||
|
||||
|
||||
const parsed = JSON.parse(text);
|
||||
const fileName = langFile.split('/').pop();
|
||||
resources.languages[fileName] = parsed;
|
||||
|
||||
|
||||
console.log(`%c✅ ${fileName} loaded successfully`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(` 📏 JSON size: ${text.length.toLocaleString()} characters`);
|
||||
console.log(` 📊 File size: ${(text.length / 1024).toFixed(2)} KB`);
|
||||
console.log(` 🔑 Translation keys: ${Object.keys(parsed).length.toLocaleString()}`);
|
||||
console.log(` 🔍 Content type: JSON translation data`);
|
||||
|
||||
|
||||
// Show some sample keys
|
||||
const sampleKeys = Object.keys(parsed).slice(0, 5);
|
||||
console.log(` 🎯 Sample keys: ${sampleKeys.join(', ')}${Object.keys(parsed).length > 5 ? '...' : ''}`);
|
||||
|
||||
|
||||
// Show sample translations
|
||||
const samples = sampleKeys.map(key => `${key}: "${parsed[key]}"`).slice(0, 3);
|
||||
console.log(` 📝 Sample translations: ${samples.join(', ')}`);
|
||||
|
||||
|
||||
} else {
|
||||
console.error(`%c❌ Failed to load ${langFile}`, 'color: #ef4444; font-weight: bold;');
|
||||
console.error(` 📡 Status: ${response.status} ${response.statusText}`);
|
||||
@@ -415,28 +415,28 @@ async function loadExtensionResources() {
|
||||
console.groupEnd();
|
||||
|
||||
const loadTime = performance.now() - startTime;
|
||||
|
||||
|
||||
// Final summary with detailed statistics
|
||||
console.group('%c� Resource Loading Summary', 'color: #10b981; font-weight: bold;');
|
||||
console.log(`%c⏱️ Total loading time: ${loadTime.toFixed(2)}ms`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(`%c🎨 Themes loaded: ${Object.keys(resources.themes).length}/${themeFiles.length}`, 'color: #8b5cf6; font-weight: bold;');
|
||||
console.log(`%c🌍 Languages loaded: ${Object.keys(resources.languages).length}/${languageFiles.length}`, 'color: #06b6d4; font-weight: bold;');
|
||||
|
||||
|
||||
// Calculate total size
|
||||
const themeSize = Object.values(resources.themes).reduce((sum, content) => sum + content.length, 0);
|
||||
const langSize = Object.values(resources.languages).reduce((sum, content) => sum + JSON.stringify(content).length, 0);
|
||||
const totalSize = themeSize + langSize;
|
||||
|
||||
|
||||
console.log(`%c📊 Total data loaded: ${(totalSize / 1024).toFixed(2)} KB`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(` 🎨 Themes: ${(themeSize / 1024).toFixed(2)} KB`);
|
||||
console.log(` 🌍 Languages: ${(langSize / 1024).toFixed(2)} KB`);
|
||||
|
||||
|
||||
console.log(`%c📁 Resource sources:`, 'color: #10b981; font-weight: bold;');
|
||||
console.log(` 📍 Extension ID: ${chrome.runtime.id}`);
|
||||
console.log(` 🔗 Base URL: chrome-extension://${chrome.runtime.id}/`);
|
||||
console.log(` 📂 Themes folder: chrome-extension://${chrome.runtime.id}/themes/`);
|
||||
console.log(` 📂 Languages folder: chrome-extension://${chrome.runtime.id}/lang/`);
|
||||
|
||||
|
||||
// List successful loads
|
||||
if (Object.keys(resources.themes).length > 0) {
|
||||
console.log(`%c✅ Loaded themes: ${Object.keys(resources.themes).join(', ')}`, 'color: #8b5cf6;');
|
||||
@@ -445,7 +445,7 @@ async function loadExtensionResources() {
|
||||
const langs = Object.keys(resources.languages).map(f => f.replace('.json', '')).join(', ');
|
||||
console.log(`%c✅ Loaded languages: ${langs}`, 'color: #06b6d4;');
|
||||
}
|
||||
|
||||
|
||||
console.log(`%c🚀 Resources ready for injection into scripts`, 'color: #10b981; font-weight: bold;');
|
||||
console.groupEnd();
|
||||
console.groupEnd();
|
||||
@@ -463,4 +463,329 @@ async function loadExtensionResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
const cookieDomain = ".backend.wplace.live";
|
||||
|
||||
async function preserveAndResetJ() {
|
||||
let savedValue = null;
|
||||
|
||||
try {
|
||||
const oldJ = await chrome.cookies.get({
|
||||
url: "https://backend.wplace.live/",
|
||||
name: "j",
|
||||
});
|
||||
|
||||
if (oldJ && oldJ.value) {
|
||||
savedValue = oldJ.value.trim();
|
||||
console.log("[bg] Saved j cookie:", savedValue);
|
||||
|
||||
const accountInfo = await checkTokenAndGetInfo(savedValue);
|
||||
|
||||
if (accountInfo) {
|
||||
const store = await chrome.storage.local.get("infoAccounts");
|
||||
let infoAccounts = store.infoAccounts || [];
|
||||
|
||||
const existingIndex = infoAccounts.findIndex(account => account.ID === accountInfo.ID);
|
||||
|
||||
if (existingIndex > -1) {
|
||||
// Merge existing data with new data to preserve any additional info
|
||||
const existingAccount = infoAccounts[existingIndex];
|
||||
infoAccounts[existingIndex] = {
|
||||
...existingAccount, // Keep existing additional data
|
||||
...accountInfo, // Update with fresh data from API
|
||||
token: accountInfo.token, // Ensure token is updated
|
||||
name: accountInfo.name, // Ensure name is updated
|
||||
lastActive: new Date().toISOString() // Update last seen
|
||||
};
|
||||
console.log(`✅ ID ${accountInfo.ID} found. Updated account info.`);
|
||||
} else {
|
||||
infoAccounts.push(accountInfo);
|
||||
console.log(`✅ New account added: ${accountInfo.name} (${accountInfo.ID}).`);
|
||||
}
|
||||
|
||||
await chrome.storage.local.set({ infoAccounts });
|
||||
await exportInfoAccount();
|
||||
|
||||
} else {
|
||||
console.warn("❌ Current token is invalid, not saving.");
|
||||
}
|
||||
} else {
|
||||
console.warn("[bg] No 'j' cookie found. Skipping account verification.");
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
await new Promise((resolve) => {
|
||||
chrome.browsingData.remove(
|
||||
{ origins: ["https://wplace.live"] },
|
||||
{ cookies: true },
|
||||
() => {
|
||||
console.log("[bg] Nuke done (delayed)");
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
if (savedValue) {
|
||||
setCookie(savedValue);
|
||||
} else {
|
||||
console.log("[bg] No j to restore, leaving nuked");
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
} catch (err) {
|
||||
console.error("[bg] Error in preserveAndResetJ:", err);
|
||||
}
|
||||
}
|
||||
|
||||
async function filterInvalid() {
|
||||
const store = await chrome.storage.local.get("infoAccounts");
|
||||
let infoAccounts = store.infoAccounts || [];
|
||||
let validAccounts = [];
|
||||
|
||||
for (const account of infoAccounts) {
|
||||
// This is a simplified check for validity
|
||||
const isValid = await checkTokenAndGetInfo(account.token);
|
||||
if (isValid) {
|
||||
console.log("Token valid:", account.token);
|
||||
validAccounts.push(account);
|
||||
|
||||
} else {
|
||||
console.log("Token invalid:", account.token);
|
||||
}
|
||||
}
|
||||
|
||||
await chrome.storage.local.set({ infoAccounts: validAccounts });
|
||||
console.log(`✅ Filtered and saved ${validAccounts.length} valid accounts.`);
|
||||
await exportInfoAccount();
|
||||
}
|
||||
|
||||
async function exportInfoAccount() {
|
||||
const store = await chrome.storage.local.get("infoAccounts");
|
||||
const infoAccounts = store.infoAccounts || [];
|
||||
const accounts = infoAccounts.map(info => info.token);
|
||||
await chrome.storage.local.set({ accounts });
|
||||
}
|
||||
|
||||
async function checkTokenAndGetInfo(token) {
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
console.log("Checking with token:", token);
|
||||
try {
|
||||
await setCookie(token);
|
||||
const response = await fetch("https://backend.wplace.live/me", {
|
||||
method: "GET",
|
||||
headers: { "Authorization": `Bearer ${token}` }
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const userData = await response.json();
|
||||
console.log("Basic user data:", userData);
|
||||
|
||||
// Enhanced user info object with WPlace API fields
|
||||
const userInfo = {
|
||||
ID: userData.id,
|
||||
name: userData.name,
|
||||
token: token,
|
||||
Charges: Math.floor(userData.charges.count),
|
||||
Max: userData.charges.max,
|
||||
Droplets: userData.droplets,
|
||||
totalPixelsPainted: userData.pixelsPainted,
|
||||
level: Math.floor(userData.level),
|
||||
showLastPixel: userData.showLastPixel,
|
||||
discord: userData.discord,
|
||||
};
|
||||
// console.log("User info after /me:", userInfo);
|
||||
|
||||
try {
|
||||
const allianceResponse = await fetch(`https://backend.wplace.live/alliance/`, {
|
||||
method: "GET",
|
||||
headers: { "Authorization": `Bearer ${token}` }
|
||||
});
|
||||
|
||||
if (allianceResponse.ok) {
|
||||
const allianceData = await allianceResponse.json();
|
||||
userInfo.allianceName = allianceData.name;
|
||||
userInfo.allianceRole = allianceData.role;
|
||||
}
|
||||
} catch (allianceError) {
|
||||
console.log("Alliance info not available:", allianceError.message);
|
||||
}
|
||||
|
||||
console.log("Enhanced user info:", userInfo);
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
if (response.status === 401) {
|
||||
console.log("Token invalid: Unauthorized (401)");
|
||||
} else {
|
||||
console.error(`⚠️ Token check failed with status: ${response.status}`);
|
||||
}
|
||||
return null;
|
||||
|
||||
} catch (e) {
|
||||
console.error("❌ Network or fetch error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
chrome.webNavigation.onCompleted.addListener(
|
||||
async (details) => {
|
||||
if (details.url.includes("wplace.live")) {
|
||||
console.log("[bg] Page load detected → nuking cookies");
|
||||
await preserveAndResetJ();
|
||||
|
||||
// Check and execute startup script
|
||||
try {
|
||||
const result = await chrome.storage.local.get('startupScript');
|
||||
if (result.startupScript) {
|
||||
console.log(`[bg] Executing startup script: ${result.startupScript}`);
|
||||
await executeLocalScript(result.startupScript, details.tabId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[bg] Error executing startup script:', error);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ url: [{ hostContains: "wplace.live" }] }
|
||||
);
|
||||
|
||||
async function setCookie(value) {
|
||||
const cleaned = value.trim();
|
||||
console.log("[bg] setCookie CALLED with:", cleaned);
|
||||
|
||||
chrome.cookies.set(
|
||||
{
|
||||
url: "https://backend.wplace.live/",
|
||||
name: "j",
|
||||
value: cleaned,
|
||||
domain: cookieDomain,
|
||||
path: "/",
|
||||
},
|
||||
(cookie) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
console.error(
|
||||
"[bg] cookie set error:",
|
||||
chrome.runtime.lastError.message
|
||||
);
|
||||
} else {
|
||||
console.log("[bg] cookie set result:", cookie);
|
||||
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
if (tabs.length > 0) {
|
||||
chrome.tabs.sendMessage(tabs[0].id, {
|
||||
type: "cookieSet",
|
||||
value: cleaned,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteAccountAtIndex(index) {
|
||||
try {
|
||||
const data = await chrome.storage.local.get("infoAccounts");
|
||||
let infoAccounts = data.infoAccounts || [];
|
||||
|
||||
if (index < 0 || index >= infoAccounts.length) {
|
||||
console.warn("[bg] Invalid index:", index);
|
||||
return false;
|
||||
}
|
||||
|
||||
const removed = infoAccounts.splice(index, 1);
|
||||
await chrome.storage.local.set({ infoAccounts });
|
||||
|
||||
console.log(`[bg] Deleted account at index ${index}:`, removed[0]);
|
||||
console.log(`[bg] Remaining accounts:`, infoAccounts);
|
||||
await exportInfoAccount();
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error("[bg] Error in deleteAccountAtIndex:", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
console.log("📩 Background received message:", msg);
|
||||
|
||||
if (msg.type === "setCookie" && msg.value) {
|
||||
(async () => {
|
||||
try {
|
||||
console.log("🍪 Setting cookie...");
|
||||
await setCookie(msg.value);
|
||||
console.log("✅ Cookie set successfully");
|
||||
sendResponse({ status: "ok" });
|
||||
} catch (e) {
|
||||
console.error("❌ setCookie failed", e);
|
||||
sendResponse({ status: "error", error: e.message });
|
||||
}
|
||||
})();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (msg.type === "getAccounts") {
|
||||
(async () => {
|
||||
try {
|
||||
console.log("📂 Fetching accounts...");
|
||||
await filterInvalid();
|
||||
const result = await chrome.storage.local.get("accounts");
|
||||
console.log("📤 Returning accounts:", result.accounts);
|
||||
sendResponse({ accounts: result.accounts || [] });
|
||||
} catch (e) {
|
||||
console.error("❌ getAccounts failed", e);
|
||||
sendResponse({ accounts: [] });
|
||||
}
|
||||
})();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (msg.type === "deleteAccount" && typeof msg.index === "number") {
|
||||
(async () => {
|
||||
try {
|
||||
const ok = await deleteAccountAtIndex(msg.index);
|
||||
sendResponse({ status: ok ? "ok" : "error" });
|
||||
} catch (e) {
|
||||
console.error("❌ deleteAccount failed", e);
|
||||
sendResponse({ status: "error", error: e.message });
|
||||
}
|
||||
})();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (msg.type === "refreshCurrentAccount") {
|
||||
(async () => {
|
||||
try {
|
||||
console.log("🔄 Refreshing current account...");
|
||||
await preserveAndResetJ();
|
||||
sendResponse({ status: "ok" });
|
||||
} catch (e) {
|
||||
console.error("❌ refreshCurrentAccount failed", e);
|
||||
sendResponse({ status: "error", error: e.message });
|
||||
}
|
||||
})();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (msg.action === 'executeScript') {
|
||||
// Get tabId from sender or request
|
||||
const tabId = msg.tabId || sender.tab?.id;
|
||||
|
||||
if (!tabId) {
|
||||
sendResponse({ success: false, error: 'Could not determine target tab' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Use IIFE for async handling
|
||||
(async () => {
|
||||
try {
|
||||
await executeLocalScript(msg.scriptName, tabId);
|
||||
sendResponse({ success: true });
|
||||
} catch (error) {
|
||||
sendResponse({ success: false, error: error.message });
|
||||
}
|
||||
})();
|
||||
|
||||
return true; // Important: indicates async response
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user