Skip to content

Instantly share code, notes, and snippets.

@dario2994
Last active May 1, 2016 20:09
Show Gist options
  • Save dario2994/e7f2c9e10419e941f6fe to your computer and use it in GitHub Desktop.
Save dario2994/e7f2c9e10419e941f6fe to your computer and use it in GitHub Desktop.
A simple js script that creates two html pages, suitable for printing, summarizing the information given in FalesiaOnline.it on a certain sector.
// It must be executed in a page as http://falesiaonline.it/settorefoto/123/sector-name.html
// Two files will be downloaded: the first is a photo of the sector with routes outlines, the second is a list of all the routes with grades and lengths.
function DownloadHtml(FileName, source) {
// Adding sector name to the source
var PossibleElements = document.getElementsByClassName("name-cell");
var SectorName = '';
for (var it in PossibleElements) {
var SectorTd = PossibleElements[it];
if (SectorTd.tagName == 'TD') {
SectorName = SectorTd.innerHTML;
break;
}
}
console.log(SectorName);
var FirstColon = SectorName.lastIndexOf(':');
SectorName = SectorName.substr(FirstColon+2);
var source = '<h1>' + SectorName + '</h1>' + source;
var SeparatedPathname = window.location.pathname.split("/");
var SectorName = SeparatedPathname[SeparatedPathname.length-1];
var blob = new Blob([source], {type: 'text/html; charset=utf8; '});
var link = document.createElement('a');
link.setAttribute('href', window.URL.createObjectURL(blob));
link.setAttribute('download', FileName + '_' + SectorName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
var SvgImage = document.getElementById('topoSvgContent').cloneNode(true);
var RouteLabels = SvgImage.getElementById('Route_Labels');
// Replacing difficulty with id
var AllTexts = RouteLabels.getElementsByTagName('text');
for (var i = 0; i < AllTexts.length; i++) {
var TextTag = AllTexts[i];
var IdValue = TextTag.parentElement.id;
TextTag.innerHTML = IdValue.substring(18, 20);
}
// All rectangles must have the same width
var AllRects = RouteLabels.getElementsByTagName('rect');
var AllWidths = [];
for (var i = 0; i < AllRects.length; i++) {
var RectTag = AllRects[i];
AllWidths.push(parseFloat(RectTag.getAttribute('width')));
}
AllWidths.sort();
console.log(AllWidths);
var CorrectWidth = 20.0;
if (AllWidths.length > 10) CorrectWidth = AllWidths[Math.floor(AllWidths.length/4)];
else if (AllWidths.length > 2)CorrectWidth = AllWidths[1];
CorrectWidth += 1;
console.log(CorrectWidth);
for (var i = 0; i < AllRects.length; i++) {
var RectTag = AllRects[i];
var x = parseInt(RectTag.getAttribute('x'));
var dx = parseInt(RectTag.getAttribute('width'));
var AverageX = x + (dx/2);
RectTag.setAttribute('x', AverageX - CorrectWidth/2.0);
RectTag.setAttribute('width', CorrectWidth + 0.5);
}
// Reference to the image must be absolute
if (SvgImage.getElementsByTagName('image').length > 0) {
var PhotoTag = SvgImage.getElementsByTagName('image')[0];
PhotoTag.setAttribute('xlink:href', 'http://falesiaonline.it/' + PhotoTag.getAttribute('xlink:href'));
}
var ImageSource = SvgImage.outerHTML;
DownloadHtml('photo', ImageSource);
var RoutesList = document.getElementsByClassName('routelistinnerelement');
var RoutesInfo = [];
for (var i = 0; i < RoutesList.length; i++) {
var route = RoutesList[i];
var NameTag = route.getElementsByClassName('starsandroutename')[0];
var name = NameTag.childNodes[NameTag.childNodes.length-1].textContent.trim();
var DifficultyAndHeight = route.getElementsByClassName('routedetails')[0].childNodes[0].textContent.trim().split('\n');
var difficulty = DifficultyAndHeight[0].trim();
var RouteHeight = 'ignota';
if (DifficultyAndHeight.length > 1) RouteHeight = DifficultyAndHeight[1].trim().replace(/–/g, "-");
var RouteId = parseInt(route.id.substring(19,23)) %100;
RoutesInfo.push({id: RouteId, name: name, difficulty: difficulty, height: RouteHeight});
}
// Building table
var table = document.createElement('table');
// table.setAttribute('cellpadding', 10);
var thead = document.createElement('thead');
var HeaderTr = document.createElement('tr');
var DetailsList = ['id', 'name', 'difficulty', 'height'];
for (var i = 0; i < DetailsList.length; i++) {
var th = document.createElement('th');
th.innerHTML = DetailsList[i];
HeaderTr.appendChild(th);
}
thead.appendChild(HeaderTr);
table.appendChild(thead);
var tbody = document.createElement('tbody');
for (var i = 0; i < RoutesInfo.length; i++) {
var route = RoutesInfo[i];
var tr = document.createElement('tr');
for (var j = 0; j < DetailsList.length; j++) {
var td = document.createElement('td');
td.textContent = route[DetailsList[j]];
td.style.padding = '5px';
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
var TableSource = table.outerHTML;
DownloadHtml('RoutesList', TableSource);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment