-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprebid.js
131 lines (105 loc) · 4.17 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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(55000);
// Set to Googlebot user agent
await page.setUserAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
const urls = readline.createInterface({
input: fs.createReadStream('input.txt')
});
try {
for await (const url of urls) {
const trimmedUrl = url.trim();
console.log(trimmedUrl)
/* page.on('request', request => {
console.log(request.url());
console.log(request.failure());
});
page.on('response', response => {
console.log(response.ok());
console.log(response.status());
console.log(response.statusText());
}); */
await page.goto(trimmedUrl, { timeout: 60000, 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(error);
} finally {
console.log('Results:', results);
try {
if (results.length == 0) {
await browser.close();
return null
}
// 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();