Untitled
unknown
plain_text
9 months ago
4.4 kB
4
Indexable
// Utility: Wait a moment
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Utility: Wait for an element to appear
async function waitForElement(selector, timeout = 10000) {
const start = Date.now();
while (Date.now() - start < timeout) {
const element = document.querySelector(selector);
if (element) return element;
await delay(500);
}
throw new Error(`Element not found: ${selector}`);
}
// Function to select an element using keyboard events
async function selectElement(element) {
try {
// Bring focus to the element
element.focus();
// Simulate pressing the Down Arrow key
const downArrowEvent = new KeyboardEvent('keydown', {
key: 'ArrowDown',
code: 'ArrowDown',
keyCode: 40,
bubbles: true
});
element.dispatchEvent(downArrowEvent);
// Simulate pressing the Spacebar
const spacebarEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
keyCode: 32,
bubbles: true
});
element.dispatchEvent(spacebarEvent);
// Wait for the UI to update
await delay(1000);
return document.getElementById('show-user-detail-expirationDate') !== null;
} catch (error) {
console.error('Error selecting element with keyboard:', 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();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
}
// Main function
async function getExpirationDates() {
console.log('Waiting for user list...');
const userList = await waitForElement('[role="option"]');
const users = document.querySelectorAll('[role="option"]');
console.log(`Found ${users.length} users`);
const data = [];
const batchSize = 50; // Process users in batches of 50
const delayBetweenBatches = 10000; // 5 seconds delay between batches
for (let i = 0; i < users.length; i += batchSize) {
const batch = Array.from(users).slice(i, i + batchSize);
console.log(`Processing batch ${i / batchSize + 1}/${Math.ceil(users.length / batchSize)}`);
for (let j = 0; j < batch.length; j++) {
const user = batch[j];
const name = user.querySelector('dd') ? user.querySelector('dd').textContent.trim() : `User ${i + j + 1}`;
console.log(`Processing user ${i + j + 1}/${users.length}: ${name}`);
try {
const selected = await selectElement(user);
if (!selected) {
console.log(`Failed to select user: ${name}`);
continue;
}
await delay(2000); // Wait for UI to update
const expirationDate = document.getElementById('show-user-detail-expirationDate')
? document.getElementById('show-user-detail-expirationDate').textContent.trim()
: 'Not found';
data.push({ name, expiration: expirationDate });
console.log(`${name}: ${expirationDate}`);
await delay(1000); // Wait before next user
} catch (error) {
console.error(`Error processing user ${i + j + 1}:`, error);
}
}
if (i + batchSize < users.length) {
console.log(`Waiting ${delayBetweenBatches / 1000} seconds before next batch...`);
await delay(delayBetweenBatches);
}
}
const csv = ['Name,Expiration Date'];
data.forEach(row => csv.push(`"${row.name}","${row.expiration}"`));
console.log('Complete data:', csv.join('\n'));
downloadCSV(csv.join('\n'));
}
// Run the script after ensuring the page is fully loaded
(async () => {
try {
await getExpirationDates();
console.log('Script completed!');
} catch (err) {
console.error('Error:', err);
}
})();Editor is loading...
Leave a Comment