Team:HSAAHNU Anhui/inject.js
From 2014hs.igem.org
(Difference between revisions)
m (breakpoint insert) |
(analyze anchor) |
||
Line 95: | Line 95: | ||
function loadNewPage (href) { | function loadNewPage (href) { | ||
- | alert(href); | + | alert(JSON.stringify(href)); |
+ | } | ||
+ | |||
+ | function startsWith (lhs, rhs) { | ||
+ | return lhs.substr(0, rhs.length) === rhs; | ||
+ | } | ||
+ | |||
+ | function analyzeAnchor (href) { | ||
+ | var basePath = 'https://2014hs.igem.org/Team:HSAAHNU_Anhui'; | ||
+ | var isOutsideLink = !startsWith(href, basePath); | ||
+ | if (isOutsideLink) | ||
+ | return false; | ||
+ | |||
+ | var current = document.URL.substr(basePath.length).split('#'); | ||
+ | var next = href.substr(basePath.length).split('#'); | ||
+ | |||
+ | var isSamePage = current[0] == next[0]; | ||
+ | if (isSamePage) | ||
+ | return false; | ||
+ | |||
+ | return { | ||
+ | path: next[0], | ||
+ | anchor: next[1] | ||
+ | }; | ||
} | } | ||
function setupPJAXListener () { | function setupPJAXListener () { | ||
- | |||
$("a").click(function (event) { | $("a").click(function (event) { | ||
var node = event.target; | var node = event.target; | ||
- | + | var href = analyzeAnchor(node.href); | |
- | + | if (href === false) | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
return true; | return true; | ||
- | loadNewPage( | + | loadNewPage(href); |
return false; | return false; | ||
}); | }); |
Revision as of 02:38, 30 May 2014
/*
* Basically this script will do the following thing: * Remove old styles. * Apply common stylesheets. * Update jQuery. * move inner HTML of main tag to body tag for clarity. * Clean up script tags in head tag for debugging purpose. * 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;
}
var JQueryLoadingTime = 0; function waitForJQueryToBeUpdated () {
//16 seconds is long enough. var isLoadFailed = JQueryLoadingTime >= 32; if (isLoadFailed) alert('One component of this page failed to load. Please refresh!');
var isJQueryUpdated = $.fn.jquery === '1.11.1'; if (isJQueryUpdated) resumeInitAfterJQueryUpdated(); else { JQueryLoadingTime ++; setTimeout(waitForJQueryToBeUpdated, 500); }
}
$(document).ready(function () {
removeOldStyles(); applyCommonStyles(); updateJQuery(); relocateMainDocument(); waitForJQueryToBeUpdated();
});
function cleanUpScriptTags () {
var scriptNodes = document.head.getElementsByTagName('script'); while (scriptNodes.length !== 0) { document.head.removeChild(scriptNodes[0]); }
}
function loadNewPage (href) {
alert(JSON.stringify(href));
}
function startsWith (lhs, rhs) {
return lhs.substr(0, rhs.length) === rhs;
}
function analyzeAnchor (href) {
var basePath = 'https://2014hs.igem.org/Team:HSAAHNU_Anhui'; var isOutsideLink = !startsWith(href, basePath); if (isOutsideLink) return false;
var current = document.URL.substr(basePath.length).split('#'); var next = href.substr(basePath.length).split('#');
var isSamePage = current[0] == next[0]; if (isSamePage) return false;
return { path: next[0], anchor: next[1] };
}
function setupPJAXListener () {
$("a").click(function (event) { var node = event.target; var href = analyzeAnchor(node.href); if (href === false) return true;
loadNewPage(href); return false; });
}
function resumeInitAfterJQueryUpdated () {
cleanUpScriptTags(); setupPJAXListener();
}