robinnorth / google-migration-utils

CLI utilities to assist with migrating Google Account data where no Google-provided solution exists
33 stars 5 forks source link

Proposal to make script more stable #14

Open usaerc opened 2 weeks ago

usaerc commented 2 weeks ago

I found delay a bit short: await window.delay(1000); //await window.delay(150);

Other fix for pages sometimes not firing load event // Added to prevent stopping async function gotoWithRetry(page, url, options, retries = 5, timeout = 10000) { let attempts = 0; while (attempts < retries) { try { // Start navigation const response = await Promise.race([ page.goto(url, options), // Attempt to go to the URL new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeout)) // Timeout promise ]);

        // Check if the response is valid
        if (response && response.ok()) {
            //console.log('Page loaded successfully!');
            return; // Successfully loaded the page
        } else {
            throw new Error('Failed to load the page.');
        }
    } catch (error) {
        attempts++;
        console.error(`Attempt ${attempts} failed: ${error.message}`);
        if (attempts === retries) {
            throw new Error(`Failed to load the page after ${retries} attempts.`);
        }
        console.log('Retrying...');
    }
}

}

replace

await gotoWithRetry(page, url, { waitUntil: 'load' }); //await page.goto(url, { waitUntil: "load" });