forked from DreiDe/komootGPXport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·334 lines (293 loc) · 12.1 KB
/
main.js
File metadata and controls
executable file
·334 lines (293 loc) · 12.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
console.log("komootGPXport activated");
// === Utility functions ===
const jsonToGpx = (coords) => {
let gpx =
`<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx version="1.1" creator="komootGPXport" xmlns="http://www.topografix.com/GPX/1/1">
<metadata></metadata>
<rte>
${coords.map((coord) => {
return `<rtept lat="${coord.lat}" lon="${coord.lng}"><ele>${coord.alt}</ele></rtept>`
}).join('\n')
}
</rte>
</gpx>`;
return gpx;
}
const downloadGpx = (filename, text) => {
let elem = document.createElement('a');
elem.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
elem.setAttribute('download', filename);
elem.style.display = 'none';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
// === Name helpers ===
function sanitizeFilename(name) {
return name.replace(/[<>:"/\\|?*]+/g, '').replace(/\s+/g, ' ').trim();
}
function getParsedPageData() {
// Cache is keyed by URL so SPA navigations always get fresh data
if (getParsedPageData._cache && getParsedPageData._url === location.href) {
return getParsedPageData._cache;
}
getParsedPageData._cache = null;
getParsedPageData._url = location.href;
const scripts = document.querySelectorAll('script');
for (let script of scripts) {
const content = script.textContent || script.innerHTML;
if (content.includes('kmtBoot.setProps(')) {
const match = content.match(/kmtBoot\.setProps\("(.+)"\)/);
if (match) {
try {
const unescapedJson = match[1].replace(/\\"/g, '"').replace(/\\\\/g, '\\');
getParsedPageData._cache = JSON.parse(unescapedJson);
return getParsedPageData._cache;
} catch (e) {}
}
}
}
return null;
}
function getTourName() {
const data = getParsedPageData();
return data?.page?._embedded?.tour?.name || 'route';
}
function getCollectionName() {
const data = getParsedPageData();
return data?.page?._embedded?.collectionHal?.name?.trim() || 'collection';
}
function getCollectionLegName(tourId) {
const data = getParsedPageData();
const items = data?.page?._embedded?.collectionHal?._embedded?.compilation?._embedded?.items;
if (!items) return null;
const leg = items.find(i => String(i.id) === String(tourId));
return leg?.name || null;
}
// === Coordinate fetching (upstream method first, then fallback) ===
function getCoordsFromScriptTags() {
// Upstream method: parse kmtBoot.setProps() from <script> tags
const rawData = getParsedPageData();
if (!rawData) return null;
const coordinates = rawData.page?._embedded?.tour?._embedded?.coordinates?.items;
return (coordinates && coordinates.length > 0) ? coordinates : null;
}
function getCoordsFromGetProps() {
// Fallback: use kmtBoot.getProps() API
try {
const page = kmtBoot.getProps().page;
if (!page) return { coords: null, tourLink: null };
const coords = page.linksEmbedded?.tour?.linksEmbedded?.coordinates?.attributes?.items;
const tourLink = page.links?.tour?.href;
return { coords: coords && coords.length > 0 ? coords : null, tourLink };
} catch (e) {
console.error('komootGPXport: kmtBoot.getProps() failed:', e);
return { coords: null, tourLink: null };
}
}
function fetchCoordsFromTourLink(tourLink) {
// Last resort: fetch coordinates via API links
return fetch(tourLink)
.then(response => {
if (!response.ok) throw new Error(`HTTP error ${response.status}`);
return response.json();
})
.then(tour_data => {
const coordinates_link = tour_data._links.coordinates.href;
return fetch(coordinates_link);
})
.then(response => {
if (!response.ok) throw new Error(`HTTP error ${response.status}`);
return response.json();
})
.then(coordinates_data => coordinates_data.items);
}
function downloader() {
const filename = sanitizeFilename(getTourName()) + '.gpx';
// Method 1: Parse <script> tags (upstream approach)
const scriptCoords = getCoordsFromScriptTags();
if (scriptCoords) {
const gpx = jsonToGpx(scriptCoords);
downloadGpx(filename, gpx);
return;
}
// Method 2: kmtBoot.getProps() fallback
const { coords, tourLink } = getCoordsFromGetProps();
if (coords) {
const gpx = jsonToGpx(coords);
downloadGpx(filename, gpx);
return;
}
// Method 3: Fetch from tour API link
if (tourLink) {
fetchCoordsFromTourLink(tourLink)
.then(items => {
const gpx = jsonToGpx(items);
downloadGpx(filename, gpx);
})
.catch(err => {
console.error('komootGPXport: Failed to fetch coordinates:', err);
alert('There was an error reading the points of your route. If this keeps happening feel free to open an issue.');
});
return;
}
alert('There was an error reading the points of your route. If this keeps happening feel free to open an issue.');
}
// === Planner page: add button next to "Save route" ===
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function addPlannerButton() {
waitForElm("[data-test-id=p_tour_save]").then((saveBtn) => {
if (document.querySelector("#download-gpx"))
return;
const downloadBtn = saveBtn.cloneNode(true);
downloadBtn.id = 'download-gpx';
downloadBtn.removeAttribute('data-test-id');
downloadBtn.removeAttribute('href');
downloadBtn.style.cursor = 'pointer';
// Replace icon with download icon
const svg = downloadBtn.querySelector('svg');
if (svg) {
svg.innerHTML = '<path d="M10 2.5v10m0 0L6.25 8.75M10 12.5l3.75-3.75M3.33 14.17v.83a2.5 2.5 0 002.5 2.5h8.34a2.5 2.5 0 002.5-2.5v-.83" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>';
}
// Replace text
const textEl = downloadBtn.querySelector('p');
if (textEl) textEl.textContent = 'Download GPX (free)';
downloadBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
downloader();
});
// Match spacing with the cross button on the right
saveBtn.parentElement.style.gap = '8px';
saveBtn.parentElement.insertBefore(downloadBtn, saveBtn);
});
}
// === Tour/collection page: add button after "Send to Phone" ===
function makeDownloadButton(templateBtn, id, clickHandler) {
const downloadBtn = templateBtn.cloneNode(true);
downloadBtn.id = id;
downloadBtn.removeAttribute('href');
downloadBtn.setAttribute('role', 'button');
// Prefer the Navigate button's class (green) over Save/Send-to-Phone (brown)
const navigateBtn = document.querySelector('a[role="button"][aria-label="Navigate"]');
if (navigateBtn && navigateBtn !== templateBtn) downloadBtn.className = navigateBtn.className;
downloadBtn.style.cursor = 'pointer';
const textEl = downloadBtn.querySelector('p');
if (textEl) textEl.textContent = 'Download GPX (free)';
const svg = downloadBtn.querySelector('svg');
if (svg) {
svg.innerHTML = '<path d="M10 2v11m0 0l-3.5-3.5M10 13l3.5-3.5M3 15v1a2 2 0 002 2h10a2 2 0 002-2v-1" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>';
}
downloadBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
clickHandler();
});
return downloadBtn;
}
function getTourIdFromContext(sendToPhoneLink) {
// Walk up to find a nearby tour link and extract the tour ID
let el = sendToPhoneLink.parentElement;
for (let i = 0; i < 5; i++) {
if (!el) break;
el = el.parentElement;
const tourLink = el.querySelector('a[href*="/tour/"]');
if (tourLink) {
const match = tourLink.href.match(/\/tour\/(\d+)/);
if (match) return match[1];
}
}
return null;
}
function downloadByTourId(tourId) {
const colName = sanitizeFilename(getCollectionName());
const legName = getCollectionLegName(tourId);
const filename = legName
? sanitizeFilename(colName + ' - ' + legName) + '.gpx'
: colName + '.gpx';
const coordsUrl = `https://api.komoot.de/v007/tours/${tourId}/coordinates`;
fetch(coordsUrl)
.then(response => {
if (!response.ok) throw new Error(`HTTP error ${response.status}`);
return response.json();
})
.then(data => {
if (!data.items || data.items.length === 0) throw new Error('No coordinates found');
const gpx = jsonToGpx(data.items);
downloadGpx(filename, gpx);
})
.catch(err => {
console.error('komootGPXport: Failed to fetch coordinates:', err);
alert('There was an error reading the points of your route. If this keeps happening feel free to open an issue.');
});
}
function addTourButtons() {
const isTourPage = /\/(tour|smarttour)\//.test(window.location.pathname);
const isCollectionPage = /\/collection\//.test(window.location.pathname);
if (!isTourPage && !isCollectionPage) return;
if (isTourPage) {
if (document.querySelector('#download-gpx-tour')) return;
const navigateBtn = document.querySelector('a[role="button"][aria-label="Navigate"]');
const saveBtn = document.querySelector('a[role="button"][aria-label="Save"]');
const templateBtn = navigateBtn || saveBtn;
if (!templateBtn) return;
const container = templateBtn.parentElement;
if (!container) return;
// Insert between Navigate and Save to match the expected button order
const anchor = saveBtn && saveBtn.parentElement === container ? saveBtn : null;
container.insertBefore(makeDownloadButton(templateBtn, 'download-gpx-tour', downloader), anchor);
return;
}
// Collection page: one download button per tour card.
// Each card has id="tour_XXXXXX" and contains Save/Navigate buttons as stable anchors.
let idx = 0;
document.querySelectorAll('[data-test-id="tour-card"]').forEach(card => {
const tourId = card.id?.replace('tour_', '');
if (!/^\d+$/.test(tourId)) return;
const btnId = idx === 0 ? 'download-gpx-tour' : `download-gpx-tour-${idx}`;
if (document.getElementById(btnId)) { idx++; return; }
// Use the Save button as template (present on both Chrome and Firefox)
const saveBtn = card.querySelector('a[role="button"][aria-label="Save"]');
const navigateBtn = card.querySelector('a[role="button"][aria-label="Navigate with device"]') ||
card.querySelector('a[role="button"][aria-label="Navigate"]');
const templateBtn = saveBtn || navigateBtn;
if (!templateBtn) return;
const container = templateBtn.parentElement;
if (!container) return;
// Insert before Save to place it first in the action row
container.insertBefore(
makeDownloadButton(templateBtn, btnId, () => downloadByTourId(tourId)),
saveBtn || null
);
idx++;
});
}
// === Init: observe DOM and add buttons as appropriate ===
const observer = new MutationObserver(() => {
addPlannerButton();
addTourButtons();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Also run once immediately
addPlannerButton();
addTourButtons();