Last active
April 2, 2023 17:37
-
-
Save valoricDe/1ef621a38b2557d63f3c22b108b7f044 to your computer and use it in GitHub Desktop.
fraabe
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
async function getHtmlDocumentById(id) { | |
const baseUrl = 'http://portal.fotoraabe.de' | |
const response = await fetch(`${baseUrl}/EditCustomer?id=${id}`) | |
if (response.status >= 400) throw Error(await response.text()) | |
const html = await response.text() | |
const { | |
window: { document }, | |
} = new JSDOM(html) | |
return document | |
} | |
function JSDOM(html) { | |
document.documentElement.innerHTML = html | |
return { window: { document: window.document }}; | |
} | |
function getInputValue(document, selector) { | |
const el = document.querySelector(selector) | |
return el && el.value || null | |
} | |
function getSelectOptionLabel(document, selector) { | |
const el = document.querySelector(selector) | |
return el && el.selectedOptions[0].label || null | |
} | |
function extractData(document) { | |
return { | |
Name: getInputValue(document, '[name=Name]'), | |
Art: getSelectOptionLabel(document, '[name=CustomerType]'), | |
Groesse: getInputValue(document, '[name=Dimension]'), | |
Nation: getInputValue(document, '[name=Nation]'), | |
Bundesland: getInputValue(document, '[name=State]'), | |
Strasse: getInputValue(document, '[name=Street]'), | |
PLZ: getInputValue(document, '[name=ZipCode]'), | |
Stadt: getInputValue(document, '[name=City]'), | |
Stadtteil: getInputValue(document, '[name=District]'), | |
Ansprechpartner: getInputValue(document, '[name=MainContactName]'), | |
AnsprechpartnerAnrede: getSelectOptionLabel(document, '[name=MainContactSalutation]'), | |
Leitung: getInputValue(document, '[name=DirectorName]'), | |
LeitungAnrede: getSelectOptionLabel(document, '[name=DirectorSalutation]'), | |
StellvLeitung: getInputValue(document, '[name=DevDirectorName]'), | |
StellvLeitungAnrede: getSelectOptionLabel(document, '[name=DevDirectorSalutation]'), | |
Vorwahl: getInputValue(document, '[name=DialingCode]'), | |
Telefon: getInputValue(document, '[name=Phonenumber]'), | |
Email: getInputValue(document, '[name=EmailAddress]'), | |
Website: getInputValue(document, '[name=Website]'), | |
} | |
} | |
const waitMs = ms => new Promise(res => setTimeout(res, ms)) | |
window.collection = [] | |
async function exportDatabase(max = 50, offset = 0) { | |
for(let id = offset; id < offset+max; id++) { | |
try { | |
const doc = await getHtmlDocumentById(id) | |
const data = extractData(doc) | |
window.collection.push(data) | |
await waitMs(100) | |
} catch(e) { console.warn(e) } | |
} | |
document.documentElement.innerHTML = JSON.stringify(window.collection) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment