index2018-12-03

Week 12 ~ eroding capitalism with a mouse

this post is in progress

In order to make the software side of the anti-capitalist mouse work I needed to build a few environments.

First I needed to build a chrome browser extension. Chrome extensions only require a few files and allow you to drop javascript (or any kind of code) onto any webpage. Below I have pasted the code which looks for HTML elements that have a class name of ad.

If the mouse moves over any of the ad elements a function called sendPost will be triggered. From here an ajax post request is sent to a local node server which I am simultaneously running on my machine.

var ads = document.getElementsByClassName('ad');
var cads = document.getElementsByClassName('c-ad');

for (var i = 0; i < ads.length; i++) {
  changeElement(ads[i]);
}

for (var i = 0; i < cads.length; i++) {
  changeElement(cads[i]);
}

function changeElement(el) {
  console.log(el);
  el.addEventListener("mouseover", mouseOver);
}

function mouseOver() {
  sendPost();
}

function sendPost(dat) {
  $.ajax({
    url: "http://localhost:8080/posts",
    type: "POST",
    data: JSON.stringify({
      "data": "We can bring to birth a new world from the ashes of the old ",
    }),
    error: function(e) {
      console.log(e);
    },
    dataType: "json",
    cache: false, 
    contentType: "application/json; charset=utf-8"
  });
}

The node server is listening for data coming in and printing it to the console.

server.post("/posts", function(request, response) {
  console.log(request.body.data);
});

The final step will be making a Serial.send function inside the function above so that the Arduino will know when to turn on the vibrating motor inside the physical mouse.