// Extracts the query string and turns it into an object of key/value pairs.
function get_qs_context() {
    var query_string = window.location.search.substring(1);
    var kv_pairs = query_string.split('&');
    var result = {};
    for (var i = 0; i < kv_pairs.length; i++) {
        var kv_pair = kv_pairs[i].split('=');
        result[kv_pair[0]] = decodeURIComponent(kv_pair[1]);
    }
    return result;
}

// Get a value from the querystring for the given key.
function get_qs_value(key) {
    var context = get_qs_context();
    var keys = Object.keys(context);
    for (var i = 0; i < keys.length; i++) {
        if(key == keys[i]) {
            return context[key];
        }
    }
}

// Given a settings object of key/value pairs, will update the query string
// element of the URL accordingly.
function set_qs(settings) {
    var qs_array = [];
    var keys = Object.keys(settings);
    for(i=0; i<keys.length; i++) {
        var key = keys[i];
        var value = settings[key];
        qs_array.push(key + '=' + encodeURIComponent(value));
    }
    var old_url = window.location.href.split('?');
    var new_url = old_url[0] + '?' + qs_array.join('&');
    // shortener API
    var linkRequest = {
      destination: new_url,
      domain: { fullName: "rebrand.ly" }
    }

    var requestHeaders = {
      "Content-Type": "application/json",
      "apikey": "389c7869b56b4084a9749a2b3e1eba03"
    }

    $.ajax({
      url: "https://api.rebrandly.com/v1/links",
      type: "post",
      data: JSON.stringify(linkRequest),
      headers: requestHeaders,
      dataType: "json",
    }).done(function( data ) {
            console.log(data);
            $('#direct-link').attr('href', "https://" + data.shortUrl);
            $('#direct-link').text(data.shortUrl);
            $('#twitter-button').html('<a href="https://twitter.com/share" class="twitter-share-button" data-url="' + data.shortUrl +'" data-text="Check out this cool Python programming comic..! :-)" data-via="ntoll" data-hashtags="pycomic" data-dnt="true">Tweet</a>');
            twttr.widgets.load();
    });
}

@@ -1845,16 +1845,36 @@
 om/share
+?ref_src=twsrc%255Etfw
 %22 class=
@@ -1899,40 +1899,8 @@
 ton%22
- data-url=%22' + data.shortUrl +'%22
  dat
@@ -1926,40 +1926,23 @@
 his 
-cool 
 Python 
-programming 
 comic..!
  :-)
@@ -1941,12 +1941,8 @@
 c..!
- :-)
 %22 da
@@ -1989,27 +1989,120 @@
 ata-
-dnt=%22true%22%3ETweet%3C/a
+show-count=%22false%22%3ETweet%3C/a%3E%3Cscript async src=%22https://platform.twitter.com/widgets.js%22 charset=%22utf-8%22%3E%3C/script
 %3E');

Fix Twitter button.