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