How paywalled sites permit access to visitors from social media sites and apps
Some code review just in case anyone is interested. (I don’t expect it to make it into the article as it was written six months ago.)<p>This pattern:<p><pre><code>  if (details.url.includes(url)) {
    return true;
  }
  return false;
</code></pre>
should be replaced by this:<p><pre><code>  return details.url.includes(url);
</code></pre>
This pattern:<p><pre><code>  array.map(someFunction).reduce(function(a, b) { return a ||b}, false)
</code></pre>
should be replaced by this:<p><pre><code>  array.some(someFunction)
</code></pre>
(Note the semantics are slightly different—`.some` will break early, so it’s more efficient and equivalent provided there are no side-effects in the map function.)<p>Taking both of these, the following:<p><pre><code>  var useTwitter = VIA_TWITTER.map(function(url) {
    if (details.url.includes(url)) {
      return true;
    }
    return false;
  })
  .reduce(function(a, b) { return a || b}, false);
</code></pre>
can be rewritten much more simply as:<p><pre><code>  var useTwitter = VIA_TWITTER.some(function(url) {
    return details.url.includes(url);
  }
</code></pre>
You could even do it thus if you desired:<p><pre><code>  var useTwitter = VIA_TWITTER.some(details.url.includes.bind(details.url));
</code></pre>
… but that’s probably harder to read. I will mention arrow functions, however, which are pretty:<p><pre><code>  var useTwitter = VIA_TWITTER.some(url =&gt; details.url.includes(url));
</code></pre>
This part:<p><pre><code>  details.requestHeaders.filter(function(header) {

    &#x2F;&#x2F; block cookies by default
    if (header.name !== &quot;Cookie&quot;) {
      return header;
    } 

  })
</code></pre>
`.filter` only cares about truthiness in its return value—as this code does it, undefined is false and an object is true. But you could simplify it:<p><pre><code>  details.requestHeaders.filter(function(header) {
    &#x2F;&#x2F; block cookies by default
    return header.name !== &quot;Cookie&quot;;
  })
</code></pre>
Also in the original code’s usage of map, it’s not actually changing the values, only things inside them, so using `map` is wasteful (as it entails allocating a new array). You could just use `forEach`:<p><pre><code>  var reqHeaders = …;
  reqHeaders.forEach(function(header) {
    if (header.name === &quot;Referer&quot;) {
      header.value = setRefer(useTwitter);
      foundReferer = true;
    }
    if (header.name === &quot;User-Agent&quot;) {
      header.value = setUserAgent(useTwitter);
      foundUA = true;
    }
  });
</code></pre>
A remark on fine-tuning performance: when you access properties inside an object multiple times, it’s optimal to store it as a local variable to save having to look it up multiple times. (This is especially the case if the property is expensive to access.) Take the `blockCookies` method:<p><pre><code>  function blockCookies(details) {
    for (var i = 0; i &lt; details.responseHeaders.length; ++i) {
      if (details.responseHeaders[i].name === &quot;Set-Cookie&quot;) {
        details.responseHeaders.splice(i, 1);
      }
    }
    return {responseHeaders: details.responseHeaders};
  }
</code></pre>
This is accessing `details.responseHeaders` many times when it only needs to access it once. It is also accessing its `length` member once per iteration, rather than caching the length. Normally for that I’d say “store the length once up front,” but in this case the code is changing the array length in the loop, so that’d actually break things. On that note, the code as published is actually missing some cookies, because it removes an item from the array and then skips past the new element at that index. To fix that, you need to move the `++i` into the loop so it can be skipped if you do splice the array. Also in order to not need to access the length property many times you could iterate in reverse instead of forwards. I might write the whole function like this:<p><pre><code>  function blockCookies(details) {
    var headers = details.responseHeaders;
    var i = headers.length - 1;
    while (i &gt; -1) {
      if (headers[i].name === &quot;Set-Cookie&quot;) {
        headers.splice(i, 1);
      } else {
        i--;
      }
    }
    return {responseHeaders: headers};
  }</code></pre>