This recipe combines browser navigation, DOM extraction, workspace storage, and full-text search indexing.
// Scrape and Store — Extract data and save it//// This script navigates to a page, extracts structured data// using DOM queries, and saves results to the workspace store.// Combines browser automation, data extraction, and persistence.// Requires an open workspace.
const targetUrl = "https://example.com";const collection = "scraped-pages";
// Ensure we have a browser tabawait g.tabs.newBrowserTab();
g.log("Navigating to " + targetUrl + "...");await g.nav.navigateToUrl(targetUrl);
// Extract data from the pageconst title = await g.page.executeJS("document.title");const heading = await g.dom.extractContent("h1");const bodyText = await g.dom.extractContent("p");
// Build a structured recordconst pageData = { url: targetUrl, title: title, heading: heading, excerpt: bodyText ? bodyText.substring(0, 200) : "", scrapedAt: new Date().toISOString()};
g.log("Extracted: " + pageData.title);
// Save to the workspace storeconst key = targetUrl.replace(/[^a-zA-Z0-9]/g, "-");g.store.put(collection, key, pageData);g.log("Saved to store: " + collection + "/" + key);
// Index for full-text searchg.search.index({ _key: key, _collection: collection, title: pageData.title, heading: pageData.heading, excerpt: pageData.excerpt, url: pageData.url});g.log("Indexed for search");
// Verify: query it backconst results = g.search.query("example");if (results && results.length > 0) { g.log("Search found " + results.length + " result(s)");}