Greasy Fork

Geocaching Druckausgabe

try to take over the world!

目前为 2016-05-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         Geocaching Druckausgabe
// @namespace    http://www.geocaching.com/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.geocaching.com/seek/cdpf.aspx?guid=*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    window.getCity = function(sCoords) {
        var aCoords;

        sCoords = sCoords.split('°').join('');
        sCoords = sCoords.split('.').join(' ');
        aCoords = sCoords.split(' ');

        var lat = Number(aCoords[1]) + Number(aCoords[2]) / 60 + Number(aCoords[3]) / 60000;
        if (aCoords[0] == 'S') {
            lat = 0 - lat;
        }

        var lon = Number(aCoords[5]) + Number(aCoords[6]) / 60 + Number(aCoords[7]) / 60000;
        if (aCoords[4] == 'W') {
            lon = 0 - lon;
        }
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({'location': {lat: lat, lng: lon}}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                for(var i = 0; i < results.length; i++) {
                    if (results[i].formatted_address.split(',').length == 2) {
                        document.querySelector('.gc-city').textContent = results[i].formatted_address;
                        break;
                    }
                }
            }
        });
    };

    window.initMap = function() {
        var oCacheData = {};
        var elements = document.querySelectorAll('#Content .ui-widget-content');
        var waypoints = [];
        var i;
        var waypoint;

        oCacheData.name = document.querySelector('#Content h2 ').textContent.trim();
        oCacheData.coords = document.querySelector('.LatLong ').textContent.trim();
        oCacheData.lat = oCacheData.coords.split(' ')[0];
        oCacheData.lon = oCacheData.coords.split(' ')[3];
        oCacheData.code = document.querySelector('#Header .HalfRight h1').textContent.trim();
        oCacheData.type = document.querySelector('#Content h2 img').getAttribute('alt');
        oCacheData.difficulty = document.querySelectorAll('#Content .DiffTerr img')[0].getAttribute('alt').split('out of 5')[0].trim();
        oCacheData.terrain = document.querySelectorAll('#Content .DiffTerr img')[1].getAttribute('alt').split('out of 5')[0].trim();
        oCacheData.size = document.querySelector('#Content .Third.AlignCenter small').textContent.replace('(', '').replace(')', '').trim();

        elements = document.querySelectorAll('#Waypoints tbody tr');
        for (i = 0; i <	elements.length; i++) {
            if (elements[i].getAttribute('ishidden')) {
                if (waypoint !== undefined && waypoint !== null) {
                    waypoints.push(waypoint);
                    waypoint = null;
                }
                waypoint = {};
                waypoint.id = elements[i].querySelectorAll('td')[4].textContent.trim();
                waypoint.coords = elements[i].querySelectorAll('td')[6].textContent.replace('???', oCacheData.lat + ' ____________ ' + oCacheData.lon + ' ____________ ').replace('?', '__').trim();
                waypoint.type = elements[i].querySelector('td:nth-child(6) a').textContent.trim();
            } else {
                waypoint.content = elements[i].innerText.replace('Note:', '').trim();
            }
        }
        if (waypoint !== undefined && waypoint !== null) {
            waypoints.push(waypoint);
        }
        console.dir(waypoints);

        elements = document.querySelectorAll('#Content .ui-widget-content');
        elements[3].remove();
        elements[4].remove();
        elements[5].remove();
        elements[6].remove();
        elements[7].remove();
        elements[8].remove();

        document.querySelector('#Footer').remove();
        document.querySelector('#Header').remove();
        document.querySelector('.TermsWidget').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('p.Meta').remove();
        document.querySelector('.Third').remove();
        document.querySelector('.Third').remove();
        document.querySelector('#Content h2').remove();
        document.querySelector('#Content h2').remove();
        document.querySelector('#Content h2').remove();
        document.querySelector('#Content h2').remove();
        document.querySelector('#Content > p').remove();

        var newElements = document.createElement('section');
        newElements.className = 'gc';
        newElements.innerHTML = '<h2>' + oCacheData.name + '<span class="subtitle">(' + oCacheData.type + ',<span class="gc-city"></span>)</span></h2><ul class="gc-meta"><li class="meta-gc-code">' + oCacheData.code + '</li><li class="meta-gc-difficulty">S: ' + oCacheData.difficulty + '</li><li class="meta-gc-terrain">G: ' + oCacheData.terrain + '</li><li class="meta-gc-size">' + oCacheData.size +'</li><li class="meta-gc-coords">' + oCacheData.coords + '</li></ul>';
        document.getElementById('Content').insertBefore(newElements, document.querySelector('#Content .sortables'));
        var styles = document.createElement('style');
        styles.innerText = '.item { border: 0;} .item .item-header { display: none; } .gc h2 { border-bottom: 1pt solid #000; color: #000; font-size: 16pt; padding: 0 0 5pt 0; margin: 0 0 5pt 0; } .gc h2 span { font-size: 12pt; padding-left: 5pt; } .gc-meta { display: block; overflow: hidden; margin: 0; padding: 0; list-style: none; font-size: 12pt; } .gc-meta li { float: left; font-weight: bold; } .gc-meta .meta-gc-code { float: right; } .gc-meta .meta-gc-difficulty { width: 3cm; } .gc-meta .meta-gc-terrain { width: 3cm; } .gc-meta .meta-gc-size { } .gc-meta .meta-gc-found { float: right; clear: right; } .gc-meta .meta-gc-coords { clear: left; } #Content, .gc-teaser, font, span { font-size: 11pt !important; font-weight: normal; font-family: sans-serif !important; line-height: 1.25; color: #000; } #Content, .gc-teaser { margin-top: 5pt; overflow: hidden; } #Content img { max-height: 90%; } #Content img.left { max-width: 40%; } #Content img.right { max-width: 40%; } .left { float: left; clear: left; margin-right: 5pt; margin-bottom: 5pt; } .right { float: right; clear: right; margin-right: 5pt; margin-bottom: 5pt; } #Content p, #Content span { text-align: left !important; font-size: 12pt !important; }.gc-images { overflow: hidden; display: block; } .gc-img { float: left; width: 48%; padding: 1%; } .gc-img:only-child { width: 100%; } .gc-img img, .gc-img span { display: block; margin: 0 auto; text-align: center; } .gc-img img { max-width: 98%; } font { color: #000; } table { height: auto !important; width: auto !important; } h3 { margin: 10pt 0 0 0; font-weight: bold; font-size: 12pt !important; break-after: avoid-page; }';
        document.querySelector('head').appendChild(styles);
        window.getCity(oCacheData.coords);

        var content = '';
        for(i = 0; i < waypoints.length; i++) {
            content += '<h3>WP ' + waypoints[i].id + (waypoints[i].coords !== '' ? ': <span class="gc-waypoint-coords">' + waypoints[i].coords + '</span>' : '') + '</h3><p>' + waypoints[i].content + '</p>';
        }
        if (content !== '') {
            content = '<h2>Wegpunkte</h2>' + content;
            newElements = document.createElement('div');
            newElements.innerHTML = content;
            document.getElementById('Content').appendChild(newElements, document.querySelector('#Content .sortables'));
        }
    };

    var script = document.createElement('script');
    script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDpqtME_4P5HJu5gJ6k-IFWmQTdNjZgnVw&callback=initMap';
    document.querySelector('head').appendChild(script);

})();