Untitled
unknown
plain_text
9 months ago
3.9 kB
3
Indexable
// Utility: Wait a moment
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Utility: Wait for an element to appear with timeout
async function waitForElement(selector, timeout = 10000) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const element = document.querySelector(selector);
if (element) return element;
await delay(200);
}
throw new Error(`Timeout: Element not found (${selector})`);
}
// Function to simulate keypress events (Down Arrow & Space)
function triggerKeyPress(element, key) {
const keyEventOptions = { key, code: key, keyCode: key === 'ArrowDown' ? 40 : 32, bubbles: true };
element.dispatchEvent(new KeyboardEvent('keydown', keyEventOptions));
element.dispatchEvent(new KeyboardEvent('keyup', keyEventOptions));
}
// Function to select an element using Down Arrow and Spacebar
async function selectElement(user) {
try {
user.focus(); // Ensure focus
// Simulate pressing Down Arrow
triggerKeyPress(user, 'ArrowDown');
await delay(100); // Short delay
// Simulate pressing Spacebar
triggerKeyPress(user, ' ');
await delay(500); // Allow UI to update
// Verify if expiration date is now visible
return document.querySelector('#show-user-detail-expirationDate') !== null;
} catch (error) {
console.error('Error selecting element:', error);
return false;
}
}
// Function to download CSV
function downloadCSV(data) {
const blob = new Blob([data], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'user_expirations.csv';
document.body.appendChild(a);
a.click();
// Cleanup
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Main function to retrieve expiration dates
async function getExpirationDates() {
console.log('Waiting for user list...');
// Wait for user list to load
await waitForElement('[role="option"]');
const users = Array.from(document.querySelectorAll('[role="option"]'));
console.log(`Found ${users.length} users`);
const data = [];
for (const [index, user] of users.entries()) {
try {
// Extract the user's name
const nameElement = user.querySelector('dd');
const name = nameElement ? nameElement.textContent.trim() : `User ${index + 1}`;
console.log(`Processing user ${index + 1}/${users.length}: ${name}`);
// Select the user using Down Arrow + Space
const selected = await selectElement(user);
if (!selected) {
console.warn(`Skipping user (selection failed): ${name}`);
continue;
}
// Wait for expiration date element
const expirationElement = await waitForElement('#show-user-detail-expirationDate', 5000);
const expirationDate = expirationElement.textContent.trim();
// Store the result
data.push({ name, expiration: expirationDate });
console.log(`${name}: ${expirationDate}`);
// Small delay to prevent UI overload
await delay(250);
} catch (error) {
console.error(`Error processing user ${index + 1}:`, error);
}
}
// Convert to CSV format
const csv = ['Name,Expiration Date', ...data.map(row => `"${row.name}","${row.expiration}"`)].join('\n');
console.log('CSV Data Ready');
downloadCSV(csv);
}
// Run the script
(async () => {
try {
await getExpirationDates();
console.log('Script completed successfully!');
} catch (err) {
console.error('Script failed:', err);
}
})();
Editor is loading...
Leave a Comment