From 3c9e6a28657cdf410fd4f1067141a8261a948b78 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Tue, 15 Jun 2021 11:38:10 +0200 Subject: [PATCH 01/10] add web-ext-artifacts to gitignore --- .gitignore | 1 + manifest.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index af0faab..208f440 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ .DS_Store *.swp +web-ext-artifacts diff --git a/manifest.json b/manifest.json index 7448734..d1e6682 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, - "name": "SAC Route Portal GPX", + "name": "SAC Route Portal GPX Downloader", "version": "0.1", "developer": { "name": "Sebastian Hugentobler", From 9c78a2e931a350f791e01d1aac1b0cab4d873323 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 10:29:25 +0200 Subject: [PATCH 02/10] add wapoints to gpx file --- background.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/background.js b/background.js index 75da0bd..7d624fd 100644 --- a/background.js +++ b/background.js @@ -1,7 +1,7 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ - + let gpxTrack = null; /** @@ -13,9 +13,9 @@ let gpxTrack = null; * (https://www.swisstopo.admin.ch/en/knowledge-facts/surveying-geodesy/reference-systems/map-projections.html). * * @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N. - * @returns Track point xml node for gpx. + * @returns Calculated lat and lon. */ -function toTrackPoint(point) { +function toWGS84(point) { // convert LV95 into the civilian system let y_aux = (point[0] - 2600000)/1000000; let x_aux = (point[1] - 1200000)/1000000; @@ -28,7 +28,7 @@ function toTrackPoint(point) { 0.0447 * Math.pow(y_aux, 2) * x_aux - 0.0140 * Math.pow(x_aux, 3); - let lng = 2.6779094 + + let lon = 2.6779094 + 4.728982 * y_aux + 0.791484 * y_aux * x_aux + 0.1306 * y_aux * Math.pow(x_aux, 2) - @@ -36,9 +36,34 @@ function toTrackPoint(point) { // unit 10000" to 1" and seconds to degrees (dec) lat = lat * 100 / 36; - lng = lng * 100 / 36; + lon = lon * 100 / 36; - return ``; + return {lat: lat, lon: lon}; +} + +/** + * Get the gpx trackpoint representation of a Swiss projection coordinate point. + * + * @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N. + * @returns Track point xml node for gpx. + */ + function toTrackPoint(point) { + let wgs84Point = toWGS84(point); + return ``; +} + +/** + * Get the gpx waypoint representation of a route portal point. + * + * @returns Way point xml node for gpx. + */ + function toWayPoint(point) { + let wgs84Point = toWGS84(point.geom.coordinates); + return ` + + ${point.altitude} + ${point.display_name} + `; } /** @@ -69,6 +94,8 @@ 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)} + ${toWayPoint(geoJson.destination_poi)} Track ${routeTitle} From 3fb2db279fb853866abcf00397d76e34f247bb66 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 10:29:41 +0200 Subject: [PATCH 03/10] version 0.2 --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index d1e6682..ec55f79 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.1", + "version": "0.2", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From 8eed835aa25d392cc2c2a221375b48672b634ac0 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 12:02:26 +0200 Subject: [PATCH 04/10] version 0.3 --- README.md | 1 - background.js | 13 +++++++------ manifest.json | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8659703..537ddfa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # SAC Route Portal GPX Downloader - The [Swiss Alpine Club](https://www.sac-cas.ch/en/) has a great [route portal](https://www.sac-cas.ch/en/huts-and-tours/sac-route-portal/) for finding interesting hiking routes. diff --git a/background.js b/background.js index 7d624fd..6c7dc24 100644 --- a/background.js +++ b/background.js @@ -129,11 +129,14 @@ function listener(details) { let buffer = await blob.arrayBuffer(); let str = decoder.decode(buffer); - gpxTrack = JSON.parse(str); updateActiveTab(browser.tabs); filter.write(encoder.encode(str)); filter.close(); + + let geoJson = JSON.parse(str); + const routeTitle = trackTitle(geoJson); + gpxTrack = {title: routeTitle, data: toGpx(geoJson)}; }; return {}; @@ -162,14 +165,12 @@ function handleClick(tab) { return; } - let blob = new Blob([toGpx(gpxTrack)], {type: "application/gpx+xml"}); + let blob = new Blob([gpxTrack.data], {type: "application/gpx+xml"}); let objectURL = URL.createObjectURL(blob); - const routeTitle = trackTitle(gpxTrack); - let downloading = browser.downloads.download({ url : objectURL, - filename : `track-${routeTitle}.gpx`, + filename : `track-${gpxTrack.title}.gpx`, saveAs: true, conflictAction : 'uniquify' }); @@ -209,7 +210,7 @@ function updateIcon(tab) { tabId: tab.id }); browser.browserAction.setTitle({ - title: hasTrack ? `Download track ${trackTitle(gpxTrack)}` : 'No track selected', + title: hasTrack ? `Download track ${gpxTrack.title}` : 'No track selected', tabId: tab.id }); } diff --git a/manifest.json b/manifest.json index ec55f79..c63b54c 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.2", + "version": "0.3", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From e18added0473153f025b5587dc7ff7f1f64b3968 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 12:42:39 +0200 Subject: [PATCH 05/10] version 0.4: add all route segments to gpx file --- background.js | 11 +++++++---- manifest.json | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/background.js b/background.js index 6c7dc24..e60dbe0 100644 --- a/background.js +++ b/background.js @@ -84,7 +84,12 @@ function trackTitle(geoJson) { * @returns Simple gpx string. */ function toGpx(geoJson) { - const route = geoJson.segments[0].geom; + let trackSegments = geoJson.segments.map(segment => { + return ` + ${segment.geom.coordinates.map(toTrackPoint).join("")} + `; + }).join(""); + const routeTitle = trackTitle(geoJson); const xmlString = ` @@ -98,9 +103,7 @@ function toGpx(geoJson) { ${toWayPoint(geoJson.destination_poi)} Track ${routeTitle} - - ${route.coordinates.map(toTrackPoint).join("")} - + ${trackSegments} `; diff --git a/manifest.json b/manifest.json index c63b54c..5dbcacf 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.3", + "version": "0.4", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From 85de91c66881ec200e59060d19efdffe113352b3 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 12:59:13 +0200 Subject: [PATCH 06/10] add all waypoints and the endpoint --- background.js | 11 ++++++++++- manifest.json | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index e60dbe0..64d2af0 100644 --- a/background.js +++ b/background.js @@ -74,7 +74,9 @@ function toWGS84(point) { */ function trackTitle(geoJson) { const route = geoJson.segments[0]; - return `${geoJson.book_route_number}-${route.route_id} ${route.title}`; + const book = geoJson.book_route_number ? `${geoJson.book_route_number}-` : ""; + + return `${book}${route.route_id} ${route.title}`; } /** @@ -90,6 +92,11 @@ function toGpx(geoJson) { `; }).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 routeTitle = trackTitle(geoJson); const xmlString = ` @@ -101,6 +108,8 @@ function toGpx(geoJson) { creator="SAC-Tourenportal GPX Downloader"> ${toWayPoint(geoJson.departure_point)} ${toWayPoint(geoJson.destination_poi)} + ${waypoints} + ${endPoint} Track ${routeTitle} ${trackSegments} diff --git a/manifest.json b/manifest.json index 5dbcacf..e89dbd4 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.4", + "version": "0.5", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From 6419d5defdc994af3dddcf95c9b0193d05f7da89 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 16 Jun 2021 13:27:51 +0200 Subject: [PATCH 07/10] use correct title for track --- background.js | 8 ++++---- manifest.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/background.js b/background.js index 64d2af0..82036a2 100644 --- a/background.js +++ b/background.js @@ -74,9 +74,9 @@ function toWGS84(point) { */ function trackTitle(geoJson) { const route = geoJson.segments[0]; - const book = geoJson.book_route_number ? `${geoJson.book_route_number}-` : ""; + const book = geoJson.book_route_number ? `${geoJson.book_route_number} - ` : ""; - return `${book}${route.route_id} ${route.title}`; + return `${book}${geoJson.title}`; } /** @@ -182,7 +182,7 @@ function handleClick(tab) { let downloading = browser.downloads.download({ url : objectURL, - filename : `track-${gpxTrack.title}.gpx`, + filename : `${gpxTrack.title}.gpx`, saveAs: true, conflictAction : 'uniquify' }); @@ -222,7 +222,7 @@ function updateIcon(tab) { tabId: tab.id }); browser.browserAction.setTitle({ - title: hasTrack ? `Download track ${gpxTrack.title}` : 'No track selected', + title: hasTrack ? `Download track "${gpxTrack.title}"` : 'No track selected', tabId: tab.id }); } diff --git a/manifest.json b/manifest.json index e89dbd4..60ddb60 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.5", + "version": "0.6", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From 553f764e69577d41e1967d88c38201e6b70a20dd Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 1 Feb 2023 20:37:40 +0100 Subject: [PATCH 08/10] unify formatting --- background.js | 240 ++++++++++++++++++++++++++------------------------ 1 file changed, 126 insertions(+), 114 deletions(-) diff --git a/background.js b/background.js index 82036a2..7afc979 100644 --- a/background.js +++ b/background.js @@ -6,60 +6,60 @@ let gpxTrack = null; /** * Convert the Swiss projection coordinates to WGS84 (lat/lon). - * - * Calculations from "Approximate formulas for the transformation between Swiss - * projection coordinates and WGS84" by the Swiss Federal Office of Topography, - * swisstopo + * + * Calculations from "Approximate formulas for the transformation between Swiss + * projection coordinates and WGS84" by the Swiss Federal Office of Topography, + * swisstopo * (https://www.swisstopo.admin.ch/en/knowledge-facts/surveying-geodesy/reference-systems/map-projections.html). - * + * * @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N. * @returns Calculated lat and lon. */ function toWGS84(point) { - // convert LV95 into the civilian system - let y_aux = (point[0] - 2600000)/1000000; - let x_aux = (point[1] - 1200000)/1000000; + // convert LV95 into the civilian system + let y_aux = (point[0] - 2600000) / 1000000; + let x_aux = (point[1] - 1200000) / 1000000; - // calculate longitude and latitude in the unit 10000" - let lat = 16.9023892 + - 3.238272 * x_aux - - 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); + // calculate longitude and latitude in the unit 10000" + let lat = 16.9023892 + + 3.238272 * x_aux - + 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); - let lon = 2.6779094 + - 4.728982 * y_aux + - 0.791484 * y_aux * x_aux + - 0.1306 * y_aux * Math.pow(x_aux, 2) - - 0.0436 * Math.pow(y_aux, 3); + let lon = 2.6779094 + + 4.728982 * y_aux + + 0.791484 * y_aux * x_aux + + 0.1306 * y_aux * Math.pow(x_aux, 2) - + 0.0436 * Math.pow(y_aux, 3); - // unit 10000" to 1" and seconds to degrees (dec) - lat = lat * 100 / 36; - lon = lon * 100 / 36; + // unit 10000" to 1" and seconds to degrees (dec) + lat = lat * 100 / 36; + lon = lon * 100 / 36; - return {lat: lat, lon: lon}; + return { lat: lat, lon: lon }; } /** * Get the gpx trackpoint representation of a Swiss projection coordinate point. - * + * * @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N. * @returns Track point xml node for gpx. */ - function toTrackPoint(point) { - let wgs84Point = toWGS84(point); - return ``; +function toTrackPoint(point) { + let wgs84Point = toWGS84(point); + return ``; } /** * Get the gpx waypoint representation of a route portal point. - * + * * @returns Way point xml node for gpx. */ - function toWayPoint(point) { - let wgs84Point = toWGS84(point.geom.coordinates); - return ` +function toWayPoint(point) { + let wgs84Point = toWGS84(point.geom.coordinates); + return ` ${point.altitude} ${point.display_name} @@ -68,38 +68,42 @@ function toWGS84(point) { /** * Create track title. - * - * @param {*} geoJson + * + * @param {*} geoJson * @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} - ` : ""; + const route = geoJson.segments[0]; + const book = geoJson.book_route_number + ? `${geoJson.book_route_number} - ` + : ""; - return `${book}${geoJson.title}`; + return `${book}${geoJson.title}`; } /** * Create a gpx representation from the sac GeoJSON data. - * - * @param {*} geoJson + * + * @param {*} geoJson * @returns Simple gpx string. */ function toGpx(geoJson) { - let trackSegments = geoJson.segments.map(segment => { - return ` + let trackSegments = geoJson.segments.map((segment) => { + 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("") : ""; + let endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : ""; + let waypoints = geoJson.waypoints + ? geoJson.waypoints.map((wp) => { + return toWayPoint(wp.reference_poi); + }).join("") + : ""; - const routeTitle = trackTitle(geoJson); + const routeTitle = trackTitle(geoJson); - const xmlString = ` + const xmlString = ` `; - const parser = new DOMParser(); - let xmlDoc = parser.parseFromString(xmlString, "text/xml"); + const parser = new DOMParser(); + let xmlDoc = parser.parseFromString(xmlString, "text/xml"); - return new XMLSerializer().serializeToString(xmlDoc.documentElement); + return new XMLSerializer().serializeToString(xmlDoc.documentElement); } /** * 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(); + let filter = browser.webRequest.filterResponseData(details.requestId); + let decoder = new TextDecoder("utf-8"); + let encoder = new TextEncoder(); - let data = []; - filter.ondata = event => { - data.push(event.data); - }; + let 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) => { + let blob = new Blob(data, { type: "text/html" }); + let buffer = await blob.arrayBuffer(); + let str = decoder.decode(buffer); - updateActiveTab(browser.tabs); + updateActiveTab(browser.tabs); - filter.write(encoder.encode(str)); - filter.close(); + filter.write(encoder.encode(str)); + filter.close(); - let geoJson = JSON.parse(str); - const routeTitle = trackTitle(geoJson); - gpxTrack = {title: routeTitle, data: toGpx(geoJson)}; - }; + let geoJson = JSON.parse(str); + const routeTitle = trackTitle(geoJson); + gpxTrack = { title: routeTitle, data: toGpx(geoJson) }; + }; - return {}; + return {}; } /** * Check if a url should be intercepted. - * - * @param {*} tab + * + * @param {*} tab * @returns True if the active tab is an sac website, false otherwise. */ function checkTrack(tab) { - if (!tab.url) { - return false; - } + if (!tab.url) { + return false; + } - return tab.url.match("https://www.sac-cas.ch/.*") && gpxTrack; + return tab.url.match("https://www.sac-cas.ch/.*") && gpxTrack; } /** * If a valid tack was selected, download it as a gpx file. */ function handleClick(tab) { - const hasTrack = checkTrack(tab); - if (!hasTrack) { - return; - } + const hasTrack = checkTrack(tab); + if (!hasTrack) { + return; + } - let blob = new Blob([gpxTrack.data], {type: "application/gpx+xml"}); - let objectURL = URL.createObjectURL(blob); + let blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" }); + let objectURL = URL.createObjectURL(blob); - let downloading = browser.downloads.download({ - url : objectURL, - filename : `${gpxTrack.title}.gpx`, - saveAs: true, - conflictAction : 'uniquify' - }); - - downloading.then( - (id) => console.log(`Started downloading: ${id}`), - (error) => console.log(`Download failed: ${error}`)); - gpxTrack = null; + let downloading = browser.downloads.download({ + url: objectURL, + filename: `${gpxTrack.title}.gpx`, + saveAs: true, + conflictAction: "uniquify", + }); + + downloading.then( + (id) => console.log(`Started downloading: ${id}`), + (error) => console.log(`Download failed: ${error}`), + ); + gpxTrack = null; } /** * Update the download icon and text. */ function updateActiveTab(tabs) { - function updateTab(tabs) { - if (tabs[0]) { - updateIcon(tabs[0]); - } + function updateTab(tabs) { + if (tabs[0]) { + updateIcon(tabs[0]); } - - let gettingActiveTab = browser.tabs.query({active: true, currentWindow: true}); - gettingActiveTab.then(updateTab); + } + + let gettingActiveTab = browser.tabs.query({ + active: true, + currentWindow: true, + }); + gettingActiveTab.then(updateTab); } /** * Update the download icon. */ function updateIcon(tab) { - const hasTrack = checkTrack(tab); + const hasTrack = checkTrack(tab); - browser.browserAction.setIcon({ - path: hasTrack ? { + browser.browserAction.setIcon({ + path: hasTrack + ? { 48: "icons/map.png", - } : { + } + : { 48: "icons/map-disabled.png", }, - tabId: tab.id - }); - browser.browserAction.setTitle({ - title: hasTrack ? `Download track "${gpxTrack.title}"` : 'No track selected', - tabId: tab.id - }); + tabId: tab.id, + }); + browser.browserAction.setTitle({ + title: hasTrack + ? `Download track "${gpxTrack.title}"` + : "No track selected", + tabId: tab.id, + }); } browser.webRequest.onBeforeRequest.addListener( - listener, - {urls: ["https://www.sac-cas.ch/*[routeId]*"]}, - ["blocking"] + listener, + { urls: ["https://www.sac-cas.ch/*[routeId]*"] }, + ["blocking"], ); browser.browserAction.onClicked.addListener(handleClick); @@ -238,5 +250,5 @@ browser.browserAction.onClicked.addListener(handleClick); browser.tabs.onUpdated.addListener(updateActiveTab); browser.tabs.onActivated.addListener(updateActiveTab); browser.windows.onFocusChanged.addListener(updateActiveTab); - + updateActiveTab(); From 7ea79b6f44ba106be459accce88627b1e1f00501 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Fri, 3 Feb 2023 12:41:49 +0100 Subject: [PATCH 09/10] Don't crash if segments.geom is null. Somtimes (for example https://www.sac-cas.ch/it/capanne-e-escursioni/portale-escursionistico-del-cas/albristhubel-6246/escursioni-con-le-racchette/von-matten-simmental-2785/) segments.geom is null and the creation of the gpx xml subsequently fails. If that is the case just ignore it and do not try to turn it into a track segment. Thanks to wizche for the report and fix! --- README.md | 3 +++ background.js | 5 +++-- manifest.json | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 537ddfa..5e58206 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,6 @@ Heed their warning and do not blindly follow the gps track! Use your brain and plan your route beforehand to be aware of dangerous parts and alternatives. Take a look at their [safety instructions](https://www.sac-cas.ch/en/training-and-safety/safety/). + +## Contributors +- wizche diff --git a/background.js b/background.js index 7afc979..95ccd76 100644 --- a/background.js +++ b/background.js @@ -89,9 +89,10 @@ function trackTitle(geoJson) { */ function toGpx(geoJson) { let trackSegments = geoJson.segments.map((segment) => { + if (segment.geom == null) return ""; return ` - ${segment.geom.coordinates.map(toTrackPoint).join("")} - `; + ${segment.geom.coordinates.map(toTrackPoint).join("")} + `; }).join(""); let endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : ""; diff --git a/manifest.json b/manifest.json index 60ddb60..b738a29 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "SAC Route Portal GPX Downloader", - "version": "0.6", + "version": "0.7", "developer": { "name": "Sebastian Hugentobler", "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" From 1fdd4e71b509a9a0cd93f35adbf6e4c1740fe260 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Fri, 16 Feb 2024 11:14:32 +0100 Subject: [PATCH 10/10] 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" - } + } }