/* 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.3.1
// @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
// @grant GM_deleteValue
// @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,
settings = {},
path = window.location.pathname;
// Don't run the script for iframes
if (window.top == window.self) {
Main();
}
/**
* Router
*/
function Main() {
ReadSettings();
CheckPGCLogin();
if (path.match(/^\/geocache\/.*/) !== null) {
Page_CachePage();
} else if (path.match(/^\/seek\/cache_logbook\.aspx.*/) !== null) {
Page_Logbook();
}
}
function GetSettingsItems() {
var items = {
showVGPS: { title: 'Show Virtual GPS', default: true },
addChallengeCheckers: { title: 'Add challenge checkers', default: true },
makeCopyFriendly: { title: 'Make copy friendly GC-Code and link', default: true },
addPgcMapLinks: { title: 'Add PGC map links', default: true },
addLatestLogs: { title: 'Add latest logs', default: true },
cloneLogsPerType: { title: 'Clone number of logs per type', default: true },
addPGCLocation: { title: 'Add PGC Location', default: true },
addAddress: { title: 'Add reverse geocoded address', default: true },
removeUTM: { title: 'Remove UTM coordinates', default: true },
addPgcFp: { title: 'Add FP from PGC', default: true },
profileStatsLinks: { title: 'Add links to Profile stats', default: true },
tidy: { title: 'Tidy the web a bit', default: true },
collapseDownloads: { title: 'Collapse download links', default: false },
addPgcGalleryLinks: { title: 'Add links to PGC gallery', default: true },
addMapBookmarkListLinks: { title: 'Add links for bookmark lists', default: true },
decryptHints: { title: 'Automatically decrypt hints', default: true }
};
return items;
}
function ReadSettings() {
settings = GM_getValue('settings');
if(typeof(settings) != 'undefined') {
settings = JSON.parse(settings);
} else {
settings = [];
}
var items = GetSettingsItems();
for(var item in items) {
if(typeof(settings[item]) == 'undefined') {
settings[item] = items[item].default;
}
}
}
function SaveSettings() {
GM_deleteValue('disabledFunctions'); // Legacy
settings = {};
var items = GetSettingsItems();
for(var item in items) {
var checked = $('#pgcUserMenuForm input[name="' + item + '"]').is(':checked');
settings[item] = (checked) ? 1 : 0;
}
var json = JSON.stringify(settings);
GM_setValue('settings', json);
$('#pgcUserMenuWarning').css('display', 'inherit');
}
function IsSettingEnabled(setting) {
return settings[setting];
}
/**
* 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';
}
var html = '\
\
\
\
\
\
\
\
PGC: ' + fp + ' FP, ' + fpp + '%, ' + fpw + 'W
'); } // Add PGC location if(IsSettingEnabled('addPGCLocation')) { 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 + '' + gccode + '
' + '►▼Print and Downloads
').insertAfter('#ctl00_ContentBody_CacheInformationTable div.LocationData'); $('#ctl00_divContentMain div.DownloadLinks, #DownloadLinksToggle .arrow.open').hide(); } // Resolve the coordinates into an address if(IsSettingEnabled('addAddress')) { 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 + 'Latest logs: ' + images + '
'); } else { $('#ctl00_ContentBody_size').append('Latest logs: ' + images + '
'); } } } } } }());