iam working google chroom extension using Html And Javascript. my extension have only two Files Popup.html and popup.js…. iam trying to load links by csv one by one when user click search its load next row and alert first name and last name and wait for user to reclick on search button…but there are some issue in this code its working but when i click other windows or tab instead popup.html and then again click on next its not wkork and lost all loaded csv data i want that csv loaded data remain until user click on clear-Data button.
here popup.js
$(document).ready(function() {
var csvData;
var currentLine = 1;
var headers;
var firstNameIndex;
var lastNameIndex;
var streetAddressIndex;
var cityIndex;
var stateIndex;
var tabId;
// Refactored file upload and parsing into a separate function
function parseCsvFile(file) {
Papa.parse(file, {
complete: function(results) {
csvData = results.data;
headers = csvData[0];
firstNameIndex = headers.indexOf('First Name');
lastNameIndex = headers.indexOf('Last Name');
streetAddressIndex = headers.indexOf('streetaddress');
cityIndex = headers.indexOf('City');
stateIndex = headers.indexOf('State');
if (firstNameIndex === -1 || lastNameIndex === -1 || streetAddressIndex === -1 || cityIndex === -1 || stateIndex === -1) {
alert('Could not find all required columns in the CSV file.');
$('#csv-file').val(''); // clear the file input field
return;
}
alert('CSV file uploaded and parsed successfully.');
},
error: function() {
alert('Failed to upload or parse the CSV file.');
}
});
}
// Handler for file input change event
$('#csv-file').on('change', function() {
var file = $('#csv-file')[0].files[0];
if (!file) {
alert('Please select a CSV file.');
return;
}
parseCsvFile(file);
});
// Define variables to keep track of the current state
var csvData = null;
var currentLine = 1;
var tabId = null;
// Function to perform a search
function performSearch() {
if (!csvData) {
alert('Please upload a CSV file first.');
return;
}
// Get the data row for the current line
var data = csvData[currentLine];
var firstName = data[firstNameIndex];
var lastName = data[lastNameIndex];
var streetAddress = data[streetAddressIndex];
var city = data[cityIndex];
var state = data[stateIndex];
// Construct the search URL using the street address, city, and state
var url = 'https://www.truepeoplesearch.com/resultaddress?streetaddress=' + encodeURIComponent(streetAddress) + '&citystatezip=' + encodeURIComponent(city) + ',' + encodeURIComponent(state);
// Load the search page in the current tab
if (tabId) {
chrome.tabs.update(tabId, { url: url });
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
tabId = tabs[0].id;
chrome.tabs.update(tabId, { url: url });
});
}
// Wait for the page to load
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) {
if (tabId === tab.id && changeInfo.status === 'complete') {
// Display the First Name And last name in alert on the active tab
chrome.tabs.executeScript(tabId, { code: 'alert("First Name: ' + firstName + '\nLast Name: ' + lastName + '");' });
// Remove the listener so it doesn't run again for other tabs
chrome.tabs.onUpdated.removeListener(listener);
}
});
}
// Function to move to the next line
function moveToNextLine() {
// Check if csvData exists and there are more lines
if (csvData && currentLine < csvData.length - 1) {
// Increment the current line
currentLine++;
// Perform the search for the current line
performSearch();
}
}
// Attach event listeners to the search and next buttons
$('#search-btn').on('click', function() {
currentLine = 1;
performSearch();
});
$('#next-btn').on('click', moveToNextLine);
// Listen for tab changes and save the csvData
chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) {
chrome.storage.local.set({ 'csvData': csvData });
});
});
// Listen for changes in the storage and update the csvData
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (var key in changes) {
if (key === 'csvData') {
csvData = changes[key].newValue;
}
}
});
});
i tried mannytime bbut its lost always
