Created
February 13, 2024 03:27
-
-
Save ShayBox/38a98de0b72e1cacd7351f0c584ed440 to your computer and use it in GitHub Desktop.
Infinite Craft automated with headless_chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{collections::HashSet, time::Duration}; | |
use anyhow::Result; | |
use derive_config::{json::Value, DeriveJsonConfig, DeriveTomlConfig}; | |
use headless_chrome::{browser::default_executable, Browser, LaunchOptions}; | |
use serde::{Deserialize, Serialize}; | |
#[derive(Debug, Default, DeriveJsonConfig, Deserialize, Serialize)] | |
struct Combinations(HashSet<(String, String)>); | |
#[derive(Debug, Default, DeriveTomlConfig, Deserialize, Serialize)] | |
struct Storage(Value); | |
fn main() -> Result<()> { | |
// Create a headful browser for preview and authentication | |
let browser = Browser::new( | |
LaunchOptions::default_builder() | |
.headless(false) | |
.idle_browser_timeout(Duration::MAX) | |
.path(default_executable().ok()) | |
.window_size(Some((800, 1400))) | |
.build()?, | |
)?; | |
// Attempt to load previously saved local storage | |
let mut combinations = Combinations::load().unwrap_or_default(); | |
let mut storage = Storage::load().unwrap_or_default(); | |
let tab = browser.new_tab()?; | |
tab.navigate_to("https://neal.fun/infinite-craft")?; | |
tab.set_storage("infinite-craft-data", storage.0)?; | |
tab.navigate_to("https://neal.fun/infinite-craft")?; | |
loop { | |
let items = tab.wait_for_elements("div.mobile-item")?; | |
println!("Found {} items", items.len()); | |
// Click each item on each other item to craft | |
for item in &items { | |
let item_text = item.get_inner_text()?; | |
for other in &items { | |
let other_text = other.get_inner_text()?; | |
let combination = (item_text.clone(), other_text.clone()); | |
if combinations.0.contains(&combination) { | |
continue; | |
} | |
item.click()?; | |
other.click()?; | |
// Prevent clicking the same combination again | |
combinations.0.insert(combination); | |
combinations.0.insert((other_text, item_text.clone())); | |
} | |
} | |
// Save the local storage and combinations for next run | |
storage.0 = tab.get_storage("infinite-craft-data")?; | |
storage.save()?; | |
combinations.save()?; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment