Files
background.js
content.js
manifest.json
popup.css
popup.html
popup.js
Code
6 862
content.js
// === Infinite Craft Automation Engine ===
const BASIC_ELEMENTS = ['Fire', 'Water', 'Earth', 'Wind'];
class InfiniteCraftPlayer {
constructor() {
this.elements = new Set(BASIC_ELEMENTS);
this.tried = new Set();
this.running = false;
this.speed = 600;
this.stats = { tried: 0, found: 4, startedAt: null, discoveries: 0 };
this.observer = null;
this.pendingCheck = false;
this.elementCache = new Map();
this.isProcessing = false;
this.needsRescan = false;
this.abortController = null;
}
sendState() {
chrome.runtime.sendMessage({
type: 'state',
data: {
running: this.running,
elements: this.elements.size,
tried: this.stats.tried,
startedAt: this.stats.startedAt,
discoveries: this.stats.discoveries
}
});
}
log(type, text) {
chrome.runtime.sendMessage({ type: 'activity', data: { type, text } });
}
error(text) {
chrome.runtime.sendMessage({ type: 'error', data: { text } });
}
findElementNodes() {
// Try multiple strategies to find element nodes in the game
const candidates = new Set();
// Strategy 1: Look for elements with specific data attributes or classes
const selectors = [
'[class*="element-sidebar"] [class*="item"]',
'[class*="sidebar-item"]',
'[class*="element"] [class*="name"]',
'[class*="craft-element"]',
'[class*="inventory"] [class*="item"]',
'[class*="elementList"] [class*="element"]',
'[class*="sidebar-content"] > div',
'[class*="element"]'
];
for (const sel of selectors) {
try {
const nodes = document.querySelectorAll(sel);
if (nodes.length >= 2) {
nodes.forEach(n => candidates.add(n));
break;
}
} catch (e) {}
}
// Strategy 2: Find all draggable elements with short text
if (candidates.size < 2) {
const all = document.querySelectorAll('[draggable="true"], [class*="elem"], [class*="item"], [class*="tile"]');
all.forEach(n => {
const text = n.textContent.trim();
if (text && text.length < 40 && !text.includes('\n')) {
candidates.add(n);
}
});
}
// Strategy 3: Look for any text-bearing elements that look like game elements
if (candidates.size < 2) {
const possibleContainers = document.querySelectorAll('main, [class*="game"], [class*="app"], [class*="craft"]');
possibleContainers.forEach(container => {
const textElements = container.querySelectorAll('span, div, p');
textElements.forEach(el => {
const text = el.textContent.trim();
if (text && text.length < 30 && /^[A-Z][a-z]/.test(text)) {
candidates.add(el);
}
});
});
}
// Filter to only keep valid element nodes (has text, reasonable size)
return Array.from(candidates).filter(n => {
const text = n.textContent.trim();
return text && text.length < 40 && text.length > 0;
});
}
scanForNewElements() {
const nodes = this.findElementNodes();
const found = new Set();
for (const node of nodes) {
const text = node.textContent.trim();
if (text && text.length < 40) {
found.add(text);
if (!this.elements.has(text)) {
this.elementCache.set(text, node);
}
}
}
let added = 0;
for (const name of found) {
if (!this.elements.has(name) && !BASIC_ELEMENTS.includes(name)) {
this.elements.add(name);
added++;
this.stats.discoveries++;
this.log('found', `✦ ${name}`);
this.sendState();
}
}
// Cache the DOM nodes for elements we know
for (const name of this.elements) {
if (!this.elementCache.has(name)) {
const match = nodes.find(n => n.textContent.trim() === name);
if (match) this.elementCache.set(name, match);
}
}
if (added > 0) {
this.log('info', `Discovered ${added} new element(s)! Total: ${this.elements.size}`);
}
return added;
}
startObserver() {
if (this.observer) this.observer.disconnect();
this.observer = new MutationObserver(() => {
if (!this.pendingCheck) {
this.pendingCheck = true;
setTimeout(() => {
this.pendingCheck = false;
this.scanForNewElements();
}, 500);
}
});
this.observer.observe(document.body, {
childList: true,
subtree: true,
characterData: true
});
}
stopObserver() {
if (this.observer) {
this.observer.disconnect();
this.observer = null;
}
}
async getElementNode(name) {
// Check cache first
if (this.elementCache.has(name)) {
const cached = this.elementCache.get(name);
if (document.body.contains(cached)) return cached;
}
// Search again
const nodes = this.findElementNodes();
for (const node of nodes) {
if (node.textContent.trim() === name) {
this.elementCache.set(name, node);
return node;
}
}
return null;
}
async tryCombine(a, b) {
const key = [a, b].sort().join('|');
if (this.tried.has(key)) return false;
this.tried.add(key);
const nodeA = await this.getElementNode(a);
const nodeB = await this.getElementNode(b);
if (!nodeA || !nodeB) return false;
const rectA = nodeA.getBoundingClientRect();
const rectB = nodeB.getBoundingClientRect();
if (rectA.width === 0 || rectA.height === 0 || rectB.width === 0 || rectB.height === 0) {
return false;
}