/**
 * Aborts requests to external resources or given domains
 *
 * Does not emit any metrics
 */
"use strict";

module.exports = function (phantomas) {
  var ourDomain,
    // --no-externals
    noExternalsMode = phantomas.getParam("no-externals") === true,
    // --allow-domain .fastly.net,.googleapis.com
    allowedDomains = phantomas.getParam("allow-domain"),
    allowedDomainsRegExp,
    // --block-domain google-analytics.com
    blockedDomains = phantomas.getParam("block-domain"),
    blockedDomainsRegExp;

  ourDomain = new URL(phantomas.getParam("url")).hostname;

  phantomas.setMetric("blockedRequests"); // @desc number of requests blocked due to domain filtering @optional

  function checkBlock(domain) {
    var blocked = false;

    // --no-externals
    if (noExternalsMode && ourDomain !== false && domain !== ourDomain) {
      blocked = true;
    }

    // match blacklist (--block-domain)
    if (blockedDomainsRegExp && blockedDomainsRegExp.test(domain)) {
      blocked = true;
    }

    // match whitelist (--allow-domain)
    if (allowedDomainsRegExp) {
      if (allowedDomainsRegExp.test(domain) || domain === ourDomain) {
        blocked = false;
      } else {
        blocked = true;
      }
    }

    return blocked;
  }

  // parse settings
  function parseParameter(value) {
    return value.split(",").map(function (item) {
      return item.trim();
    });
  }

  allowedDomains =
    typeof allowedDomains === "string" ? parseParameter(allowedDomains) : false;
  blockedDomains =
    typeof blockedDomains === "string" ? parseParameter(blockedDomains) : false;

  if (noExternalsMode) {
    phantomas.log(
      'Block domains: working in --no-externals mode ("%s" is our domain)',
      ourDomain
    );
  }

  if (allowedDomains !== false) {
    phantomas.log("Block domains: whitelist - " + allowedDomains.join(", "));
    allowedDomainsRegExp = new RegExp("(" + allowedDomains.join("|") + ")$");
  }

  if (blockedDomains !== false) {
    phantomas.log("Block domains: blacklist - " + blockedDomains.join(", "));
    blockedDomainsRegExp = new RegExp("(" + blockedDomains.join("|") + ")$");
  }

  // https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pagesetrequestinterceptionvalue
  phantomas.on("init", async (page) => {
    await page.setRequestInterception(true);

    page.on("request", (interceptedRequest) => {
      const url = interceptedRequest.url(),
        domain = new URL(url).hostname;

      if (checkBlock(domain)) {
        interceptedRequest.abort();

        phantomas.log("Request has been blocked: <%s>", url);

        // stats
        phantomas.incrMetric("blockedRequests");
        phantomas.addOffender("blockedRequests", url);
      } else {
        interceptedRequest.continue();
      }
    });

    phantomas.log("Requests intercepting enabled");
  });
};

@@ -2132,24 +2132,107 @@
 %22)$%22);%0A  %7D%0A%0A
+  if (noExternalsMode %7C%7C allowedDomains !== false %7C%7C blockedDomains !== false) %7B%0A  
   // https:/
@@ -2325,16 +2325,18 @@
 value%0A  
+ 
+ 
 phantoma
@@ -2362,24 +2362,26 @@
 (page) =%3E %7B%0A
+  
     await pa
@@ -2418,16 +2418,18 @@
 );%0A%0A    
+ 
+ 
 page.on(
@@ -2461,24 +2461,26 @@
 quest) =%3E %7B%0A
+  
       const 
@@ -2519,16 +2519,18 @@
         
+  
 domain =
@@ -2550,24 +2550,26 @@
 .hostname;%0A%0A
+  
       if (ch
@@ -2588,32 +2588,34 @@
 ain)) %7B%0A        
+  
 interceptedReque
@@ -2623,24 +2623,26 @@
 t.abort();%0A%0A
+  
         phan
@@ -2700,16 +2700,18 @@
         
+  
 // stats
@@ -2711,16 +2711,18 @@
 / stats%0A
+  
         
@@ -2762,32 +2762,34 @@
 ests%22);%0A        
+  
 phantomas.addOff
@@ -2817,32 +2817,34 @@
 s%22, url);%0A      
+ 
+ 
 %7D else %7B%0A       
@@ -2828,32 +2828,34 @@
        %7D else %7B%0A
+  
         intercep
@@ -2879,26 +2879,30 @@
 ue();%0A      
-%7D%0A
+  %7D%0A  
     %7D);%0A%0A   
@@ -2898,24 +2898,26 @@
    %7D);%0A%0A    
+ 
+ 
 phantomas.lo
@@ -2952,17 +2952,23 @@
 bled%22);%0A
+  
   %7D);%0A
+  %7D%0A
 %7D;%0A

Fix browser adding pragma: no-cache to all requests