Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): extract background images (#2611)

tom870b4b1af979
diff --git a/apps/api/native/src/html.rs b/apps/api/native/src/html.rs
index e1afab37f..db6505dd4 100644
--- a/apps/api/native/src/html.rs
+++ b/apps/api/native/src/html.rs
@@ -1,13 +1,19 @@
use std::collections::{HashMap, HashSet};
+use std::sync::LazyLock;
+
use kuchikiki::{iter::NodeEdge, parse_html, traits::TendrilSink, NodeRef};
use napi_derive::napi;
use nodesig::{get_node_signature, SignatureMode};
+use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::task;
use url::Url;
+static URL_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r#"url\(['"]?([^'")]+)['"]?\)"#).expect("URL_REGEX is a valid static regex pattern"));
+
use crate::utils::to_napi_err;
fn _extract_base_href_from_document(
@@ -865,6 +871,24 @@ fn _extract_images(
}
}
+ // <... style="background: url(...)"> or <... style="background-image: url(...)">
+ if let Ok(elements) = document.select("[style*=\"background\"]") {
+ for element in elements {
+ if let Some(style) = element.attributes.borrow().get("style") {
+ for cap in URL_REGEX.captures_iter(style) {
+ if let Some(url_match) = cap.get(1) {
+ let url = url_match.as_str().trim();
+ if !url.is_empty() {
+ if let Ok(resolved) = resolve_image_url(url) {
+ images.insert(resolved);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
let filtered_images: Vec<String> = images
.into_iter()
.filter(|url| !url.to_lowercase().starts_with("javascript:"))