forked from 3meraldK/earthmc-dynmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userscript.user.js
274 lines (246 loc) · 11.6 KB
/
userscript.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
// ==UserScript==
// @name EarthMC Dynmap+
// @version 1.13
// @description Extension to enrich the EarthMC Dynmap experience
// @author 3meraldK
// @match https://earthmc.net/map/*
// @iconURL https://raw.githubusercontent.com/3meraldK/earthmc-dynmap/main/icon.png
// ==/UserScript==
const repo = 'https://raw.githubusercontent.com/3meraldK/earthmc-dynmap',
endpointsURL = `${repo}/main/endpoints.json`,
playerLabelClass = 'span.playerNameNoHealth',
sidebarClass = 'emcdynmap coord-control leaflet-control';
function injectMainScript() {
const mainScript = document.createElement('script');
mainScript.src = 'https://cdn.jsdelivr.net/gh/3meraldK/earthmc-dynmap@main/main.js';
mainScript.onload = function () { this.remove(); };
(document.head || document.documentElement).appendChild(mainScript);
(document.head || document.documentElement).insertAdjacentHTML('beforeend',
'<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/3meraldK/earthmc-dynmap@main/style.css" />')
}
function loadDarkMode() {
document.head.insertAdjacentHTML('beforeend',
`<style id="dark-mode">
.leaflet-control,
.leaflet-popup-content-wrapper,
.leaflet-control-zoom > a,
.leaflet-control button,
.leaflet-control input,
.leaflet-popup-tip {
background: #111 !important;
color: #bbb !important;
box-shadow: inset 0 0 0 1px #bbb !important;
}
</style>`
);
}
function waitForHTMLelems(selector1, selector2) {
return new Promise(resolve => {
if (document.querySelector(selector1) && document.querySelector(selector2)) {
return resolve([document.querySelector(selector1), document.querySelector(selector2)]);
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector1) && document.querySelector(selector2)) {
resolve([document.querySelector(selector1), document.querySelector(selector2)]);
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true });
});
}
function sendAlert(message, error = null) {
const button = `<button onclick="document.getElementById('error-label').remove()">OK</button>`;
document.body.insertAdjacentHTML('beforeend', `<span id="error-label">${message}<br>${button}</span>`);
if (error) console.log(message + `\n${error}`);
}
async function search(searchWhat) {
const inputValue = document.getElementById(`${searchWhat}-search-input`).value;
const inputLength = inputValue.length;
if (!inputLength) return;
const map = location.href.split('/')[4];
const markersURL = await fetch(endpointsURL)
.then(resp => resp.json())
.then(json => json.markers.replace('{map}', map))
.catch(() => {
sendAlert(`Couldn't get needed data, try again later.`, `Attempted to fetch data from: ${endpointsURL}`);
return;
});
const markers = await fetch(markersURL)
.then(resp => resp.json())
.then(json => json.sets['townyPlugin.markerset'].areas)
.catch(() => {
sendAlert(`Couldn't get needed data, try again later.`, `Attempted to fetch data from: ${endpointsURL}`);
return;
});
const town = Object.values(markers).find(town => {
const {desc, label} = town;
if (searchWhat === 'nation') {
const nation = getNation(desc);
return nation.toLowerCase() === inputValue.toLowerCase() && desc.includes('capital: true');
} else return label.toLowerCase() === inputValue.toLowerCase();
});
if (town) {
const avgX = (Math.min(...town.x) + Math.max(...town.x)) / 2;
const avgZ = (Math.min(...town.z) + Math.max(...town.z)) / 2;
location.href = `https://earthmc.net/map/${map}/?zoom=6&x=${avgX}&z=${avgZ}`;
return;
}
sendAlert(`Couldn't find searched ${searchWhat}.`);
}
function getNation(desc) {
const nationWikiRegex = /(?<=nofollow">)[^<]+(?=<\/a>\))/;
const nationRegex = /(?<=\()[^)]*/;
if (desc.includes('(<a ')) return desc.match(nationWikiRegex)[0];
else return desc.match(nationRegex)[0];
}
function addDarkMode() {
if (localStorage.getItem('emcdynmap-darkmode')) loadDarkMode();
// Dark mode button & listener
document.querySelector('.leaflet-top.leaflet-left').insertAdjacentHTML('beforeend', `<div class="${sidebarClass}"
style="display: grid"><button id="dark-mode-button">Dark mode</button></div>`);
document.getElementById('dark-mode-button').addEventListener("click", function () {
if (localStorage.getItem('emcdynmap-darkmode')) {
localStorage.removeItem('emcdynmap-darkmode');
document.getElementById('dark-mode').remove();
} else {
localStorage.setItem('emcdynmap-darkmode', 1);
loadDarkMode();
}
});
}
function addMainMenu() {
// Main box
document.querySelector('.leaflet-top.leaflet-left').insertAdjacentHTML('beforeend', `<div class="${sidebarClass}"
style="display: grid"></div>`);
const menu = document.querySelector('.emcdynmap');
// Switch map mode button
const mapModeHint = 'Switch between meganations, alliances and default map modes.';
menu.insertAdjacentHTML('beforeend', `<div><abbr title="${mapModeHint}">(?)</abbr>
<button id="switch-button">Switch map mode</button></div>`);
document.getElementById('switch-button').addEventListener('click', () => {
let mapMode = sessionStorage.getItem('emcdynmap-mapMode') || 'meganations';
mapMode = (mapMode === 'meganations') ? 'alliances' : (mapMode === 'alliances') ? 'default' : 'meganations';
sessionStorage.setItem('emcdynmap-date', '0');
sessionStorage.setItem('emcdynmap-mapMode', mapMode);
location.reload();
});
// The archive input
const minDate = (location.href.includes('nova')) ? '2018-12-18' : '2022-05-01',
maxDate = new Date().toLocaleDateString('en-ca'),
archiveHint = 'View old claims & stats, does not include terrain. Switch map mode to leave this mode.';
menu.insertAdjacentHTML('beforeend', `<div><abbr title="${archiveHint}">(?)</abbr>
<input id="date" type="date" style="width: 120px" min="${minDate}" max="${maxDate}"></div>`);
const dateInput = document.getElementById('date');
dateInput.addEventListener('change', () => {
const unformattedDate = dateInput.valueAsDate.toLocaleDateString('sv').replaceAll('-', '');
sessionStorage.setItem('emcdynmap-date', unformattedDate);
sessionStorage.setItem('emcdynmap-mapMode', 'archive');
location.reload();
});
// Search town/nation
const searchNationHint = 'Search nations by name. It will set your view on its capital.';
menu.insertAdjacentHTML('beforeend', `<div><abbr title="Search cities by name.">(?)</abbr>
<input id="town-search-input" placeholder="London"><button id="town-search-button">Search</button></div>`);
menu.insertAdjacentHTML('beforeend', `<div><abbr title="${searchNationHint}">(?)</abbr>
<input id="nation-search-input" placeholder="Germany"><button id="nation-search-button">Search</button></div>`);
['town', 'nation'].forEach(elem => {
const button = document.getElementById(`${elem}-search-button`);
const input = document.getElementById(`${elem}-search-input`);
button.addEventListener('click', () => search(elem));
input.addEventListener('keyup', (event) => { if (event.key === 'Enter') search(elem) });
})
// Current map mode and date
const date = sessionStorage.getItem('emcdynmap-date') || '0',
formattedDate = date.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3'),
dateInfo = (date !== '0') ? `, date: ${formattedDate}` : '',
mapMode = sessionStorage.getItem('emcdynmap-mapMode') || 'meganations';
menu.insertAdjacentHTML('beforeend', `<div>Showing: ${mapMode}${dateInfo}</div>`);
}
function checkForUpdates() {
fetch(`${repo}/main/manifest.json`)
.then(response => response.json())
.then(manifest => {
const localVersion = chrome.runtime.getManifest().version;
if (localVersion === manifest.version) return;
const releaseURL = `https://github.com/3meraldK/earthmc-dynmap/releases/latest`;
const updateMsg = `<div class="${sidebarClass}"><a href="${releaseURL}" target="_blank">
Extension update available</a><br>(from ${localVersion} to ${manifest.version})</div>`;
document.querySelector('.leaflet-top.leaflet-left').insertAdjacentHTML('beforeend', updateMsg);
}).catch(() => console.log(`EarthMC Dynmap+ couldn't get latest release version.`));
}
function addPlayerLookup() {
const playerLabels = document.querySelector('.leaflet-pane');
// Hovering over usernames
playerLabels.addEventListener('mouseover', function (event) {
if (event.target.matches(playerLabelClass)) event.target.style = 'background: rgba(55, 55, 55, 0.6); cursor: default';
})
playerLabels.addEventListener('mouseout', function (event) {
if (event.target.matches(playerLabelClass)) event.target.style.background = 'rgba(0, 0, 0, 0.6)';
})
// Clicking usernames
const closeButton = `<button id="player-info-close"
onclick="document.getElementById('player-info').remove()">X</button>`;
playerLabels.addEventListener('click', async function (event) {
if (event.target.matches(playerLabelClass)) {
let playerLookupElem = document.getElementById('player-info');
if (playerLookupElem) playerLookupElem.remove();
const playerLookupHTML = `<div class="${sidebarClass}" id="player-info">Loading...${closeButton}</div>`,
map = location.href.split('/')[4],
player = event.target.innerText,
faceImg = `https://earthmc.net/map/aurora/standalone/MySQL_markers.php?marker=faces/16x16/${player}.png`;
const beginning = `<img src="${faceImg}" id="player-face"/><br><b>${player}</b><br>`;
let playerStatus = `Townless`;
document.querySelector('.leaflet-top.leaflet-left').insertAdjacentHTML('beforeend', playerLookupHTML)
playerLookupElem = document.getElementById('player-info');
const onlinePlayersURL = await fetch(endpointsURL)
.then(resp => resp.json())
.then(json => json.onlineplayers.replace('{map}', map).replace('{player}', player))
.catch(() => {
sendAlert(`Couldn't get list of players, try again later.`, `Attempted to fetch data from: ${endpointsURL}`);
return;
});
const playerData = await fetch(onlinePlayersURL)
.then(resp => resp.json())
.catch(() => {
playerLookupElem.innerHTML = `${beginning}Couldn't get player info, try again later.${closeButton}`;
return;
});
if (playerData.town) playerStatus =
`Town: ${playerData.town}<br>
Nation: ${playerData.nation}<br>
Rank: ${playerData.rank}`;
playerLookupElem.innerHTML = `${beginning}${playerStatus}${closeButton}`;
}
});
}
function addLegend() {
const hint = `<abbr title="Land shared between two or more alliances/meganations.">(?)</abbr>`,
legend =
`<div class="box" id="nationless"></div> Nation-less town<br>
<div class="box" id="ruin"></div> Ruined town<br>
<div class="box" id="default"></div> Default nation<br>
<div class="box" id="premium"></div> Custom-colored nation<br>
<div class="box" id="meganation"></div> Mega-nation<br>
<div class="box" id="alliance"></div> Alliance<br>
<div class="box" id="condominium"></div> ${hint} Condominium`;
document.querySelector('.leaflet-top.leaflet-left').insertAdjacentHTML('beforeend', `<div class="${sidebarClass}"
style="display: grid"><button id="collapsible">Open legend</button><div id="collapsible-content">${legend}</div></div>`);
document.getElementById('collapsible').addEventListener("click", function () {
this.classList.toggle("active");
const content = this.nextElementSibling;
content.style.display = (content.style.display === "block") ? "none" : "block";
});
}
(function() {
'use strict';
injectMainScript();
waitForHTMLelems('.compass', '.largeclock').then(() => {
document.querySelector('.compass').remove();
document.querySelector('.largeclock').remove();
addMainMenu()
addLegend()
addPlayerLookup()
addDarkMode()
checkForUpdates()
});
})();