Last active
August 6, 2018 00:07
-
-
Save SmithKevin/e0e3e3317bdbddae5ca08960e250182f to your computer and use it in GitHub Desktop.
Finding a Quantcast tag using Puppeteer
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
const puppeteer = require('puppeteer'); | |
const browserPromise = puppeteer.launch( | |
{timeout:60000, args:['--aggressive-cache-discard','--disable-cache', '--disable-application-cache']} | |
); | |
browserPromise.then(browser => { | |
return browser.version() | |
}).then(ver => console.log("Browser version " + ver)); | |
module.exports.open = function(url){ | |
return new Promise(function (resolve, reject) { | |
browserPromise.then( browser => { | |
return browser.newPage(); | |
}).then(async page => { | |
let tags = []; | |
page.on('requestfinished', request => { | |
let resp = request.response(); | |
if (resp && resp.url.indexOf('pixel.quantserve.com/pixel') > -1) { | |
tags.push({tag: resp.url, code: resp.status}); | |
} | |
}); | |
// images are expensive to download and slow to load. Skip them (except the QC pixel image of course) | |
page.on('request', request => { | |
if( request ) { | |
if (request.resourceType === 'image' && request.url.indexOf('pixel.quantserve.com/pixel') === -1) { | |
request.abort(); | |
} else { | |
request.continue(); | |
} | |
} | |
}); | |
let result; | |
try { | |
await page.setRequestInterception(true); | |
const resp = await page.goto(url, {waitUntil: ['load', 'networkidle2']}); | |
result = {tags: tags, code: resp.status}; | |
} catch (err) { | |
// tags ended up loading, but page technically did not load successfully | |
if(tags.length > 0){ | |
result = {tags: tags, code: -1}; | |
}else { | |
//its an navigation error, but still a successful scan | |
result = {error: err.message}; | |
} | |
} | |
//cleanup the page | |
await page.close(); | |
resolve(result); | |
}).catch(err => { | |
//this however is always bad | |
reject({error: err}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment