繁体   English   中英

如何使用木偶,无头铬阻止广告

[英]how to block ads with puppeteer, headless chrome

我正在寻找在node.js中使用伪造者(无头铬)时阻止广告的任何方式,并且我在https://groups.google.com/a/chromium.org/forum/#!msg/headless中找到了这种方法-dev / G1u6SGeq7nw / VaIcIPlCAQAJ ;

 //http://winhelp2002.mvps.org/hosts.txt //For puppeteer I read in this host file: //now we read the host file var hostFile = fs.readFileSync('hosts.txt', 'utf8').split('\\n'); var hosts = {}; for (var i = 0; i < hostFile.length; i++) { var frags = hostFile[i].split(' '); if (frags.length > 1 && frags[0] === '0.0.0.0') { hosts[frags[1].trim()] = true; } } //When loading a page I then filter out requests for these domains (and optionally images): page.on('request', request => { var domain = null; if (task.input.blockads) { var frags = request.url().split('/'); if (frags.length > 2) { domain = frags[2]; } } if ((task.input.blockads && hosts[domain] === true) || (!task.input.includephotos && request.resourceType() === 'image')) { request.abort(); } else { request.continue(); } }); //This solution hugely improved the speed of our scraper. 

但是我不知道这部分。 task.input.blockads这什么都不是,我一无所知。 任何想法?

这只是启用/禁用检查主机文件的参数。 如果您一直想要检查,则忽略该部分

page.on('request', request => {

    var domain = null;
    var frags = request.url().split('/');
    if (frags.length > 2) {
       domain = frags[2];
    }

    // just abort if found
    if (hosts[domain] === true) {
        request.abort();
    } else {
        request.continue();
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM