Facebook feed without ads? Possible?

The cat and mouse game between advertisers and ad-blockers are never over. As soon the blocker guys come up with some solution, the advertisers take counter measure to make the ad-blocking ineffective. No wonder, since selling ads is their business-model.

The gold standard for blocking content -including ads- is to use CSS selector based rules. This is what we can set up in our browser extensions, like Adblock, Adblock Plus, uBlock Origin, etc. All of them come with an extensive set of rules, sometimes referred to as filters.

The weakness in the CSS selector based approach has been identified by advertisers and they have made their counter measure. Since the CSS selector uses fixed IDs and classes, the obvious counter measure is to use dynamically generated IDs and class names on the server side, which changes all the time, making the fixed CSS selector based approach useless.

What else can be done to block unwanted content and ads when dynamic IDs and class names are in place? Well, instead of using CSS selectors, one can inspect the JavaScript objects themselves, using injected JavaScript code. Injecting JavaScript code into pages are supported by the major browsers, through some API. However this often comes with a limitations like the “isolated world” approach like in Chrome, meaning the injected JavaScript code runs in a different -isolated- engine, but shares the same DOM. Of course there are techniques to circumvent this limitation.

Facebook uses React. Even though the IDs and classes are dynamically generated, the properties in the JavaScript objects can be inspected and based on their values one can decide whether a particular feed-unit is genuine from one of your friends, or an ad.

I’ve written a piece of JavaScript code, called as a “scriptlet” -using uBlock Origin’s terminology-, which can be used together with the uBlock Origin browser extension. This small JavaScript code, identifies the ads and other unwanted feed-units -configurable what- and blocks them.

To use it, you first need to install the uBlock Origin browser extension, then configure the extension to use the external scriptlet from my repository, then you need to add a filter line to have it injected.

If you are interested, check out my GitHub repository!

Facebook feed without ads? Possible?

Recursive anonymous functions?

Introduction

The Lambda Calculus, invented by Alonzo Church back in the 1930s, can be referred to as the smallest universal programming language. It’s Turing complete, meaning everything which is computable, can be computed in it.

It has only a few basic constructs, like – anonymous – functions and function application. These are the primitives. By adding Church Numerals, representation of Booleans, conditionals, lists and recursion with the help of combinators, we get a fully usable language.

Functional programming was born to Lambda Calculus. Functional programming is getting more and more attention nowadays, because it makes parallel programming so much easier.

Recursion

Recursion is amazing, since it enables us to compute and process self-similar structures in a natural way. It also helps to solve a more complex problem by breaking it down into similar, but simpler problems to be solved separately. Finally recursion can be thought of an alternative to iteration.

Side note. The conventional programming languages are equipped with looping-constructs like, while, for, etc. The interesting thing is that it turns out these are unnecessary, since the same can be accomplished by using functions and function-application. By adding Tail Call Optimisation (TCO for short) and having the underlying system translate them to primitive loops in the machine language, we can achieve the same performance as we were using the usual looping-constructs to express iteration.

To get recursion working, we need to have a “handle” on the function itself. But functions in Lambda Calculus are anonymous. (Meaning, we cannot really refer to them by their names.) So how can we make it work then? Well, we can bind expressions to function parameters. That’s all what’s offered by Lambda Calculus. But as you will see shortly, it’s sufficient to make it work. U and Y combinators to the rescue!

The U combinator is a higher-order function, that applies its parameter to its parameter. It provides a way to get a handle on the function through parameter binding, hence making recursion possible.

Side note. The smallest non-terminating program can be written using the U combinator like this: U(U)

The Y combinator is a bit more complicated compared to the U combinator, and it’s better-known too.

The funny thing is that today’s programming languages, which are functional, or multi-paradigm but supporting the functional paradigm as well, are capable of this kind of programming. That’s because they are descendants of the Lambda Calculus.

Next let’s see how to do recursion with anonymous functions in some mainstream programming languages. And, also in Scheme for those who are not afraid of this beautiful, minimalistic LISP dialect.

Code Examples

Side note. While in Lambda Calculus we bind the name (parameter of the function) to the expression to which the function is applied, they’re bound to the values of the expressions in the following examples, since they use applicative order evaluation. That’s why it’s better to call these combinators applicative order combinators.

function (f) { return f(f); }
(function (f) {
  return f(f);
}(function (h) {
  return function(n) {
    return n <= 1 ? 1 : n * h(h)(n - 1);
};
}))(5);
 -> f { f.call(f) }
-> f { f.call(f) }.call(
  lambda do |h|
    lambda do |n|
      n <= 1 ? 1 : n * h.call(h).call(n - 1)
    end
  end
).call(5)
(lambda (f) (f f))
(((lambda (f) (f f))
  (lambda (h)
    (lambda (n)
      (if (<= n 1)
        1
        (* n ((h h) (- n 1)))))))
  5)

 

References

  1. Lambda Calculus
  2. Anonymous function
  3. Church Encoding, U combinator
  4. Y Combinator
Recursive anonymous functions?

Programming an AVR MCU in JavaScript

arduinoyun

I have launched a project exploring some aspects of moving the development of an AVR MCU from the AVR/C++ world to the MIPS Linux/JavaScript world.

Code example:

// PWM glowing example.
// Copyright (C) 2015 Imre Horvath
//
// Build a circuit using a breadboard like this:
// Arduino Yun digital pin 11 (PWM) --> 270 R 5% --> 5 mm LED --> Arduino Yun GND
//
// This code below will cause the LED to glow first brighter and brighter gradually,
// then it will glow dimmer and dimmer till off.
// And then, this will repeat forever.

var firmata = require('firmata');
var board = new firmata.Board('/dev/ttyATH0', function (err) {

  var pwmPin = 11;
  var value = 0;
  var up = true;
  var incr = 10;

  if (err) {
    console.log(err);
    return;
  }

  function cleanExit() {
    board.reset();
    process.exit();
  }

  process.on('SIGTERM', cleanExit);
  process.on('SIGINT', cleanExit);

  board.pinMode(pwmPin, board.MODES.PWM);
  
  setInterval(function () {

    board.analogWrite(pwmPin, value);

    if (up) {
      if (value < 250) {
        value += incr;
      } else {
        up = false;
        value -= incr;
      }
    } else {
      if (value > 0) {
        value -= incr;
      } else {
        up = true;
        value += incr;
      }
    }
  }, 125);

});

Actually all the parts to assemble a system for experimenting are available.

  1. They have created the Arduino Yun, which is an ATMega AVR MCU connected to a Atheros MIPS Linux SOC with a serial line on board.
  2. There is the Arduino software to program the MCU
  3. There is the StandardFirmata sketch and the modification of it to enable it’s use within the Yun
  4. There is the Node.js and the serialport module with native extension provided through the Yun package repository
  5. You can configure BusyBox to start your node app at the end of the boot process, hence having a similar behaviour as with a native MCU, but with slower startup time
  6. You have SCP to transfer your app from your development machine to the Yun
  7. You can create a simple helper script to stop the old app on the Yun, transfer the new app and finally start it again.

It is an interesting idea to program an MCU in JavaScript, because of the language’s powerful features like closures, first-class function objects and higher-order functions. These features enable enormous expressive power compared to plain C/C++.

However transferring control from the AVR to the Embedded Linux on-board through a serial line (57600 baud) and the Firmata protocol on top of it, might render this idea useless for time critical real-time applications.

Also a lot of low-level (C/C++) libraries are available for the Arduino platform like the Arduino-IRremote which are unavailable and unusable on the Node.js side. While other low-level libraries like the one for controlling a WiFi shield are rendered unnecessary when using a Yun with the Linux side which already has WiFI capabilities.

Check out my GitHub repo on the topic!

Programming an AVR MCU in JavaScript