Team:HSAAHNU Anhui/inject.js

From 2014hs.igem.org

(Difference between revisions)
(add nprogress)
(refactor)
Line 1: Line 1:
-
function removeStyleNodes () {
+
/*
-
   var styleNodes = document.head.getElementsByTagName('style');
+
* Basically this script will do the following thing:
-
  while (styleNodes.length != 0) {
+
* Remove old styles.
-
    document.head.removeChild(styleNodes[0]);
+
* Apply common stylesheets.
 +
* Update jQuery.
 +
* Clean up script tags in head tag for debugging purpose.
 +
* move inner HTML of main tag to body tag for clarity.
 +
* Set up PJAX for better performance.
 +
* Use PJAX.complete to set up page specific things.
 +
*/
 +
 
 +
function removeOldStyles () {
 +
   function removeStyleNodes () {
 +
    var styleNodes = document.head.getElementsByTagName('style');
 +
    while (styleNodes.length != 0) {
 +
      document.head.removeChild(styleNodes[0]);
 +
    }
   }
   }
-
}
 
-
function removelinkStylesheetNodes () {
+
  function removelinkStylesheetNodes () {
-
  var linkNodes = document.head.getElementsByTagName('link');
+
    var linkNodes = document.head.getElementsByTagName('link');
-
  var nonStylesheetLinksCounter = 0;
+
    var nonStylesheetLinksCounter = 0;
-
  var iter = 0;
+
    var iter = 0;
-
  while (linkNodes.length != nonStylesheetLinksCounter) {
+
    while (linkNodes.length != nonStylesheetLinksCounter) {
-
    var node = linkNodes[iter];
+
      var node = linkNodes[iter];
-
    if (node['rel'] == 'stylesheet')
+
      if (node['rel'] == 'stylesheet')
-
      document.head.removeChild(node);
+
        document.head.removeChild(node);
-
    else {
+
      else {
-
      nonStylesheetLinksCounter ++;
+
        nonStylesheetLinksCounter ++;
-
      iter ++;
+
        iter ++;
 +
      }
     }
     }
   }
   }
 +
 +
  removeStyleNodes();
 +
  removelinkStylesheetNodes();
}
}
-
removeStyleNodes();
+
function applyCommonStyles () {
-
removelinkStylesheetNodes();
+
  var stylesheetURLs = [
 +
    'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css',
 +
    'http://ricostacruz.com/nprogress/nprogress.css',
 +
    '/Team:HSAAHNU_Anhui/normalize.css?action=raw&ctype=text/css'
 +
  ];
-
var stylesheetList = [
+
  stylesheetURLs.forEach(function (url) {
-
  '<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">',
+
    var tag = document.createElement('link');
-
  '<link href="/Team:HSAAHNU_Anhui/normalize.css?action=raw&ctype=text/css" rel="stylesheet">',
+
    tag.href = url;
-
  '<link href="http://ricostacruz.com/nprogress/nprogress.css" rel="stylesheet">'
+
    tag.rel = 'stylesheet';
-
];
+
    document.head.appendChild(tag);
 +
  });
 +
}
-
document.head.innerHTML += stylesheetList.join('\n');
+
function updateJQuery () {
 +
  var tag = document.createElement('script');
 +
  tag.src = 'http://code.jquery.com/jquery-1.11.1.min.js';
 +
  document.head.appendChild(tag);
 +
}
-
var javascriptList = [
+
function relocateMainDocument () {
-
   'http://ricostacruz.com/nprogress/nprogress.js'
+
  var main = document.body.getElementsByTagName('main')[0];
-
];
+
   document.body.innerHTML = main.innerHTML;
 +
}
-
javascriptList.forEach(function (script) {
+
removeOldStyles();
-
  $.getScript(script);
+
applyCommonStyles();
-
});
+
updateJQuery();
 +
relocateMainDocument();

Revision as of 16:52, 29 May 2014

/*

* Basically this script will do the following thing:
* Remove old styles.
* Apply common stylesheets.
* Update jQuery.
* Clean up script tags in head tag for debugging purpose.
* move inner HTML of main tag to body tag for clarity.
* Set up PJAX for better performance.
* Use PJAX.complete to set up page specific things.
*/

function removeOldStyles () {

 function removeStyleNodes () {
   var styleNodes = document.head.getElementsByTagName('style');
   while (styleNodes.length != 0) {
     document.head.removeChild(styleNodes[0]);
   }
 }
 function removelinkStylesheetNodes () {
   var linkNodes = document.head.getElementsByTagName('link');
   var nonStylesheetLinksCounter = 0;
   var iter = 0;
   while (linkNodes.length != nonStylesheetLinksCounter) {
     var node = linkNodes[iter];
     if (node['rel'] == 'stylesheet')
       document.head.removeChild(node);
     else {
       nonStylesheetLinksCounter ++;
       iter ++;
     }
   }
 }
 removeStyleNodes();
 removelinkStylesheetNodes();

}

function applyCommonStyles () {

 var stylesheetURLs = [
   'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css',
   'http://ricostacruz.com/nprogress/nprogress.css',
   '/Team:HSAAHNU_Anhui/normalize.css?action=raw&ctype=text/css'
 ];
 stylesheetURLs.forEach(function (url) {
   var tag = document.createElement('link');
   tag.href = url;
   tag.rel = 'stylesheet';
   document.head.appendChild(tag);
 });

}

function updateJQuery () {

 var tag = document.createElement('script');
 tag.src = 'http://code.jquery.com/jquery-1.11.1.min.js';
 document.head.appendChild(tag);

}

function relocateMainDocument () {

 var main = document.body.getElementsByTagName('main')[0];
 document.body.innerHTML = main.innerHTML;

}

removeOldStyles(); applyCommonStyles(); updateJQuery(); relocateMainDocument();