(function() {

  // Feature detection results
  var supports = {};

  // Detect localStorage support
  try {
    localStorage.setItem('test', 'test');
    localStorage.removeItem('test');
    supports.localStorage = true;
  } catch (e) {
    supports.localStorage = false;
  }

  // Detect inline SVG support
  supports.inlineSVG = (function() {
    var div = document.createElement('div');
    div.innerHTML = '<svg/>';
    return (
      typeof SVGRect != 'undefined'
      && div.firstChild
      && div.firstChild.namespaceURI
    ) == 'http://www.w3.org/2000/svg';
  })();

  // Add ios class to body on iOS devices
  function iosBodyClass() {
    if(
      /iPad|iPhone|iPod/.test(navigator.userAgent)
      && !window.MSStream
      && document.body.classList
    ) {
      document.body.classList.add('ios');
    }
  }

  // Favourite nodes
  function favouriteNodes() {

    // Get heart SVG
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/assets/heart.svg');
    xhr.addEventListener('load', function(res) {

      // Create heart SVG elem
      var div = document.createElement('div');
      div.innerHTML = xhr.responseText;
      this.heart = div.firstChild;

      // Inject heart into DOM
      this.title = document.querySelector('h2.node-title');
      if(this.title) {
        this.title.insertBefore(this.heart, this.title.firstChild);
      }
    });
    xhr.send();
  }

  // Check if DOM has already loaded as we're loading async
  ['interactive', 'complete'].indexOf(document.readyState) >= 0
    ? init()
    : document.addEventListener('DOMContentLoaded', init);

  // When DOM is ready
  function init() {

    // Check for iOS
    iosBodyClass();

    // Check required features for favourite nodes
    if(supports.localStorage && supports.inlineSVG) {
      favouriteNodes();
    }
  }

})();

@@ -8,16 +8,339 @@
 on() %7B%0A%0A
+  // Run callback when DOM is ready%0A  function DOMReady(fn) %7B%0A%0A    // Run now if DOM has already loaded as we're loading async%0A    if(%5B'interactive', 'complete'%5D.indexOf(document.readyState) %3E= 0) %7B%0A      fn();%0A%0A    // Otherwise wait for DOM%0A    %7D else %7B%0A      document.addEventListener('DOMContentLoaded', fn);%0A    %7D%0A  %7D%0A%0A
   // Fea
@@ -904,299 +904,100 @@
  // 
-Add ios class to body on iOS devices%0A  function iosBodyClass() %7B%0A    if(%0A      /iPad%7CiPhone%7CiPod/.test(navigator.userAgent)%0A      && !window.MSStream%0A      && document.body.classList%0A    ) %7B%0A      document.body.classList.add('ios');%0A    %7D%0A  %7D%0A%0A  // Favourite nodes%0A  function favouriteNodes(
+Check required features for favourite nodes%0A  if(supports.localStorage && supports.inlineSVG
 ) %7B%0A
@@ -1270,26 +1270,27 @@
 ;%0A      
-this.
+var 
 heart
+El
  = div.f
@@ -1338,26 +1338,57 @@
 M%0A      
-this.
+DOMReady(function() %7B%0A        var 
 title
+El
  = docum
@@ -1433,21 +1433,20 @@
     
+  
 if(
-this.
 title
+El
 ) %7B%0A
@@ -1453,26 +1453,25 @@
         
-this.
+  
 title
+El
 .insertB
@@ -1480,30 +1480,24 @@
 ore(
-this.
 heart
+El
 , 
-this.
 title
+El
 .fir
@@ -1512,17 +1512,29 @@
 ;%0A      
-%7D
+  %7D%0A      %7D);
 %0A    %7D);
@@ -1564,233 +1564,64 @@
  // 
-Check if DOM has already loaded as we're loading async%0A  %5B'interactive', 'complete'%5D.indexOf(document.readyState) %3E= 0%0A    ? init()%0A    : document.addEventListener('DOMContentLoaded', init);%0A%0A  // When DOM is r
+Add ios class to body on iOS devices%0A  DOMR
 eady
-%0A  
+(
 function
  ini
@@ -1620,187 +1620,178 @@
 tion
- init
 () %7B%0A
-%0A
     
-// Check for iOS%0A    iosBodyClass();%0A%0A    // Check required features for favourite nodes%0A    if(supports.localStorage && supports.inlineSVG) %7B%0A      favouriteNodes(
+if(%0A      /iPad%7CiPhone%7CiPod/.test(navigator.userAgent)%0A      && !window.MSStream%0A      && document.body.classList%0A    ) %7B%0A      document.body.classList.add('ios'
 );%0A 
@@ -1798,16 +1798,18 @@
    %7D%0A  %7D
+);
 %0A%0A%7D)();%0A

Refactor JS