Things I learned today


  1. Mozjpeg https://imageoptim.com/mozjpeg

Chrome

  1. Background scripts tag in manifest

  2. checking background messages in console.

JavaScript

  1. Making async calls synchronously in JavaScript using callback.
    //This is an async method;
    //We wanted geo location before making ajax call.
    function geolocation(callback) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var latitude = position.coords.latitude.toFixed(3)
        var longitude = position.coords.longitude.toFixed(3)
        var location='{"lat" : '+latitude+',"long" : '+longitude+'}'
        callback(JSON.parse(location))
      })
    }
    
    //get geo location using callback & then make POST request
    geolocation(function(geo) {
     $.ajax({
     url: ANALYTICS_HOST + '/store_session',
     data: JSON.stringify(geo),
     contentType: 'application/json',
     dataType: 'json',
     type: 'POST'
     })
    });

  2. JSON.stringify vs JSON.parse

    • JSON.stringify turns Javascript object into json text & stores it in a string

    • JSON.parse turns string of JSON text to javascript object.

Comments