Refactor to use ArtifactScraper singleton

This commit is contained in:
2025-07-11 14:45:21 -04:00
parent a5bbaaa470
commit b8a7f7a192

View File

@ -21,8 +21,11 @@
return true; return true;
} }
// Step 4: Open the artifact list menu // Static object containing all artifact scraping functionality
function openArtifactList() { const ArtifactScraper = {
// Open the artifact list menu
openArtifactList() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Find the global artifact menu button (has the hamburger/list icon) // Find the global artifact menu button (has the hamburger/list icon)
const artifactMenuButton = document.querySelector('button[aria-haspopup="menu"] svg[viewBox="0 0 256 256"] path[d*="M80,64a8,8,0,0,1,8-8H216"]'); const artifactMenuButton = document.querySelector('button[aria-haspopup="menu"] svg[viewBox="0 0 256 256"] path[d*="M80,64a8,8,0,0,1,8-8H216"]');
@ -82,10 +85,10 @@
} }
}, 3000); }, 3000);
}); });
} },
// Step 5: Parse the artifact list and return filename -> isSelected hash // Parse the artifact list and return filename -> isSelected hash
function getArtifactListItems() { getArtifactListItems() {
const menuItems = document.querySelectorAll('li[role="none"] div[role="menuitem"]'); const menuItems = document.querySelectorAll('li[role="none"] div[role="menuitem"]');
const artifactHash = {}; const artifactHash = {};
@ -107,10 +110,10 @@
}); });
return artifactHash; return artifactHash;
} },
// Step 6: Select a specific artifact list item // Select a specific artifact list item
function selectArtifactListItem(filename) { selectArtifactListItem(filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const menuItems = document.querySelectorAll('li[role="none"] div[role="menuitem"]'); const menuItems = document.querySelectorAll('li[role="none"] div[role="menuitem"]');
let targetItem = null; let targetItem = null;
@ -145,10 +148,10 @@
resolve(true); resolve(true);
}, 1000); }, 1000);
}); });
} },
// Step 7: Get artifact content and handle selection if needed // Get artifact content and handle selection if needed
function getArtifact(filename, isCurrentlySelected) { getArtifact(filename, isCurrentlySelected) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
// If not currently selected, we need to select it // If not currently selected, we need to select it
if (!isCurrentlySelected) { if (!isCurrentlySelected) {
@ -156,7 +159,7 @@
const menuOpen = document.querySelector('li[role="none"] div[role="menuitem"]'); const menuOpen = document.querySelector('li[role="none"] div[role="menuitem"]');
if (!menuOpen) { if (!menuOpen) {
try { try {
await openArtifactList(); await this.openArtifactList();
} catch (error) { } catch (error) {
reject(new Error(`Failed to open menu to select: ${filename} - ${error.message}`)); reject(new Error(`Failed to open menu to select: ${filename} - ${error.message}`));
return; return;
@ -165,14 +168,14 @@
// Select the artifact // Select the artifact
try { try {
await selectArtifactListItem(filename); await this.selectArtifactListItem(filename);
} catch (error) { } catch (error) {
reject(new Error(`Failed to select artifact: ${filename} - ${error.message}`)); reject(new Error(`Failed to select artifact: ${filename} - ${error.message}`));
return; return;
} }
} }
// Extract content from the code block // Extract content from the code block using the updated selector
const codeBlock = document.querySelector('div.right-0 code'); const codeBlock = document.querySelector('div.right-0 code');
if (!codeBlock) { if (!codeBlock) {
reject(new Error(`No code block found for artifact: ${filename}`)); reject(new Error(`No code block found for artifact: ${filename}`));
@ -184,24 +187,20 @@
resolve(content); resolve(content);
}); });
} },
// Step 2: Main collection method // Main collection method
async function collectArtifacts() { async collectArtifacts() {
console.log('Starting artifact collection...'); console.log('Starting artifact collection...');
// Step 3: Initialize empty collection // Initialize empty collection
const artifactCollection = {}; const artifactCollection = {};
// Step 4: Open artifact list // Open artifact list
const menuOpened = await openArtifactList(); await this.openArtifactList();
if (!menuOpened) {
console.log('Failed to open artifact list');
return;
}
// Step 5: Get list of artifacts // Get list of artifacts
const artifactList = getArtifactListItems(); const artifactList = this.getArtifactListItems();
if (Object.keys(artifactList).length === 0) { if (Object.keys(artifactList).length === 0) {
console.log('No artifacts found in list'); console.log('No artifacts found in list');
@ -210,12 +209,12 @@
console.log(`Found ${Object.keys(artifactList).length} artifacts to collect`); console.log(`Found ${Object.keys(artifactList).length} artifacts to collect`);
// Step 6-8: Iterate through artifacts and collect content // Iterate through artifacts and collect content
for (const [filename, isSelected] of Object.entries(artifactList)) { for (const [filename, isSelected] of Object.entries(artifactList)) {
console.log(`Processing artifact: ${filename}`); console.log(`Processing artifact: ${filename}`);
try { try {
const content = await getArtifact(filename, isSelected); const content = await this.getArtifact(filename, isSelected);
artifactCollection[filename] = content; artifactCollection[filename] = content;
console.log(`✓ Collected: ${filename}`); console.log(`✓ Collected: ${filename}`);
} catch (error) { } catch (error) {
@ -226,13 +225,16 @@
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
} }
// Step 9: Log final collection // Log final collection
console.log('=== ARTIFACT COLLECTION COMPLETE ==='); console.log('=== ARTIFACT COLLECTION COMPLETE ===');
console.log(`Collected ${Object.keys(artifactCollection).length} artifacts:`); console.log(`Collected ${Object.keys(artifactCollection).length} artifacts:`);
console.log(artifactCollection); console.log(artifactCollection);
}
// Step 1: Content script is loaded, start collection return artifactCollection;
}
};
// Main function
async function main() { async function main() {
console.log('Running artifact collector'); console.log('Running artifact collector');
@ -240,7 +242,7 @@
return; return;
} }
await collectArtifacts(); await ArtifactScraper.collectArtifacts();
} }
// Run the main function // Run the main function