tracedetrail.js (1770B)
1 import BaseService from './baseService'; 2 import {urlViaCorsProxy} from '~/lib/CORSProxy'; 3 import L from 'leaflet'; 4 5 class Tracedetrail extends BaseService { 6 urlRe = /^https?:\/\/(?:www\.)?tracedetrail\.[a-z]{2,}.*\/trace\/trace\/([0-9]+)/u; 7 8 isOurUrl() { 9 return this.urlRe.test(this.origUrl); 10 } 11 12 requestOptions() { 13 return [{ 14 url: urlViaCorsProxy(this.origUrl), 15 }]; 16 } 17 18 parseResponse(responses) { 19 const trackId = this.urlRe.exec(this.origUrl)[1]; 20 let name = `Tracedetrail track ${trackId}`; 21 const documentText = responses[0].responseText; 22 try { 23 const geometryMatch = /geometry\s*:\s*"(.+)",\n/u.exec(documentText); 24 if (!geometryMatch) { 25 let error = 'UNSUPPORTED'; 26 if (documentText.includes("track doesn't exist")) { 27 error = '{name} was deleted or did not exist'; 28 } else if (documentText.includes('Private track')) { 29 error = '{name} is private'; 30 } 31 return [{name, error}]; 32 } 33 const encodedGeometry = geometryMatch[1]; 34 const titleMatch = /<title>.+:\s*(.+)<\/title>/u.exec(documentText); 35 if (titleMatch) { 36 name = titleMatch[1]; 37 } 38 39 const geometry = JSON.parse(encodedGeometry.replaceAll('\\"', '"')); 40 const proj = L.CRS.EPSG3857; 41 const points = geometry.map((item) => proj.unproject(L.point(item.lon, item.lat))); 42 43 return [{ 44 name, 45 tracks: [points] 46 }]; 47 } catch (e) { 48 return [{name, error: 'UNSUPPORTED'}]; 49 } 50 } 51 } 52 53 export default Tracedetrail;