forked from magma1447/greasemonkey-geocaching-projectgc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greasemonkey-geocaching-projectgc.user.js
316 lines (249 loc) · 10.5 KB
/
greasemonkey-geocaching-projectgc.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// ==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
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// Global variables
pgcApiUrl = 'http://project-gc.com/api/gm/v1/';
externalLinkIcon = 'http://maxcdn.project-gc.com/images/external_small.png';
loggedIn = GM_getValue('loggedIn');
subscription = GM_getValue('subscription');
// -Global variables
// Don't run the script for iframes
if(window.top == window.self) {
Main();
}
function Main() {
// Always
CheckPGCLogin();
// Router
var path = window.location.pathname;
if(path.match(/^\/geocache\/.*/) != null) {
CachePage();
} else if(path.match(/^\/seek\/cache_logbook\.aspx.*/) != null) {
Logbook();
}
}
// Check that we are logged in at PGC, and that it's with the same username
function CheckPGCLogin() {
var gccomUsername = $('#ctl00_divSignedIn p.SignedInText strong a').html();
GM_xmlhttpRequest({
method: "GET",
url: pgcApiUrl + 'GetMyUsername',
onload: function(response) {
var ret = JSON.parse(response.responseText);
var pgcUsername = ret['data']['username'];
loggedIn = ret['data']['loggedIn'];
subscription = ret['data']['subscription'];
var text = '<div class="ProfileWidget" style="margin-right: 10px;"><div id="pgc_signedIn"><a href="http://project-gc.com/"><img class="Avatar NoBottomSpacing" style="width: auto;" src="http://maxcdn.project-gc.com/images/logo_gc_4.png" title="Project-GC"></a>';
if(loggedIn === false) {
text = text + '<p class="SignedInText">Not logged in</p>';
} else if(pgcUsername == gccomUsername) {
text = text + '<p class="SignedInText">Logged in as: <strong>' + pgcUsername + '</strong></p>';
} else {
text = text + '<p class="SignedInText" style="color: red">Logged in as: <strong>' + pgcUsername + '</strong></p>';
}
if(subscription) {
text = text + '<p>Subscription: Yes</p>';
} else {
text = text + '<p>Subscription: No</p>';
}
text = text + '</div></div>';
$('#ctl00_siteHeader div.container').append(text);
// Save the login value
GM_setValue('loggedIn', loggedIn);
GM_setValue('subscription', subscription);
}
});
}
function CachePage() {
var gccode = $('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoCode').html();
latestLogs = [];
// Append link to Profile Stats after the cache owners name
var cacheOwnerDiv = $('#ctl00_ContentBody_mcd1');
var placedBy = $('#ctl00_ContentBody_mcd1 a').html();
cacheOwnerDiv.append('<a href="http://project-gc.com/ProfileStats/' + encodeURIComponent(placedBy) + '"><img src="' + externalLinkIcon + '" title="PGC Profile Stats"></a>');
// Append links to Profile Stats for every geocacher who has logged the cache as well
// Though this is ajax, so we need some magic
waitForKeyElements ('#cache_logs_table tr', CachePage_Logbook);
// Get cache data from PGC
if(subscription) {
GM_xmlhttpRequest({
method: "GET",
url: pgcApiUrl + 'GetCacheDataGccode?gccode=' + gccode,
onload: function(response) {
var ret = JSON.parse(response.responseText);
var cacheData = ret['data'];
// Add FP/FP%/FPW below the current FP
var fp = parseInt(cacheData['favorite_points']);
var fpp = parseInt(cacheData['favorite_points_pct']);
var fpw = parseInt(cacheData['favorite_points_wilson']);
$('#ctl00_divContentMain div.span-17 div.span-6.right.last div.favorite.right').append('<p>' + fp + ' FP, ' + fpp + '%, ' + fpw + 'W</p>');
// Add PGC location
var 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(' / ');
$('#ctl00_ContentBody_CacheInformationTable div.LocationData div.span-7').append('<span>' + location + '</span>');
}
});
}
// Make it easier to copy the gccode
$('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoLinkPanel').html('<p style="font-size: 125%; margin-bottom: 0px;">' + gccode + '</p><input type="text" value="http://coord.info/' + gccode + '" onClick="this.setSelectionRange(0, this.value.length)">');
// Remove the UTM coordinates
// $('#ctl00_ContentBody_CacheInformationTable div.LocationData div.span-9 p.NoBottomSpacing br').remove();
$('#ctl00_ContentBody_LocationSubPanel').remove();
// Remove ads
$('#ctl00_ContentBody_uxBanManWidget').remove();
// Remove disclaimer
$('#ctl00_divContentMain div.span-17 div.Note.Disclaimer').remove();
// Hide download links
$('<p style="cursor: pointer;" onclick="$(\'#ctl00_divContentMain div.DownloadLinks\').toggle();"><span class="arrow">▼</span>Print and Downloads</p>').insertAfter('#ctl00_ContentBody_CacheInformationTable div.LocationData');
$('#ctl00_divContentMain div.DownloadLinks').hide();
// Turn the coordinates into an address
var coordinates = $('#ctl00_ContentBody_lnkConversions').attr('href');
var latitude = coordinates.replace(/.*lat=([^&]*)&lon=.*/, "$1");
var longitude = coordinates.replace(/.*&lon=([^&]*)&.*/, "$1");
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=false';
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
var ret = JSON.parse(response.responseText);
var formattedAddress = ret['results'][0]['formatted_address'];
$('<br><span>' + formattedAddress + '</span>').insertAfter('#uxLatLonLink');
}
});
// Add number of finds to the top
// $('#ctl00_ContentBody_lblFindCounts').find('img').each(function() {
// if($(this).attr('src') == '/images/logtypes/2.png') { // Found
// }
// });
$('#cacheDetails').append('<div>' + $('#ctl00_ContentBody_lblFindCounts').html() + '</div>');
}
function CachePage_Logbook(jNode) {
// Add Profile stats link after each user
var profileName = $(jNode).find('p.logOwnerProfileName strong a').html();
if(profileName != null) {
var profileName = $(jNode).find('p.logOwnerProfileName strong a').append('<a href="http://project-gc.com/ProfileStats/' + encodeURIComponent(profileName) + '"><img src="' + externalLinkIcon + '" title="PGC Profile Stats"></a>');
}
// Save to latest logs
if(latestLogs.length < 5) {
var logType = $(jNode).find('div.LogType strong img').attr('src');
if(logType == '/images/logtypes/3.png') { // dnf
latestLogs.push('<img src="' + logType + '">');
} else if(logType == '/images/logtypes/2.png') { // found
latestLogs.push('<img src="' + logType + '">');
}
// Show them
if(latestLogs.length == 5) {
var images = latestLogs.join('');
// $('#ctl00_ContentBody_diffTerr').append('<dl><dt> Latest logs:</dt><dd><span>' + images + '</span></dd></dl>');
$('#ctl00_ContentBody_size p').addClass('NoBottomSpacing');
$('#ctl00_ContentBody_size').append('<p class="AlignCenter NoBottomSpacing">Latest logs:<span>' + images + '</span></p>');
}
}
}
function Logbook() {
waitForKeyElements ('#AllLogs tr', CachePage_Logbook);
}
function Logbook_Logbook(jNode) {
CachePage_Logbook(jNode);
}
/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
that detects and handles AJAXed content.
Usage example:
waitForKeyElements (
"div.comments"
, commentCallbackFunction
);
//--- Page-specific function to do what we want when the node is found.
function commentCallbackFunction (jNode) {
jNode.text ("This comment changed by waitForKeyElements().");
}
IMPORTANT: This function requires your script to have loaded jQuery.
*/
function waitForKeyElements (
selectorTxt, /* Required: The jQuery selector string that
specifies the desired element(s).
*/
actionFunction, /* Required: The code to run when elements are
found. It is passed a jNode to the matched
element.
*/
bWaitOnce, /* Optional: If false, will continue to scan for
new elements even after the first match is
found.
*/
iframeSelector /* Optional: If set, identifies the iframe to
search.
*/
) {
var targetNodes, btargetsFound;
if (typeof iframeSelector == "undefined")
targetNodes = $(selectorTxt);
else
targetNodes = $(iframeSelector).contents ()
.find (selectorTxt);
if (targetNodes && targetNodes.length > 0) {
btargetsFound = true;
/*--- Found target node(s). Go through each and act if they
are new.
*/
targetNodes.each ( function () {
var jThis = $(this);
var alreadyFound = jThis.data ('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
var cancelFound = actionFunction (jThis);
if (cancelFound)
btargetsFound = false;
else
jThis.data ('alreadyFound', true);
}
} );
}
else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace (/[^\w]/g, "_");
var timeControl = controlObj [controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval (timeControl);
delete controlObj [controlKey]
}
else {
//--- Set a timer, if needed.
if ( ! timeControl) {
timeControl = setInterval ( function () {
waitForKeyElements ( selectorTxt,
actionFunction,
bWaitOnce,
iframeSelector
);
},
300
);
controlObj [controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}