From 1fdd4e71b509a9a0cd93f35adbf6e4c1740fe260 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Fri, 16 Feb 2024 11:14:32 +0100 Subject: [PATCH] departure point can be null --- background.js | 72 ++++++++++++++++++++++++++++----------------------- manifest.json | 14 ++++------ 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/background.js b/background.js index 95ccd76..e03772c 100644 --- a/background.js +++ b/background.js @@ -17,8 +17,8 @@ let gpxTrack = null; */ function toWGS84(point) { // convert LV95 into the civilian system - let y_aux = (point[0] - 2600000) / 1000000; - let x_aux = (point[1] - 1200000) / 1000000; + const y_aux = (point[0] - 2600000) / 1000000; + const x_aux = (point[1] - 1200000) / 1000000; // calculate longitude and latitude in the unit 10000" let lat = 16.9023892 + @@ -26,7 +26,7 @@ function toWGS84(point) { 0.270978 * Math.pow(y_aux, 2) - 0.002528 * Math.pow(x_aux, 2) - 0.0447 * Math.pow(y_aux, 2) * x_aux - - 0.0140 * Math.pow(x_aux, 3); + 0.014 * Math.pow(x_aux, 3); let lon = 2.6779094 + 4.728982 * y_aux + @@ -35,8 +35,8 @@ function toWGS84(point) { 0.0436 * Math.pow(y_aux, 3); // unit 10000" to 1" and seconds to degrees (dec) - lat = lat * 100 / 36; - lon = lon * 100 / 36; + lat = (lat * 100) / 36; + lon = (lon * 100) / 36; return { lat: lat, lon: lon }; } @@ -48,7 +48,7 @@ function toWGS84(point) { * @returns Track point xml node for gpx. */ function toTrackPoint(point) { - let wgs84Point = toWGS84(point); + const wgs84Point = toWGS84(point); return ``; } @@ -58,7 +58,7 @@ function toTrackPoint(point) { * @returns Way point xml node for gpx. */ function toWayPoint(point) { - let wgs84Point = toWGS84(point.geom.coordinates); + const wgs84Point = toWGS84(point.geom.coordinates); return ` ${point.altitude} @@ -73,7 +73,6 @@ function toWayPoint(point) { * @returns Combined route number, id and route title. */ function trackTitle(geoJson) { - const route = geoJson.segments[0]; const book = geoJson.book_route_number ? `${geoJson.book_route_number} - ` : ""; @@ -88,18 +87,25 @@ function trackTitle(geoJson) { * @returns Simple gpx string. */ function toGpx(geoJson) { - let trackSegments = geoJson.segments.map((segment) => { - if (segment.geom == null) return ""; - return ` + const trackSegments = geoJson.segments + .map((segment) => { + if (segment.geom == null) return ""; + return ` ${segment.geom.coordinates.map(toTrackPoint).join("")} `; - }).join(""); + }) + .join(""); - let endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : ""; - let waypoints = geoJson.waypoints - ? geoJson.waypoints.map((wp) => { - return toWayPoint(wp.reference_poi); - }).join("") + const departurePoint = geoJson.departure_point + ? toWayPoint(geoJson.departure_point) + : ""; + const endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : ""; + const waypoints = geoJson.waypoints + ? geoJson.waypoints + .map((wp) => { + return toWayPoint(wp.reference_poi); + }) + .join("") : ""; const routeTitle = trackTitle(geoJson); @@ -111,7 +117,7 @@ function toGpx(geoJson) { xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" version="1.0" creator="SAC-Tourenportal GPX Downloader"> - ${toWayPoint(geoJson.departure_point)} + ${departurePoint} ${toWayPoint(geoJson.destination_poi)} ${waypoints} ${endPoint} @@ -123,7 +129,7 @@ function toGpx(geoJson) { `; const parser = new DOMParser(); - let xmlDoc = parser.parseFromString(xmlString, "text/xml"); + const xmlDoc = parser.parseFromString(xmlString, "text/xml"); return new XMLSerializer().serializeToString(xmlDoc.documentElement); } @@ -132,26 +138,26 @@ function toGpx(geoJson) { * Intercept the download of GeoJSON data and save it for the background script. */ function listener(details) { - let filter = browser.webRequest.filterResponseData(details.requestId); - let decoder = new TextDecoder("utf-8"); - let encoder = new TextEncoder(); + const filter = browser.webRequest.filterResponseData(details.requestId); + const decoder = new TextDecoder("utf-8"); + const encoder = new TextEncoder(); - let data = []; + const data = []; filter.ondata = (event) => { data.push(event.data); }; - filter.onstop = async (event) => { - let blob = new Blob(data, { type: "text/html" }); - let buffer = await blob.arrayBuffer(); - let str = decoder.decode(buffer); + filter.onstop = async (_event) => { + const blob = new Blob(data, { type: "text/html" }); + const buffer = await blob.arrayBuffer(); + const str = decoder.decode(buffer); updateActiveTab(browser.tabs); filter.write(encoder.encode(str)); filter.close(); - let geoJson = JSON.parse(str); + const geoJson = JSON.parse(str); const routeTitle = trackTitle(geoJson); gpxTrack = { title: routeTitle, data: toGpx(geoJson) }; }; @@ -182,10 +188,10 @@ function handleClick(tab) { return; } - let blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" }); - let objectURL = URL.createObjectURL(blob); + const blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" }); + const objectURL = URL.createObjectURL(blob); - let downloading = browser.downloads.download({ + const downloading = browser.downloads.download({ url: objectURL, filename: `${gpxTrack.title}.gpx`, saveAs: true, @@ -202,14 +208,14 @@ function handleClick(tab) { /** * Update the download icon and text. */ -function updateActiveTab(tabs) { +function updateActiveTab(_tabs) { function updateTab(tabs) { if (tabs[0]) { updateIcon(tabs[0]); } } - let gettingActiveTab = browser.tabs.query({ + const gettingActiveTab = browser.tabs.query({ active: true, currentWindow: true, }); diff --git a/manifest.json b/manifest.json index b738a29..8c464a2 100644 --- a/manifest.json +++ b/manifest.json @@ -1,19 +1,15 @@ { - "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.7", + "version": "0.8", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" }, - "description": "Download gpx tracks from the sac route portal.", - "icons": { "48": "icons/map.png" }, - "permissions": [ "activeTab", "downloads", @@ -21,13 +17,13 @@ "webRequestBlocking", "https://www.sac-cas.ch/*" ], - "background": { - "scripts": ["background.js"] + "scripts": [ + "background.js" + ] }, - "browser_action": { "default_icon": "icons/map.png", "default_title": "To GPX" - } + } }