/* global $: true */
/* global waitForKeyElements: true */
/* global GM_xmlhttpRequest: true */
/* global GM_getValue: true */
// ==UserScript==
// @name Geocaching.com + Project-GC
// @namespace PGC
// @description Adds links and data to Geocaching.com to make it collaborate with PGC
// @include http://www.geocaching.com/*
// @include https://www.geocaching.com/*
// @version 1.2.12
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
// @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=19641
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @license The MIT License (MIT)
// @downloadURL none
// ==/UserScript==
'use strict';
(function() {
var pgcUrl = 'http://project-gc.com/',
pgcApiUrl = pgcUrl + 'api/gm/v1/',
externalLinkIcon = 'http://maxcdn.project-gc.com/images/external_small.png',
galleryLinkIcon = 'http://maxcdn.project-gc.com/images/pictures_16.png',
mapLinkIcon = 'http://maxcdn.project-gc.com/images/map_app_16.png',
loggedIn = GM_getValue('loggedIn'),
subscription = GM_getValue('subscription'),
pgcUsername = GM_getValue('pgcUsername'),
gccomUsername = GM_getValue('gccomUsername'),
latestLogs = [],
latestLogsAlert = false,
path = window.location.pathname;
// Don't run the script for iframes
if (window.top == window.self) {
Main();
}
/**
* Router
*/
function Main() {
CheckPGCLogin();
if (path.match(/^\/geocache\/.*/) !== null) {
Page_CachePage();
} else if (path.match(/^\/seek\/cache_logbook\.aspx.*/) !== null) {
Page_Logbook();
}
}
/**
* Check that we are logged in at PGC, and that it's with the same username
*/
function CheckPGCLogin() {
gccomUsername = false;
if( $('#ctl00_divSignedIn').length ) {
gccomUsername = $('#ctl00_divSignedIn .li-user-info span').html();
} else if( $('ul.profile-panel-menu').length ) {
gccomUsername = $('ul.profile-panel-menu .li-user-info span:nth-child(2)').text();
}
GM_setValue('gccomUsername', gccomUsername);
GM_xmlhttpRequest({
method: "GET",
url: pgcApiUrl + 'GetMyUsername',
onload: function(response) {
var result = JSON.parse(response.responseText),
html, loggedInContent, subscriptionContent = '';
if (result.status !== 'OK') {
alert(response.responseText);
return false;
}
pgcUsername = result.data.username;
loggedIn = !!result.data.loggedIn;
subscription = !!result.data.subscription;
if (loggedIn === false) {
loggedInContent = 'Not logged in';
} else if (pgcUsername == gccomUsername) {
loggedInContent = '' + pgcUsername + '';
} else {
loggedInContent = '' + pgcUsername + '';
}
if (subscription) {
subscriptionContent = 'Paid membership';
} else {
subscriptionContent = 'Missing membership';
}
html = '\
\
\
\
\
' + loggedInContent + '\
' + subscriptionContent + '\
\
';
if ( $('#ctl00_divSignedIn ul.logged-in-user').length ) { // The default look of the header bar
$('#ctl00_divSignedIn ul.logged-in-user').prepend('
' + html + '
');
} else if( $('ul.profile-panel-menu').length ) { // Special case for https://www.geocaching.com/account/settings/preferences
$('ul.profile-panel-menu').prepend('
' + html + '
');
}
// Save the login value
GM_setValue('loggedIn', loggedIn);
GM_setValue('subscription', subscription);
GM_setValue('pgcUsername', pgcUsername);
},
onerror: function(response) {
alert(response);
return false;
}
});
}
/**
* getGcCodeFromPage
* @return string
*/
function getGcCodeFromPage() {
return $('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoCode').html();
}
/**
* addToVGPS
*/
function addToVGPS() {
var gccode = GM_getValue('gccode'),
listId = $('#comboVGPS').val(),
msg,
url = pgcApiUrl + 'AddToVGPSList?listId=' + listId + '&gccode=' + gccode + '§ionName=GM-script';
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
var result = JSON.parse(response.responseText);
msg = 'Geocache not added to Virtual-GPS :(';
if (result.status === 'OK') {
msg = 'Geocache added to Virtual-GPS!';
}
alert(msg);
return true;
},
onerror: function(response) {
console.log(response);
return false;
}
});
}
/**
* Page_CachePage
*/
function Page_CachePage() {
var gccode = getGcCodeFromPage(),
placedBy = $('#ctl00_ContentBody_mcd1 a').html(),
lastUpdated = $('#ctl00_ContentBody_bottomSection p small time').get(1),
lastFound = $('#ctl00_ContentBody_bottomSection p small time').get(2);
lastUpdated = (lastUpdated) ? lastUpdated.dateTime : false;
lastFound = (lastFound) ? lastFound.dateTime : false;
GM_setValue('gccode', gccode);
// Since everything in the logbook is ajax, we need to wait for the elements
waitForKeyElements('#cache_logs_table tr', Logbook);
// Get cache data from PGC
var url = pgcApiUrl + 'GetCacheDataFromGccode&gccode=' + gccode;
if(lastUpdated)
url += '&lastUpdated=' + lastUpdated;
if(lastFound)
url += '&lastFound=' + lastFound;
if (GM_getValue('subscription')) {
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
var result = JSON.parse(response.responseText),
cacheData = result.data.cacheData,
cacheOwner = result.data.owner,
challengeCheckerTagIds = result.data.challengeCheckerTagIds,
location = [],
fp = 0,
fpp = 0,
fpw = 0;
if(result.status == 'OK' && cacheData !== false) {
// If placed by != owner, show the real owner as well.
if(placedBy != cacheOwner) {
$('#ctl00_ContentBody_mcd1 span.message__owner').before(' (' + cacheOwner + ')');
}
// Append link to Profile Stats for the cache owner
// Need to real cache owner name from PGC since the web only has placed by
$('#ctl00_ContentBody_mcd1 span.message__owner').before('');
// Add FP/FP%/FPW below the current FP
fp = parseInt(+cacheData.favorite_points, 10),
fpp = parseInt(+cacheData.favorite_points_pct, 10),
fpw = parseInt(+cacheData.favorite_points_wilson, 10);
// Add PGC location
if (cacheData.country.length > 0) {
location.push(cacheData.country);
}
if (cacheData.region.length > 0) {
location.push(cacheData.region);
}
if (cacheData.county.length > 0) {
location.push(cacheData.county);
}
location = location.join(' / ');
var gccomLocationData = $('#ctl00_ContentBody_Location').html();
$('#ctl00_ContentBody_Location').html('' + gccomLocationData + ' ' + location + '');
// $('#ctl00_divContentMain div.span-17 div.span-6.right.last div.favorite.right').append('
(' + fp + ' FP, ' + fpp + '%, ' + fpw + 'W)
');
$('#uxFavContainerLink').append('
PGC: ' + fp + ' FP, ' + fpp + '%, ' + fpw + 'W
');
// Add challenge checkers
if(challengeCheckerTagIds.length > 0) {
var html = '';
html += '
';
for(var i = 0 ; i < challengeCheckerTagIds.length ; i++) {
html += '';
}
html += '
';
$('#ctl00_ContentBody_CacheInformationTable').append(html)
}
}
}
});
}
// Tidy the web
$('#ctl00_ContentBody_lnkMessageOwner').html('');
$('#ctl00_divContentMain p.Clear').css('margin', '0');
$('div.Note.PersonalCacheNote').css('margin', '0');
$('h3.CacheDescriptionHeader').remove();
$('#ctl00_ContentBody_EncryptionKey').remove();
// Make it easier to copy the gccode
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel').
html('
' + gccode + '
' +
'
');
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel').css('font-weight', 'inherit').css('margin-right', '40px');
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel div').css('margin', '0px');
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel div p').css('font-weight', 'bold');
// Add PGC Map links
var gccomUsername = GM_getValue('gccomUsername'),
mapUrl = pgcUrl + 'Maps/mapcompare/?profile_name=' + gccomUsername +
'&nonefound=on&ownfound=on&location=' + latitude + ',' + longitude +
'&max_distance=5&submit=Filter';
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel').append(
'
'
);
// Remove the UTM coordinates
// $('#ctl00_ContentBody_CacheInformationTable div.LocationData div.span-9 p.NoBottomSpacing br').remove();
$('#ctl00_ContentBody_LocationSubPanel').html();
// Remove ads
// PGC can't really do this officially
// $('#ctl00_ContentBody_uxBanManWidget').remove();
// Remove disclaimer
// PGC can't really do this officially
// $('#ctl00_divContentMain div.span-17 div.Note.Disclaimer').remove();
// Collapse download links
// http://www.w3schools.com/charsets/ref_utf_geometric.asp (x25BA, x25BC)
$('
►▼Print and Downloads
').insertAfter('#ctl00_ContentBody_CacheInformationTable div.LocationData');
$('#ctl00_divContentMain div.DownloadLinks, #DownloadLinksToggle .arrow.open').hide();
// Resolve the coordinates into an address
var coordinates = $('#ctl00_ContentBody_lnkConversions').attr('href'),
latitude = coordinates.replace(/.*lat=([^&]*)&lon=.*/, "$1"),
longitude = coordinates.replace(/.*&lon=([^&]*)&.*/, "$1"),
url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=false';
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
var result = JSON.parse(response.responseText);
if (result.status !== 'OK') {
return false;
}
var formattedAddress = result.results[0].formatted_address;
$('#ctl00_ContentBody_LocationSubPanel').html(formattedAddress + ' ');
}
});
// Add number of finds to the top
$('#ctl00_ContentBody_CacheInformationTable').before('