nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

tracedetrail.js (1964B)


      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|trace|iframe)\/([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             options: {
     16                 isResponseSuccess: (xhr) => [200, 500].includes(xhr.status),
     17             },
     18         }];
     19     }
     20 
     21     parseResponse(responses) {
     22         const trackId = this.urlRe.exec(this.origUrl)[1];
     23         let name = `Tracedetrail track ${trackId}`;
     24         const response = responses[0];
     25         const documentText = response.responseText;
     26         try {
     27             const geometryMatch = /geometry\s*:\s*"(.+)",\n/u.exec(documentText);
     28             if (!geometryMatch) {
     29                 let error = 'UNSUPPORTED';
     30                 if (documentText.includes("track doesn't exist") || response.status === 500) {
     31                     error = '{name} was deleted or did not exist';
     32                 } else if (documentText.includes('Private track')) {
     33                     error = '{name} is private';
     34                 }
     35                 return [{name, error}];
     36             }
     37             const encodedGeometry = geometryMatch[1];
     38             const titleMatch = /<title>.+:\s*(.+)<\/title>/u.exec(documentText);
     39             if (titleMatch) {
     40                 name = titleMatch[1];
     41             }
     42 
     43             const geometry = JSON.parse(encodedGeometry.replaceAll('\\"', '"'));
     44             const proj = L.CRS.EPSG3857;
     45             const points = geometry.map((item) => proj.unproject(L.point(item.lon, item.lat)));
     46 
     47             return [{
     48                 name,
     49                 tracks: [points]
     50             }];
     51         } catch (e) {
     52             return [{name, error: 'UNSUPPORTED'}];
     53         }
     54     }
     55 }
     56 
     57 export default Tracedetrail;