Last active
September 27, 2024 01:22
-
-
Save jonathands/03a449740ce72acbc68b8d1e6f6aa7ef to your computer and use it in GitHub Desktop.
extrair dados registro.br
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// === Global Variables === | |
var siteResults = {}; | |
var isPaused = false; | |
var pausePromiseResolve; | |
// === Function to Export siteResults as CSV === | |
function exportSiteResultsAsCSV(siteResults) { | |
// Create the CSV headers | |
var csvContent = 'Site,Domain Info,DNS Info,Error\n'; | |
// Loop through each site and add its data to the CSV | |
for (var site in siteResults) { | |
if (siteResults.hasOwnProperty(site)) { | |
var domainInfo = siteResults[site]["Domain Info"] || 'N/A'; | |
var dnsInfo = siteResults[site]["DNS Info"] || 'N/A'; | |
var error = siteResults[site]["Error"] || 'N/A'; | |
// Escape any quotes in the values | |
domainInfo = '"' + domainInfo.replace(/"/g, '""') + '"'; | |
dnsInfo = '"' + dnsInfo.replace(/"/g, '""') + '"'; | |
error = '"' + error.replace(/"/g, '""') + '"'; | |
csvContent += '"' + site.replace(/"/g, '""') + '",' + domainInfo + ',' + dnsInfo + ',' + error + '\n'; | |
} | |
} | |
// Create a Blob from the CSV content | |
var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); | |
// Create a download link and trigger it | |
var link = document.createElement('a'); | |
var url = URL.createObjectURL(blob); | |
link.setAttribute('href', url); | |
link.setAttribute('download', 'site_results.csv'); | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
// === Function to Create Floating Status Bar === | |
function createFloatingStatusBar(totalSites) { | |
// Create the container | |
var statusBar = document.createElement('div'); | |
statusBar.id = 'floating-status-bar'; | |
statusBar.style.position = 'fixed'; | |
statusBar.style.bottom = '0'; | |
statusBar.style.left = '0'; | |
statusBar.style.width = '100%'; | |
statusBar.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'; | |
statusBar.style.color = '#fff'; | |
statusBar.style.padding = '10px'; | |
statusBar.style.zIndex = '10000'; | |
statusBar.style.display = 'flex'; | |
statusBar.style.justifyContent = 'space-between'; | |
statusBar.style.alignItems = 'center'; | |
statusBar.style.fontFamily = 'Arial, sans-serif'; | |
// Status Message | |
var statusMessage = document.createElement('span'); | |
statusMessage.id = 'status-message'; | |
statusMessage.textContent = 'Initializing...'; | |
// Progress Info | |
var progressInfo = document.createElement('span'); | |
progressInfo.id = 'progress-info'; | |
progressInfo.textContent = '0 of ' + totalSites + ' processed (0%)'; | |
// Control Buttons | |
var controls = document.createElement('div'); | |
// Pause Button | |
var pauseButton = document.createElement('button'); | |
pauseButton.id = 'pause-button'; | |
pauseButton.textContent = '⏸️ Pause'; | |
pauseButton.style.marginRight = '10px'; | |
pauseButton.style.padding = '5px 10px'; | |
pauseButton.style.cursor = 'pointer'; | |
// Resume Button | |
var resumeButton = document.createElement('button'); | |
resumeButton.id = 'resume-button'; | |
resumeButton.textContent = '▶️ Resume'; | |
resumeButton.style.marginRight = '10px'; | |
resumeButton.style.padding = '5px 10px'; | |
resumeButton.style.cursor = 'pointer'; | |
resumeButton.style.display = 'none'; // Hidden initially | |
// Download Button | |
var downloadButton = document.createElement('button'); | |
downloadButton.id = 'download-button'; | |
downloadButton.textContent = '📥 Download CSV'; | |
downloadButton.style.padding = '5px 10px'; | |
downloadButton.style.cursor = 'pointer'; | |
downloadButton.style.display = 'none'; // Hidden initially | |
// Append buttons to controls | |
controls.appendChild(pauseButton); | |
controls.appendChild(resumeButton); | |
controls.appendChild(downloadButton); | |
// Append all elements to the status bar | |
statusBar.appendChild(statusMessage); | |
statusBar.appendChild(progressInfo); | |
statusBar.appendChild(controls); | |
// Append the status bar to the body | |
document.body.appendChild(statusBar); | |
// Event Listeners for Pause and Resume | |
pauseButton.addEventListener('click', function () { | |
isPaused = true; | |
pauseButton.style.display = 'none'; | |
resumeButton.style.display = 'inline-block'; | |
statusMessage.textContent = '⏸️ Paused'; | |
}); | |
resumeButton.addEventListener('click', function () { | |
isPaused = false; | |
if (pausePromiseResolve) { | |
pausePromiseResolve(); | |
} | |
pauseButton.style.display = 'inline-block'; | |
resumeButton.style.display = 'none'; | |
statusMessage.textContent = '▶️ Resuming...'; | |
}); | |
// Event Listener for Download Button | |
downloadButton.addEventListener('click', function () { | |
exportSiteResultsAsCSV(siteResults); | |
}); | |
// Return references to update later | |
return { | |
statusMessage: statusMessage, | |
progressInfo: progressInfo, | |
pauseButton: pauseButton, | |
resumeButton: resumeButton, | |
downloadButton: downloadButton | |
}; | |
} | |
// === Function to Pause Processing === | |
function pauseProcessing() { | |
return new Promise(function (resolve) { | |
pausePromiseResolve = resolve; | |
}); | |
} | |
// === Function to Reuse 'framoso' iframe and Load a New URL === | |
function loadInFramoso(url) { | |
return new Promise(function (resolve, reject) { | |
var framosoElement = document.getElementById('im-the-awesome-framoso'); | |
console.log('🌐 Loading ' + url + ' into framoso...'); | |
framosoElement.src = url; | |
framosoElement.onload = function () { | |
console.log('✅ Successfully loaded ' + url); | |
resolve(); | |
}; | |
framosoElement.onerror = function () { | |
console.log('❌ Failed to load ' + url); | |
reject('Failed to load: ' + url); | |
}; | |
}); | |
} | |
function waitForSelectorInFramoso(selector, retries, timeout) { | |
retries = retries || 5; | |
timeout = timeout || 2000; | |
return new Promise(function (resolve) { | |
var framosoElement = document.getElementById('im-the-awesome-framoso'); | |
var attemptCount = 0; | |
var checkForElement = function () { | |
if (isPaused) { | |
statusBarElements.statusMessage.textContent = '⏸️ Paused'; | |
pauseProcessing().then(function () { | |
statusBarElements.statusMessage.textContent = '▶️ Resuming...'; | |
checkForElement(); | |
}); | |
return; | |
} | |
try { | |
var contentDoc = framosoElement.contentDocument; | |
var element = contentDoc.querySelector(selector); | |
if (element) { | |
console.log('✅ Selector "' + selector + '" found!'); | |
resolve(element.innerHTML); // Return the content of the found element | |
} else if (attemptCount >= retries) { | |
console.log('⚠️ Selector "' + selector + '" not found after ' + retries + ' attempts.'); | |
resolve('not found'); // Return 'not found' if retries exceeded | |
} else { | |
attemptCount++; | |
console.log('🔄 Retrying (' + attemptCount + '/' + retries + ') for selector: "' + selector + '"...'); | |
setTimeout(checkForElement, timeout); // Retry after the timeout | |
} | |
} catch (error) { | |
console.log('❌ Error accessing iframe content or finding selector: ' + error); | |
resolve('not found'); // Return 'not found' if there's an error accessing iframe | |
} | |
}; | |
checkForElement(); // Start the checking loop | |
}); | |
} | |
// === Function to Process the Sites One by One === | |
async function processSitesSequentially(sites) { | |
var total = sites.length; | |
var processed = 0; | |
for (var i = 0; i < sites.length; i++) { | |
var site = sites[i]; | |
processed++; | |
statusBarElements.statusMessage.textContent = '🚀 Processing ' + site.text + '...'; | |
statusBarElements.progressInfo.textContent = processed + ' of ' + total + ' processed (' + Math.round((processed / total) * 100) + '%)'; | |
siteResults[site.text] = {}; // Initialize entry for the site | |
try { | |
await loadInFramoso(site.href); | |
var domainInfo = await waitForSelectorInFramoso("#conteudo > div.page > div > section.domain-info.box.box-gray > ul > li:nth-child(2) > span"); | |
siteResults[site.text]["Domain Info"] = domainInfo; | |
var dnsInfo = await waitForSelectorInFramoso("#conteudo > div.page > div > section.domain-dns.domain-box.box.box-green > ul"); | |
siteResults[site.text]["DNS Info"] = dnsInfo; | |
statusBarElements.statusMessage.textContent = '✅ Completed ' + site.text; | |
} catch (error) { | |
console.error('❌ Error:', error); | |
siteResults[site.text]["Error"] = error; // Log any errors encountered | |
statusBarElements.statusMessage.textContent = '❌ Error with ' + site.text; | |
} | |
} | |
// Processing Complete | |
statusBarElements.statusMessage.textContent = '📝 Process complete.'; | |
statusBarElements.downloadButton.style.display = 'inline-block'; | |
} | |
// === Setup Framoso iframe === | |
var framoso = document.createElement('iframe'); | |
framoso.style.width = '1000px'; | |
framoso.id = 'im-the-awesome-framoso'; | |
framoso.style.height = '1000px'; | |
framoso.style.top = '0'; | |
framoso.style.left = '0'; | |
framoso.style.border = 'none'; // Optional: Hide the border | |
framoso.style.position = 'absolute'; // Optional: Avoid affecting page layout | |
framoso.style.visibility = 'hidden'; // Keep hidden | |
document.body.appendChild(framoso); | |
var sites = Array.from(document.querySelectorAll("#domains > table > tbody > tr")).map(function (it) { | |
var a = it.querySelector("td a"); | |
return { href: a.href, text: a.text }; | |
}); | |
var statusBarElements = createFloatingStatusBar(sites.length); | |
processSitesSequentially(sites); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Só rodar logado na sua conta do registro.br