|
const { join } = require('path'); |
|
const { readFileSync, writeFileSync } = require('fs'); |
|
|
|
// ========== |
|
// Constants. |
|
// ========== |
|
|
|
const FILE_NAME_TEXT = 'domains.txt'; |
|
const FILE_NAME_TRACE = 'trace.har'; |
|
|
|
// ======== |
|
// Options. |
|
// ======== |
|
|
|
const options = { |
|
encoding: 'utf8', |
|
}; |
|
|
|
// =============== |
|
// Get file paths. |
|
// =============== |
|
|
|
const pathForFileInput = join(__dirname, FILE_NAME_TRACE); |
|
const pathForFileOutput = join(__dirname, FILE_NAME_TEXT); |
|
|
|
// =============== |
|
// Get input text. |
|
// =============== |
|
|
|
const textForFileInput = readFileSync(pathForFileInput, options); |
|
const jsonForFileInput = JSON.parse(textForFileInput); |
|
|
|
// =================== |
|
// Helper: add domain. |
|
// =================== |
|
|
|
const domainSet = new Set(); |
|
|
|
const addDomain = (value = '') => { |
|
// Get domain. |
|
let domain = value.split('://')[1] || ''; |
|
domain = domain.split('/')[0] || ''; |
|
|
|
// Domain exists? |
|
if (domain) { |
|
// Add to set. |
|
domainSet.add(domain); |
|
} |
|
}; |
|
|
|
// ================================================ |
|
// Helper: recursively parse `*.parent.callFrames`. |
|
// ================================================ |
|
|
|
const parseCallFrames = (obj = {}) => { |
|
// Loop through call frames. |
|
obj.callFrames?.forEach(({ url }) => { |
|
// Add domains. |
|
addDomain(url); |
|
}); |
|
|
|
// Parent exists? |
|
if (obj.parent) { |
|
// Recursion. |
|
parseCallFrames(obj.parent) |
|
} |
|
}; |
|
|
|
// ===================== |
|
// Loop through entries. |
|
// ===================== |
|
|
|
jsonForFileInput.log.entries.forEach((entry) => { |
|
// ======================== |
|
// Loop through initiators. |
|
// ======================== |
|
|
|
// Add domains. |
|
addDomain(entry._initiator.url); |
|
addDomain(entry.request.url); |
|
|
|
// Parse call frames. |
|
parseCallFrames(entry._initiator.stack); |
|
}); |
|
|
|
// ================ |
|
// Get output text. |
|
// ================ |
|
|
|
const domainArray = Array.from(domainSet).sort(); |
|
const textForFileOutput = domainArray.join('\n'); |
|
|
|
// ============== |
|
// Write to file. |
|
// ============== |
|
|
|
writeFileSync(pathForFileOutput, textForFileOutput, options); |
|
|
|
// =============== |
|
// Log completion. |
|
// =============== |
|
|
|
global.console.clear(); |
|
global.console.log(`Parsing complete. Domains written to file:\n\n${pathForFileOutput}\n`); |