newspaint

Documenting Problems That Were Difficult To Find The Answer To

Adblock for PhantomJS

Starting from version 1.9 of PhantomJS there exists the ability to abort a request for a URL.

The below code is an example of how to do this (blocking by site name):

// extract domain name from a URL
function sitename( url ) {
    var result = /^https?:\/\/([^\/]+)/.exec( url );
    if ( result ) {
        return( result[1] );
    } else {
        return( null );
    }
}

// add a callback to every request performed on a webpage
function adblock( page ) {
    page.onResourceRequested = function ( requestData, networkRequest ) {
        // pull out site name from URL
        var site = sitename( requestData.url );
        if ( ! site )
            return;

        // abort requests for particular domains
        if (
            ( /\.doubleclick\./.test( site ) ) ||
            ( /\.pubmatic\.com$/.test( site ) )
        ) {
            console.error( "  - BLOCKED URL from " + site );
            networkRequest.abort();
            return;
        }
    };
}

var page = require('webpage').create();
adblock( page );

If, for example, you wanted to prevent images from being loaded, you could define that adblock() to be:

function adblock( page ) {
    var regexpImg = new RegExp( '\.(jpe?g|png|gif|svg)(\?.*)?$', 'i' );

    page.onResourceRequested = function ( requestData, networkRequest ) {
        if ( regexpImg.test( requestData.url ) ) {
            console.error( "  - BLOCKED URL: " + requestData.url );
            networkRequest.abort();
            return;
        }
    };
}

2 responses to “Adblock for PhantomJS

  1. Cheney Jin March 15, 2014 at 11:06 am

    TypeError: ‘undefined’ is not an object (evaluating ‘networkRequest.abort’)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: