$(function() {
  // NOTE: if pasting this code into another script, you'll need to manually change the
  // next line. Instead of assigning the value null, you need to assign the value of
  // the Canvabadges domain, i.e. "https://www.canvabadges.org". If you have a custom
  // domain configured then it'll be something like "https://www.canvabadges.org/_my_site"
  // instead.
  var protocol_and_host = null;
  var $scripts = $("script");
  $("script").each(function() {
    var src = $(this).attr('src');
    if(src && src.match(/canvas_profile_badges/)) {
      var splits = src.split(/\//);
      protocol_and_host = splits[0] + "//" + splits[2];
    }
    var prefix = src && src.match(/\?path_prefix=\/(\w+)/);
    if(prefix && prefix[1]) {
      protocol_and_host = protocol_and_host + "/" + prefix[1];
    }
  });
  if(!protocol_and_host) {
    console.log("Couldn't find a valid protocol and host. Canvabadges will not appear on profile pages until this is fixed.");
  }
  var match = location.href.match(/\/(users|about)\/(\d+)$/);
  if(match && protocol_and_host) {
    var user_id = match[2];
    var domain = location.host;
    var url = protocol_and_host + "/api/v1/badges/public/" + user_id + "/" + encodeURIComponent(domain) + ".json";
    $.ajax({
      type: 'GET',
      dataType: 'jsonp',
      url: url,
      success: function(data) {
        if(data.objects && data.objects.length > 0) {
          var $box = $("<div/>", {style: 'margin-bottom: 20px;'});
          $box.append("<h2 class='border border-b'>Badges</h2>");
          for(idx in data.objects) {
            var badge = data.objects[idx];
            var $badge = $("<div/>", {style: 'float: left;'});
            var link = protocol_and_host + "/badges/criteria/" + badge.config_id + "/" + badge.config_nonce + "?user=" + badge.nonce;
            var $a = $("<a/>", {href: link});
            $a.append($("<img/>", {src: badge.image_url, style: 'width: 72px; height: 72px; padding-right: 10px;'}));
            $badge.append($a);
            $box.append($badge);
          }
          $box.append($("<div/>", {style: 'clear: left'}));
          $("#edit_profile_form,fieldset#courses,.more_user_information + div").after($box);
        }
      },
      error: function() {
        console.log("badges failed to load");
      },
      timeout: 5000
    });
  }
});
@@ -404,16 +404,45 @@
 = null;%0A
+  if(!protocol_and_host) %7B%0A  
   var $s
@@ -465,16 +465,18 @@
 pt%22);%0A  
+ 
+ 
 $(%22scrip
@@ -493,24 +493,26 @@
 unction() %7B%0A
+  
     var src 
@@ -538,16 +538,18 @@
 ');%0A    
+ 
+ 
 if(src &
@@ -590,24 +590,26 @@
 /)) %7B%0A      
+  
 var splits =
@@ -622,24 +622,26 @@
 plit(/%5C//);%0A
+  
       protoc
@@ -684,26 +684,30 @@
 its%5B2%5D;%0A    
-%7D%0A
+  %7D%0A  
     var pref
@@ -758,16 +758,18 @@
 /);%0A    
+  
 if(prefi
@@ -782,24 +782,26 @@
 refix%5B1%5D) %7B%0A
+  
       protoc
@@ -859,16 +859,24 @@
     
-%7D%0A
+  %7D%0A  
   %7D);%0A
+  %7D%0A
   if
@@ -915,16 +915,29 @@
 le.log(%22
+CANVABADGES: 
 Couldn't
@@ -2276,16 +2276,131 @@
 ($box);%0A
+        %7D else %7B%0A          console.log(%22CANVABADGES: no badges found for the user: %22 + user_id + %22 at %22 + domain);%0A
         
@@ -2457,16 +2457,29 @@
 le.log(%22
+CANVABADGES: 
 badges f
@@ -2491,16 +2491,36 @@
  to load
+, API error response
 %22);%0A    

use protocol if provided, better logging