From e43d9b99877a506779142d22e6716b531fa51ace Mon Sep 17 00:00:00 2001 From: Lord_Devi Date: Thu, 21 Nov 2024 08:26:45 -0500 Subject: [PATCH] Fixing GKWP a bit more. --- content-script.js | 64 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/content-script.js b/content-script.js index 49fc7d9..91e3ba1 100644 --- a/content-script.js +++ b/content-script.js @@ -483,6 +483,36 @@ return false; } + // Helper function to check if pagination is needed + function shouldHandlePagination(options) { + try { + // First check if user wants all pages + if (!options.extractAllPages) { + console.log('Skipping pagination: extractAllPages is false'); + return false; + } + + // Get current page info + const pageInfo = getCurrentPageInfo(); + if (!pageInfo) { + console.log('Skipping pagination: could not get page info'); + return false; + } + + // If total is less than or equal to 100, no need for pagination + if (pageInfo.total <= 100) { + console.log(`Skipping pagination: only ${pageInfo.total} total items`); + return false; + } + + console.log(`Pagination needed: ${pageInfo.total} total items`); + return true; + } catch (error) { + console.error('Error checking pagination:', error); + return false; + } + } + // Since this is an Angular app, content might load dynamically // Let's try to wait for the content to load function waitForContent() { @@ -519,26 +549,32 @@ // Main function to extract keywords async function extractKeywords() { try { - // First set the rows to 500 if possible - const rowsSet = await setShowRows500(); - if (!rowsSet) { - console.log('Warning: Could not set rows to 500'); - } - - // Add a delay and wait for pagination to update - await new Promise(resolve => setTimeout(resolve, 2000)); - await waitForPaginationUpdate(); - - // Ensure content is fully loaded - await ensureContentVisible(); - await loadAllRows(); - // Get the options passed from the popup const options = window.keywordGrabberOptions || { includeHeaders: true, extractAllPages: true }; // Create a Map to store unique keywords with their volumes and sources const processedKeywords = new Map(); // Using Map to store keyword -> { volume, source } pairs + // Check if we need pagination + const needsPagination = shouldHandlePagination(options); + + // Only try to set rows to 500 if we need pagination + if (needsPagination) { + // First set the rows to 500 if possible + const rowsSet = await setShowRows500(); + if (!rowsSet) { + console.log('Warning: Could not set rows to 500'); + } + + // Add a delay and wait for pagination to update + await new Promise(resolve => setTimeout(resolve, 2000)); + await waitForPaginationUpdate(); + } + + // Ensure content is fully loaded + await ensureContentVisible(); + await loadAllRows(); + // Get current page info const pageInfo = getCurrentPageInfo(); if (!pageInfo) {