• Home
  • About
    • Richie's Blog photo

      Richie's Blog

      Science, data, biology, digital health, programming, tech

    • Learn More
    • Twitter
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Using the XMLHttpRequest API in Chrome Extensions

28 Aug 2016

Reading time ~2 minutes

Data transfer between JavaScript client and server is made relatively simple by using the XMLHttpRequest API.

function fetchLights(fooValue, barValue, userObject, callback) {

    // The URL to POST our data to
    var url = "https://127.0.0.1:8000/users/";

    // Create JSON data string to send via POST
    var mydata = JSON.stringify({
                          "foo": fooValue,
                          "bar": varValue,
                          "user": userObject
                        });

    // Set up an asynchronous AJAX POST request
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    
    // Set header to send JSON data 
    xhr.setRequestHeader("Content-Type", "application/json");

    // Handle request state change events
    xhr.onreadystatechange = function() { 

        if (xhr.readyState == 4) {  // xhr request completed

            if (xhr.status == 200) {
              // Data posted successfully and JSON returned
              // Parse the returned JSON string to an object and pass to callback function 
              callback(userObject, JSON.parse(xhr.responseText));
            } else {
                // Show what went wrong
                console.log("XMLHttpRequest error")
                console.log(xhr.responseText)
            }

        }

    };

    // Send the request and set status
    xhr.send(mydata);

}

Dealing with SSL

The package manager Homebrew is an amazing tool which has made working on a Mac even easier and more productive. It’s a great way to keep tools and packages updated, is easy to manage, and my time spent fixing dependency issues and similar problems has been greatly reduced ever since I started using it a couple of years ago. However, as Homebrew doesn’t delete old versions of installed formulae, over time the ‘Cellar’ become filled with old/unused files and directories and it was time for a clearout. As with most things involving Homebrew, this was very straightforward and relatively painless:

$ brew cleanup

A number of old formula versions were taking up a surprising amount of sapce, visible during the cleanup process, ranging from 22 versions of Node.js each taking ~35-40MB, and 11 versions of MySQL, each between 315-445MB. In total I managed to clear 21.9 GB of disk space, which was very welcome indeed! If I need older versions, I can reinstall and brew switch to them as needed.

A dry-run of the cleanup, where nothing is deleted but the items to be deleted are listed can be done by using the -n switch:

$ brew cleanup -n

Fixing virtualenv problems post-cleanup

One adverse thing from this cleanup was that my virtualenvironments for Python projects now seem to be all broken. This is due to the original symlinks made when the virtualenv was created were links to the Python installed by Homebrew and in use at the time of creation. When brew cleanup is run, Homebrew deletes old versions of Python, and those symlinks then point to paths that don’t exist. The fix to this is relatively simple though, recreating the symlinks in the virtualenv. For example, in a virtualenv called ‘foo-env’, this would be:

# Delete old symlinks for virtualenv foo-env
find ~/.virtualenvs/foo-env/ -type l -delete

# Create new symlinks for virtualenv foo-env
virtualenv ~/.virtualenvs/foo-env

Further, using gfind can be used to fix only those links that are broken:

gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete

The fix to this problem was found in this Stackoverflow post.



Like Tweet +1