// ==UserScript==
/* globals jQuery, $, L, waitForKeyElements, cloneInto */
// @name Delorme Grid Multi-state Overlay
// @author rragan (derived from cachetur Assistant code)
// @version 1.0.0.6
// @description Companion script for geocaching.com
// @include https://www.geocaching.com/play/map*
// @include http://www.geocaching.com/play/map*
// @include https://www.geocaching.com/map/*
// @include http://www.geocaching.com/map/*
// @include https://www.geocaching.com/play/map*
// @include http://www.geocaching.com/play/map*
// @include https://www.geocaching.com/geocache/*
// @include http://www.geocaching.com/geocache/*
// @include https://www.geocaching.com/seek/cache_details.aspx*
// @include https://www.geocaching.com/plan/lists/BM*
// @include http://www.geocaching.com/play/geotours/*
// @include https://www.geocaching.com/play/geotours/*
// @include http://project-gc.com/*
// @include https://project-gc.com/*
// @connect self
// @connect rragan.atwebpages.com
// @connect raw.githubusercontent.com
// @grant GM_xmlhttpRequest
// @grant GM_info
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_openInTab
// @grant GM_addStyle
// @grant GM_addStyle
// @grant unsafeWindow
// @run-at document-end
// @copyright 2021+, rragan
// @require https://code.jquery.com/jquery-latest.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @updateURL https://github.com/rragan/DeLormeGridOverlay/raw/main/multiState.meta.js
// @downloadURL https://github.com/rragan/DeLormeGridOverlay/raw/main/multiState.user.js
// ==/UserScript==
function wait4containers() {
let mapNode = document.querySelector("div.map-container, .leaflet-container");
if (mapNode == null) {
// the node doesn't exist yet, wait and try again
window.setTimeout(wait4containers, 300);
return;
}
//Handy tool to convert GSAK data to JSON grid form: https://www.convertcsv.com/csv-to-json.htm Use CSV to JSON array
// Generic State DeLorme Grid coordinates
this.$ = this.jQuery = jQuery.noConflict(true);
let _dgPage = "unknown";
let _gridLayer = [];
let _pagenumberLayer = [];
let _initialized = false;
let _pageNumbersShown = false;
let _dgGridColor = "#CC00FF";
let _dgGridArchivedColor = "#000";
let _dgWhichGrid = "";
let _stateCode = "";
let _newView = "";
let _changedState;
let options = '';
// # Sign icon
let numberImg = '';
console.log("Starting Delorme Grid " + GM_info.script.version);
$("#gc-header, #GCHeader").show();
let pathname = window.location.pathname;
let domain = document.domain;
if (domain === "www.geocaching.com") {
if (pathname.indexOf("/play/map") > -1) _dgPage = "gc_map_new";
else if (pathname.indexOf("/map/") > -1) _dgPage = "gc_map";
}
console.log("Running in " + _dgPage + " mode");
if (_dgPage === "gc_map_new") {
var checkExist = setInterval(function() {
if ($('.mapboxgl-canvas').length) {
console.log("Exists!");
clearInterval(checkExist);
}
}, 100); // check every 100ms
// See if Cachetur Assistant has got the map object. If so, just use it.
if (unsafeWindow.cacheturGCMap) {
console.log("MULTISTATE was BEAT TO MAP BY CTA");
unsafeWindow.delormeGCMap = unsafeWindow.cacheturGCMap;
} else if (unsafeWindow.gcMap) { // If anyone has set the object in a shared place, use it
console.log("MULTISTATE sees MAP from other extension");
unsafeWindow.delormeGCMap = unsafeWindow.gcMap;
} else {
console.log("DeLorme Doing dirty trick to take over Geocaching.com's leaflet object");
let originalLMap = L.Map;
L.Map = function(div, settings) {
unsafeWindow.delormeGCMap = new originalLMap(div, settings);
L.Map = originalLMap;
console.log("MULTISTATE got MAP");
unsafeWindow.gcMap = unsafeWindow.delormeGCMap; // Save a copy in a shared place for others
return unsafeWindow.delormeGCMap;
};
}
}
$(document).ready(function() {
ctInit();
});
// Init the menu and data needed
function ctInit() {
console.log("Initializing Delorme Grid");
// Build state list menu
dgDataCall("stateCodesList", function(data) {
var codes = data.list;
let storedState = GM_getValue("dg_selected_state", 0);
var selected = "";
var opt = "";
if (data.list.length > 0) {
codes.forEach(function(item) {
if (storedState === item.code) {
opt = '';
} else {
opt = '';
}
options = options + opt;
});
}
if (!_initialized) {
ctPrependToHeader('