-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebid.js
110 lines (90 loc) · 3.6 KB
/
prebid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as fs from 'fs';
import * as readline from 'readline';
import puppeteer from 'puppeteer-extra';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
async function prebidExplorer() {
const browser = await puppeteer
.use(StealthPlugin())
.launch({
protocolTimeout: 300000,
defaultViewport: null,
headless: true,
});
let results = [];
const page = await browser.newPage();
page.setDefaultTimeout(65000);
const urls = readline.createInterface({
input: fs.createReadStream('input.txt')
});
try {
for await (const url of urls) {
const trimmedUrl = url.trim();
console.log(`Processing URL: ${trimmedUrl}`);
await page.goto(trimmedUrl, { timeout: 70000, waitUntil: 'networkidle2' });
// Slight delay to ensure the page is fully loaded
await page.evaluate(async () => {
const sleep = ms => new Promise(res => setTimeout(res, ms));
await sleep((1000 * 60) * .10);
})
// Collect data from the page
const pageData = await page.evaluate(() => {
const data = {};
// Initialize libraries array
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;
});
// Add the input URL to the pageData
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) {
console.error('An error occurred:', error);
} finally {
console.log('Results:', results);
try {
// 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
fs.appendFileSync('output/results.json', jsonOutput, 'utf8');
console.log('Results have been saved to output/results.json');
} catch (err) {
console.error('Failed to write results:', err);
}
if (browser) {
await browser.close();
}
}
}
prebidExplorer();