Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add cache to linkcheck. It will check once per location.pathname per
commit. On a new commit localStorage is wiped.
  • Loading branch information
gfldex committed Jul 14, 2016
1 parent 2d9cfd9 commit 573d773
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions html/js/main.js
Expand Up @@ -110,36 +110,50 @@ function setup_debug_mode(){

}

console.info("checking for dead links");
if(window.localStorage){
var sS = window.localStorage;
var commit = $('#footer-commit').text();
if ( sS.getItem('commit') != commit ) {
sS.clear();
sS.setItem('commit', commit);
console.info("wiping cache");
}

if ( ! sS.getItem(commit+window.location.pathname) ) {
sS.setItem(commit+window.location.pathname, "seen");
console.info("checking for dead links");

function report_broken_link(url) {
$('html').find('#search').after('<div style="text-align: center;">Broken link: ' + url + ' found. Please report at <a href="https://webchat.freenode.net/?channels=perl6">irc.freenode.net#perl6</a></div>');
}
function report_broken_link(url) {
$('html').find('#search').after('<div style="text-align: center;">Broken link: ' + url + ' found. Please report at <a href="https://webchat.freenode.net/?channels=perl6">irc.freenode.net#perl6</a></div>');
}

var seen_link = [];
$('html').find('a[href]').each( function(i, el) {
var url_without_anchor = el.href.split('#')[0];
if ( ! seen_link.includes(decodeURIComponent(url_without_anchor)) ) {
seen_link.push(decodeURIComponent(url_without_anchor));
}
});
var seen_link = [];
$('html').find('a[href]').each( function(i, el) {
var url_without_anchor = el.href.split('#')[0];
if ( ! seen_link.includes(decodeURIComponent(url_without_anchor)) ) {
seen_link.push(decodeURIComponent(url_without_anchor));
}
});

seen_link.forEach( function(url) {
var request = new XMLHttpRequest();

request.onreadystatechange = function(){
if ( request.readyState === 4 ) {
if ( request.status >= 400 ) {
report_broken_link(request.status + " for " + url);
} else {
// console.log(request.status + " for " + url);
seen_link.forEach( function(url) {
var request = new XMLHttpRequest();

request.onreadystatechange = function(){
if ( request.readyState === 4 ) {
if ( request.status >= 400 ) {
report_broken_link(request.status + " for " + url);
} else {
// console.log(request.status + " for " + url);
}
}
}
}
}

try {
request.open('HEAD', url);
request.send();
} catch (e) { /* this will catch errors due to browser security settings for external links */ }
});
try {
request.open('HEAD', url);
request.send();
} catch (e) { /* this will catch errors due to browser security settings for external links */ }
});
}
}
}

0 comments on commit 573d773

Please sign in to comment.