Update prebid.js: support other globals

merge #5 first
This commit is contained in:
Patrick McCann 2024-10-03 13:42:32 -04:00 committed by GitHub
parent 340a246816
commit 0ff29b71f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,46 +22,80 @@ async function prebidExplorer() {
}); });
try { try {
for await (const url of urls) { for await (const url of urls) {
console.log(`Line from file: ${url}`); const trimmedUrl = url.trim();
console.log(`Processing URL: ${trimmedUrl}`);
await page.goto(url.trim(), { timeout: 70000, waitUntil: 'networkidle2' });
await page.evaluate(async () => {
const sleep = ms => new Promise(res => setTimeout(res, ms));
await sleep((1000 * 60) * 0.11); // Slight delay to ensure page is loaded
});
const hasPrebid = await page.evaluate(() => { await page.goto(trimmedUrl, { timeout: 70000, waitUntil: 'networkidle2' });
return window._pbjsGlobals ? true : false;
});
const prebidObj = await page.evaluate(() => { // Slight delay to ensure the page is fully loaded
if (window._pbjsGlobals && window._pbjsGlobals.includes('pbjs')) { await page.waitForTimeout(7000);
return {
url: location.href, // Collect data from the page
version: pbjs.version, const pageData = await page.evaluate(() => {
modules: pbjs.installedModules const data = {};
};
} else { // Initialize libraries array
return null; data.libraries = [];
// Check for apstag
if (window.apstag) {
data.libraries.push('apstag');
} }
// Check for googletag
if (window.googletag) {
data.libraries.push('googletag');
}
// Check for ats
if (window.ats) {
data.libraries.push('ats');
}
// Check for Prebid.js instances
if (window._pbjsGlobals && Array.isArray(window._pbjsGlobals)) {
data.prebidInstances = [];
window._pbjsGlobals.forEach(function(globalVarName) {
const pbjsInstance = window[globalVarName];
if (pbjsInstance && pbjsInstance.version && pbjsInstance.installedModules) {
data.prebidInstances.push({
globalVarName: globalVarName,
version: pbjsInstance.version,
modules: pbjsInstance.installedModules
});
}
});
}
return data;
}); });
if (prebidObj != null) { // Add the input URL to the pageData
results.push(prebidObj); pageData.url = trimmedUrl;
// Only push data if any libraries are found or Prebid.js is present
if (pageData.libraries.length > 0 || (pageData.prebidInstances && pageData.prebidInstances.length > 0)) {
results.push(pageData);
} }
} }
} catch (error) { } catch (error) {
console.error(error); console.error('An error occurred:', error);
throw new Error(error);
} finally { } finally {
console.log(results); console.log('Results:', results);
try { try {
// Write results as valid JSON array // Ensure the output directory exists
if (!fs.existsSync('output')) {
fs.mkdirSync('output');
}
// Write results as a JSON array
const jsonOutput = JSON.stringify(results, null, 2); // Pretty print with 2 spaces const jsonOutput = JSON.stringify(results, null, 2); // Pretty print with 2 spaces
fs.writeFileSync('output/results.json', jsonOutput, 'utf8'); fs.writeFileSync('output/results.json', jsonOutput, 'utf8');
console.log('Results have been saved to output/results.json');
} catch (err) { } catch (err) {
console.error(err); console.error('Failed to write results:', err);
} }
if (browser) { if (browser) {